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