1 | <?php |
||
29 | class Match implements \Caridea\Validate\Rule |
||
30 | { |
||
31 | /** |
||
32 | * @var string The pattern to match |
||
33 | */ |
||
34 | private $pattern; |
||
35 | /** |
||
36 | * @var string The failure error code |
||
37 | */ |
||
38 | private $error; |
||
39 | |||
40 | /** |
||
41 | * Modified version of the Regular Expression for URL validation. |
||
42 | * |
||
43 | * @var string Regular Expression to match URLs |
||
44 | * @copyright 2010-2013 Diego Perini (http://www.iport.it) |
||
45 | * @license http://opensource.org/licenses/MIT MIT License |
||
46 | * @link https://gist.github.com/dperini/729294 GitHub gist |
||
47 | */ |
||
48 | const URL = "_^(?:https?://)(?:\\S+(?::\\S*)?@)?(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?\$_iuS"; |
||
49 | /** |
||
50 | * RegEx taken from HTML5 spec for email input type. |
||
51 | * |
||
52 | * @var string Regular Expression to match e-mails |
||
53 | * @link http://www.w3.org/TR/html5/forms.html#e-mail-state-%28type=email%29 HTML5 Form Specification |
||
54 | */ |
||
55 | const EMAIL = '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/'; |
||
56 | /** |
||
57 | * A "pretty good" regex for ISO 8601 Dates |
||
58 | * |
||
59 | * @var string Regular Expression to match dates |
||
60 | */ |
||
61 | const DATE = '/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'; |
||
62 | |||
63 | /** |
||
64 | * Creates a new MatchRule. |
||
65 | * |
||
66 | * @param string $pattern The pattern to match |
||
67 | * @param string $error The failure error code |
||
68 | */ |
||
69 | 1 | public function __construct(string $pattern, string $error = null) |
|
74 | |||
75 | /** |
||
76 | * {@inheritDoc} |
||
77 | */ |
||
78 | 1 | public function apply($value, $data = []): ?array |
|
85 | |||
86 | /** |
||
87 | * Gets a regular expression matching rule. |
||
88 | * |
||
89 | * ```php |
||
90 | * $rule = CompareRule::matches("^[a-z]$", "i"); |
||
91 | * ``` |
||
92 | * |
||
93 | * @param string $pattern An unbounded regular expression (no delimiters!) |
||
94 | * @param string $flags Any PCRE regex flags (e.g. i, s) |
||
95 | * @return \Caridea\Validate\Rule\Match the created rule |
||
96 | */ |
||
97 | 1 | public static function like(string $pattern, string $flags = ''): Match |
|
101 | |||
102 | /** |
||
103 | * Gets a rule that matches URLs. |
||
104 | * |
||
105 | * @return \Caridea\Validate\Rule\Match the created rule |
||
106 | */ |
||
107 | 1 | public static function url(): Match |
|
111 | |||
112 | /** |
||
113 | * Gets a rule that matches e-mail addresses. |
||
114 | * |
||
115 | * @return \Caridea\Validate\Rule\Match the created rule |
||
116 | */ |
||
117 | 1 | public static function email(): Match |
|
121 | |||
122 | /** |
||
123 | * Gets a rule that matches ISO 8601 dates. |
||
124 | * |
||
125 | * @return \Caridea\Validate\Rule\Match the created rule |
||
126 | */ |
||
127 | 1 | public static function isoDate(): Match |
|
131 | } |
||
132 |