|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Caridea |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Caridea\Validate\Rule; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Pattern matching rule. |
|
25
|
|
|
* |
|
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
|
27
|
|
|
* @license Apache-2.0 |
|
28
|
|
|
*/ |
|
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) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->pattern = (string) $pattern; |
|
72
|
1 |
|
$this->error = strlen(trim((string)$error)) > 0 ? (string) $error : 'WRONG_FORMAT'; |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function apply($value, $data = []): ?array |
|
79
|
|
|
{ |
|
80
|
1 |
|
if (!is_scalar($value)) { |
|
81
|
1 |
|
return ['FORMAT_ERROR']; |
|
82
|
|
|
} |
|
83
|
1 |
|
return preg_match($this->pattern, $value) ? null : [$this->error]; |
|
84
|
|
|
} |
|
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 |
|
98
|
|
|
{ |
|
99
|
1 |
|
return new Match("/$pattern/$flags", 'WRONG_FORMAT'); |
|
100
|
|
|
} |
|
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 |
|
108
|
|
|
{ |
|
109
|
1 |
|
return new Match(self::URL, 'WRONG_URL'); |
|
110
|
|
|
} |
|
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 |
|
118
|
|
|
{ |
|
119
|
1 |
|
return new Match(self::EMAIL, 'WRONG_EMAIL'); |
|
120
|
|
|
} |
|
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 |
|
128
|
|
|
{ |
|
129
|
1 |
|
return new Match(self::DATE, 'WRONG_DATE'); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|