-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegExp.php
56 lines (49 loc) · 1.64 KB
/
RegExp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Colibri\Util;
use RuntimeException;
/**
* RegExp popular patterns.
*/
class RegExp
{
public const IS_DATE = '/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$/';
public const IS_EMAIL = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
/**
* Returns array of found matches or empty array.
* If error occurred, throws RuntimeException with preg_last_error_msg(), preg_last_error().
*
* @param string $pattern
* @param string $subject
*
* @return array
*
* @throws RuntimeException
*/
public static function findAll(string $pattern, string $subject): array
{
$found = preg_match_all($pattern, $subject, $matches);
if ($found === false) {
throw new RuntimeException(preg_last_error_msg(), preg_last_error());
}
return $found ? Arr::transpose($matches) : [];
}
/**
* Returns found matches or null.
* If error occurred, throws RuntimeException with preg_last_error_msg(), preg_last_error().
*
* @param string $pattern
* @param string $subject
*
* @return array|null
*
* @throws RuntimeException
*/
public static function findFirst(string $pattern, string $subject): ?array
{
$found = preg_match($pattern, $subject, $matches);
if ($found === false) {
throw new RuntimeException(preg_last_error_msg(), preg_last_error());
}
return $found ? $matches : null;
}
}