|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Respect\Validation; |
|
15
|
|
|
|
|
16
|
|
|
use finfo; |
|
17
|
|
|
use ReflectionClass; |
|
18
|
|
|
use Respect\Validation\Exceptions\AllOfException; |
|
19
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
20
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
|
21
|
|
|
use Respect\Validation\Rules\AllOf; |
|
22
|
|
|
use Respect\Validation\Rules\Key; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @method static Validator age(int $minAge = null, int $maxAge = null) |
|
26
|
|
|
* @method static Validator allOf(Validatable ...$rule) |
|
27
|
|
|
* @method static Validator alnum(string $additionalChars = null) |
|
28
|
|
|
* @method static Validator alpha(string $additionalChars = null) |
|
29
|
|
|
* @method static Validator alwaysInvalid() |
|
30
|
|
|
* @method static Validator alwaysValid() |
|
31
|
|
|
* @method static Validator anyOf() |
|
32
|
|
|
* @method static Validator arrayType() |
|
33
|
|
|
* @method static Validator arrayVal() |
|
34
|
|
|
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true) |
|
35
|
|
|
* @method static Validator base() |
|
36
|
|
|
* @method static Validator base64() |
|
37
|
|
|
* @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = true) |
|
38
|
|
|
* @method static Validator boolType() |
|
39
|
|
|
* @method static Validator boolVal() |
|
40
|
|
|
* @method static Validator bsn() |
|
41
|
|
|
* @method static Validator call() |
|
42
|
|
|
* @method static Validator callableType() |
|
43
|
|
|
* @method static Validator callback(mixed $callback) |
|
44
|
|
|
* @method static Validator charset(mixed $charset) |
|
45
|
|
|
* @method static Validator cnh() |
|
46
|
|
|
* @method static Validator cnpj() |
|
47
|
|
|
* @method static Validator consonant(string $additionalChars = null) |
|
48
|
|
|
* @method static Validator contains(mixed $containsValue, bool $identical = false) |
|
49
|
|
|
* @method static Validator countable() |
|
50
|
|
|
* @method static Validator countryCode() |
|
51
|
|
|
* @method static Validator currencyCode() |
|
52
|
|
|
* @method static Validator cpf() |
|
53
|
|
|
* @method static Validator creditCard(string $brand = null) |
|
54
|
|
|
* @method static Validator dateTime(string $format = null) |
|
55
|
|
|
* @method static Validator digit(string $additionalChars = null) |
|
56
|
|
|
* @method static Validator directory() |
|
57
|
|
|
* @method static Validator domain(bool $tldCheck = true) |
|
58
|
|
|
* @method static Validator each(Validatable $itemValidator = null, Validatable $keyValidator = null) |
|
59
|
|
|
* @method static Validator email() |
|
60
|
|
|
* @method static Validator endsWith(mixed $endValue, bool $identical = false) |
|
61
|
|
|
* @method static Validator equals(mixed $compareTo) |
|
62
|
|
|
* @method static Validator even() |
|
63
|
|
|
* @method static Validator executable() |
|
64
|
|
|
* @method static Validator exists() |
|
65
|
|
|
* @method static Validator extension(string $extension) |
|
66
|
|
|
* @method static Validator factor(int $dividend) |
|
67
|
|
|
* @method static Validator falseVal() |
|
68
|
|
|
* @method static Validator fibonacci() |
|
69
|
|
|
* @method static Validator file() |
|
70
|
|
|
* @method static Validator filterVar(int $filter, mixed $options = null) |
|
71
|
|
|
* @method static Validator finite() |
|
72
|
|
|
* @method static Validator floatVal() |
|
73
|
|
|
* @method static Validator floatType() |
|
74
|
|
|
* @method static Validator graph(string $additionalChars = null) |
|
75
|
|
|
* @method static Validator hexRgbColor() |
|
76
|
|
|
* @method static Validator identical(mixed $value) |
|
77
|
|
|
* @method static Validator identityCard(string $countryCode) |
|
78
|
|
|
* @method static Validator image(finfo $fileInfo = null) |
|
79
|
|
|
* @method static Validator imei() |
|
80
|
|
|
* @method static Validator in(mixed $haystack, bool $compareIdentical = false) |
|
81
|
|
|
* @method static Validator infinite() |
|
82
|
|
|
* @method static Validator instance(string $instanceName) |
|
83
|
|
|
* @method static Validator intVal() |
|
84
|
|
|
* @method static Validator intType() |
|
85
|
|
|
* @method static Validator ip(mixed $ipOptions = null) |
|
86
|
|
|
* @method static Validator iterableType() |
|
87
|
|
|
* @method static Validator json() |
|
88
|
|
|
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true) |
|
89
|
|
|
* @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true) |
|
90
|
|
|
* @method static Validator keySet(Key ...$rule) |
|
91
|
|
|
* @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey) |
|
92
|
|
|
* @method static Validator languageCode(string $set) |
|
93
|
|
|
* @method static Validator leapDate(string $format) |
|
94
|
|
|
* @method static Validator leapYear() |
|
95
|
|
|
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true) |
|
96
|
|
|
* @method static Validator lowercase() |
|
97
|
|
|
* @method static Validator luhn() |
|
98
|
|
|
* @method static Validator macAddress() |
|
99
|
|
|
* @method static Validator max(mixed $maxValue, bool $inclusive = true) |
|
100
|
|
|
* @method static Validator mimetype(string $mimetype) |
|
101
|
|
|
* @method static Validator min(mixed $minValue, bool $inclusive = true) |
|
102
|
|
|
* @method static Validator minimumAge(int $age, bool $format = null) |
|
103
|
|
|
* @method static Validator multiple(int $multipleOf) |
|
104
|
|
|
* @method static Validator negative() |
|
105
|
|
|
* @method static Validator nif() |
|
106
|
|
|
* @method static Validator no($useLocale = false) |
|
107
|
|
|
* @method static Validator noneOf(Validatable ...$rule) |
|
108
|
|
|
* @method static Validator not(Validatable $rule) |
|
109
|
|
|
* @method static Validator notBlank() |
|
110
|
|
|
* @method static Validator notEmpty() |
|
111
|
|
|
* @method static Validator notOptional() |
|
112
|
|
|
* @method static Validator noWhitespace() |
|
113
|
|
|
* @method static Validator nullType() |
|
114
|
|
|
* @method static Validator number() |
|
115
|
|
|
* @method static Validator numericVal() |
|
116
|
|
|
* @method static Validator objectType() |
|
117
|
|
|
* @method static Validator odd() |
|
118
|
|
|
* @method static Validator oneOf(Validatable ...$rule) |
|
119
|
|
|
* @method static Validator optional(Validatable $rule) |
|
120
|
|
|
* @method static Validator perfectSquare() |
|
121
|
|
|
* @method static Validator pesel() |
|
122
|
|
|
* @method static Validator phone() |
|
123
|
|
|
* @method static Validator phpLabel() |
|
124
|
|
|
* @method static Validator pis() |
|
125
|
|
|
* @method static Validator positive() |
|
126
|
|
|
* @method static Validator postalCode(string $countryCode) |
|
127
|
|
|
* @method static Validator primeNumber() |
|
128
|
|
|
* @method static Validator prnt(string $additionalChars = null) |
|
129
|
|
|
* @method static Validator punct(string $additionalChars = null) |
|
130
|
|
|
* @method static Validator readable() |
|
131
|
|
|
* @method static Validator regex(string $regex) |
|
132
|
|
|
* @method static Validator resourceType() |
|
133
|
|
|
* @method static Validator roman() |
|
134
|
|
|
* @method static Validator scalarVal() |
|
135
|
|
|
* @method static Validator sf(string $name, array $params = null) |
|
136
|
|
|
* @method static Validator size(string $minSize = null, string $maxSize = null) |
|
137
|
|
|
* @method static Validator slug() |
|
138
|
|
|
* @method static Validator space(string $additionalChars = null) |
|
139
|
|
|
* @method static Validator startsWith(mixed $startValue, bool $identical = false) |
|
140
|
|
|
* @method static Validator stringType() |
|
141
|
|
|
* @method static Validator stringVal() |
|
142
|
|
|
* @method static Validator subdivisionCode(string $countryCode) |
|
143
|
|
|
* @method static Validator symbolicLink() |
|
144
|
|
|
* @method static Validator tld() |
|
145
|
|
|
* @method static Validator trueVal() |
|
146
|
|
|
* @method static Validator type(string $type) |
|
147
|
|
|
* @method static Validator unique() |
|
148
|
|
|
* @method static Validator uploaded() |
|
149
|
|
|
* @method static Validator uppercase() |
|
150
|
|
|
* @method static Validator url() |
|
151
|
|
|
* @method static Validator uuid() |
|
152
|
|
|
* @method static Validator vatin(string $countryCode) |
|
153
|
|
|
* @method static Validator version() |
|
154
|
|
|
* @method static Validator videoUrl(string $service = null) |
|
155
|
|
|
* @method static Validator vowel() |
|
156
|
|
|
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null) |
|
157
|
|
|
* @method static Validator writable() |
|
158
|
|
|
* @method static Validator xdigit(string $additionalChars = null) |
|
159
|
|
|
* @method static Validator yes($useLocale = false) |
|
160
|
|
|
* @method static Validator zend(mixed $validator, array $params = null) |
|
161
|
|
|
*/ |
|
162
|
|
|
class Validator extends AllOf |
|
163
|
|
|
{ |
|
164
|
|
|
/** |
|
165
|
|
|
* @return Factory |
|
166
|
|
|
*/ |
|
167
|
2 |
|
protected static function getFactory(): Factory |
|
168
|
|
|
{ |
|
169
|
2 |
|
return Factory::getDefaultInstance(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param Factory $factory |
|
174
|
|
|
*/ |
|
175
|
|
|
public static function setFactory(Factory $factory): void |
|
176
|
|
|
{ |
|
177
|
|
|
Factory::setDefaultInstance($factory); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $rulePrefix |
|
182
|
|
|
* @param bool $prepend |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function with($rulePrefix, $prepend = false): void |
|
185
|
|
|
{ |
|
186
|
|
|
if (false === $prepend) { |
|
187
|
|
|
self::getFactory()->appendRulePrefix($rulePrefix); |
|
188
|
|
|
} else { |
|
189
|
|
|
self::getFactory()->prependRulePrefix($rulePrefix); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function check($input) |
|
194
|
|
|
{ |
|
195
|
|
|
try { |
|
196
|
|
|
return parent::check($input); |
|
197
|
|
|
} catch (ValidationException $exception) { |
|
198
|
|
|
if (1 == count($this->getRules()) && $this->template) { |
|
199
|
|
|
$exception->setTemplate($this->template); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
throw $exception; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param string $ruleName |
|
208
|
|
|
* @param array $arguments |
|
209
|
|
|
* |
|
210
|
|
|
* @return Validator |
|
211
|
|
|
*/ |
|
212
|
1 |
|
public static function __callStatic($ruleName, $arguments) |
|
213
|
|
|
{ |
|
214
|
1 |
|
if ('allOf' === $ruleName) { |
|
215
|
|
|
return static::buildRule($ruleName, $arguments); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
$validator = new static(); |
|
219
|
|
|
|
|
220
|
1 |
|
return $validator->__call($ruleName, $arguments); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param mixed $ruleSpec |
|
225
|
|
|
* @param array $arguments |
|
226
|
|
|
* |
|
227
|
|
|
* @return Validatable |
|
228
|
|
|
*/ |
|
229
|
2 |
|
public static function buildRule($ruleSpec, $arguments = []) |
|
230
|
|
|
{ |
|
231
|
|
|
try { |
|
232
|
2 |
|
return static::getFactory()->rule($ruleSpec, $arguments); |
|
233
|
1 |
|
} catch (\Exception $exception) { |
|
234
|
1 |
|
throw new ComponentException($exception->getMessage(), $exception->getCode(), $exception); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @param string $method |
|
240
|
|
|
* @param array $arguments |
|
241
|
|
|
* |
|
242
|
|
|
* @return self |
|
243
|
|
|
*/ |
|
244
|
2 |
|
public function __call($method, $arguments) |
|
245
|
|
|
{ |
|
246
|
2 |
|
return $this->addRule(static::buildRule($method, $arguments)); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Create instance validator. |
|
251
|
|
|
* |
|
252
|
|
|
* @return Validator |
|
253
|
|
|
*/ |
|
254
|
1 |
|
public static function create() |
|
255
|
|
|
{ |
|
256
|
1 |
|
$ref = new ReflectionClass(__CLASS__); |
|
257
|
|
|
|
|
258
|
1 |
|
return $ref->newInstanceArgs(func_get_args()); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: