1
|
|
|
<?php |
2
|
|
|
namespace Respect\Validation; |
3
|
|
|
|
4
|
|
|
use ReflectionClass; |
5
|
|
|
use ReflectionException; |
6
|
|
|
use Respect\Validation\Exceptions\AllOfException; |
7
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
8
|
|
|
use Respect\Validation\Rules\AllOf; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method static Validator age(int $minAge = null, int $maxAge = null) |
12
|
|
|
* @method static Validator allOf() |
13
|
|
|
* @method static Validator alnum(string $additionalChars = null) |
14
|
|
|
* @method static Validator alpha(string $additionalChars = null) |
15
|
|
|
* @method static Validator alwaysInvalid() |
16
|
|
|
* @method static Validator alwaysValid() |
17
|
|
|
* @method static Validator arr() |
18
|
|
|
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true) |
19
|
|
|
* @method static Validator bank(string $countryCode) |
20
|
|
|
* @method static Validator bankAccount(string $countryCode) |
21
|
|
|
* @method static Validator base() |
22
|
|
|
* @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = false) |
23
|
|
|
* @method static Validator bic(string $countryCode) |
24
|
|
|
* @method static Validator bool() |
25
|
|
|
* @method static Validator call() |
26
|
|
|
* @method static Validator callback(mixed $callback) |
27
|
|
|
* @method static Validator charset(mixed $charset) |
28
|
|
|
* @method static Validator cnh() |
29
|
|
|
* @method static Validator cnpj() |
30
|
|
|
* @method static Validator consonant(string $additionalChars = null) |
31
|
|
|
* @method static Validator contains(mixed $containsValue, bool $identical = false) |
32
|
|
|
* @method static Validator countryCode() |
33
|
|
|
* @method static Validator cpf() |
34
|
|
|
* @method static Validator creditCard() |
35
|
|
|
* @method static Validator date(string $format = null) |
36
|
|
|
* @method static Validator digit(string $additionalChars = null) |
37
|
|
|
* @method static Validator directory() |
38
|
|
|
* @method static Validator domain() |
39
|
|
|
* @method static Validator each(Validatable $itemValidator = null, Validatable $keyValidator = null) |
40
|
|
|
* @method static Validator email() |
41
|
|
|
* @method static Validator endsWith(mixed $endValue, bool $identical = false) |
42
|
|
|
* @method static Validator equals(mixed $compareTo, bool $compareIdentical = false) |
43
|
|
|
* @method static Validator even() |
44
|
|
|
* @method static Validator executable() |
45
|
|
|
* @method static Validator exists() |
46
|
|
|
* @method static Validator false() |
47
|
|
|
* @method static Validator file() |
48
|
|
|
* @method static Validator filterVar(int $filter, mixed $options = null) |
49
|
|
|
* @method static Validator float() |
50
|
|
|
* @method static Validator graph(string $additionalChars = null) |
51
|
|
|
* @method static Validator hexRgbColor() |
52
|
|
|
* @method static Validator in(mixed $haystack, bool $compareIdentical = false) |
53
|
|
|
* @method static Validator instance(string $instanceName) |
54
|
|
|
* @method static Validator int() |
55
|
|
|
* @method static Validator ip(mixed $ipOptions = null) |
56
|
|
|
* @method static Validator json() |
57
|
|
|
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true) |
58
|
|
|
* @method static Validator leapDate(string $format) |
59
|
|
|
* @method static Validator leapYear() |
60
|
|
|
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true) |
61
|
|
|
* @method static Validator lowercase() |
62
|
|
|
* @method static Validator macAddress() |
63
|
|
|
* @method static Validator max(mixed $maxValue, bool $inclusive = false) |
64
|
|
|
* @method static Validator min(mixed $minValue, bool $inclusive = false) |
65
|
|
|
* @method static Validator minimumAge(int $age) |
66
|
|
|
* @method static Validator multiple(int $multipleOf) |
67
|
|
|
* @method static Validator negative() |
68
|
|
|
* @method static Validator no($useLocale = false) |
69
|
|
|
* @method static Validator noneOf() |
70
|
|
|
* @method static Validator not(Validatable $rule) |
71
|
|
|
* @method static Validator notEmpty() |
72
|
|
|
* @method static Validator noWhitespace() |
73
|
|
|
* @method static Validator nullValue() |
74
|
|
|
* @method static Validator numeric() |
75
|
|
|
* @method static Validator object() |
76
|
|
|
* @method static Validator odd() |
77
|
|
|
* @method static Validator oneOf() |
78
|
|
|
* @method static Validator perfectSquare() |
79
|
|
|
* @method static Validator phone() |
80
|
|
|
* @method static Validator positive() |
81
|
|
|
* @method static Validator postalCode(string $countryCode) |
82
|
|
|
* @method static Validator primeNumber() |
83
|
|
|
* @method static Validator prnt(string $additionalChars = null) |
84
|
|
|
* @method static Validator punct(string $additionalChars = null) |
85
|
|
|
* @method static Validator readable() |
86
|
|
|
* @method static Validator regex(string $regex) |
87
|
|
|
* @method static Validator roman() |
88
|
|
|
* @method static Validator sf(string $name, array $params = null) |
89
|
|
|
* @method static Validator slug() |
90
|
|
|
* @method static Validator space(string $additionalChars = null) |
91
|
|
|
* @method static Validator startsWith(mixed $startValue, bool $identical = false) |
92
|
|
|
* @method static Validator string() |
93
|
|
|
* @method static Validator symbolicLink() |
94
|
|
|
* @method static Validator tld() |
95
|
|
|
* @method static Validator true() |
96
|
|
|
* @method static Validator type(string $type) |
97
|
|
|
* @method static Validator uploaded() |
98
|
|
|
* @method static Validator uppercase() |
99
|
|
|
* @method static Validator url() |
100
|
|
|
* @method static Validator version() |
101
|
|
|
* @method static Validator vowel() |
102
|
|
|
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null) |
103
|
|
|
* @method static Validator writable() |
104
|
|
|
* @method static Validator xdigit(string $additionalChars = null) |
105
|
|
|
* @method static Validator yes($useLocale = false) |
106
|
|
|
* @method static Validator zend(mixed $validator, array $params = null) |
107
|
|
|
*/ |
108
|
|
|
class Validator extends AllOf |
109
|
|
|
{ |
110
|
|
|
protected static $factory; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Factory |
114
|
|
|
*/ |
115
|
28 |
|
protected static function getFactory() |
116
|
|
|
{ |
117
|
28 |
|
if (! static::$factory instanceof Factory) { |
118
|
|
|
static::$factory = new Factory(); |
119
|
|
|
} |
120
|
|
|
|
121
|
28 |
|
return static::$factory; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param Factory $factory |
126
|
|
|
* |
127
|
|
|
* @return null |
128
|
|
|
*/ |
129
|
|
|
public static function setFactory($factory) |
130
|
|
|
{ |
131
|
|
|
static::$factory = $factory; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $rulePrefix |
136
|
|
|
* @param bool $prepend |
137
|
|
|
* |
138
|
|
|
* @return null |
139
|
|
|
*/ |
140
|
|
|
public static function with($rulePrefix, $prepend = false) |
141
|
|
|
{ |
142
|
|
|
if (false === $prepend) { |
143
|
|
|
self::getFactory()->appendRulePrefix($rulePrefix); |
144
|
|
|
} else { |
145
|
|
|
self::getFactory()->prependRulePrefix($rulePrefix); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $ruleName |
151
|
|
|
* @param array $arguments |
152
|
|
|
* |
153
|
|
|
* @return Validator |
154
|
|
|
*/ |
155
|
26 |
|
public static function __callStatic($ruleName, $arguments) |
156
|
|
|
{ |
157
|
26 |
|
if ('allOf' === $ruleName) { |
158
|
2 |
|
return static::buildRule($ruleName, $arguments); |
159
|
|
|
} |
160
|
|
|
|
161
|
26 |
|
$validator = new static(); |
162
|
|
|
|
163
|
26 |
|
return $validator->__call($ruleName, $arguments); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param mixed $ruleSpec |
168
|
|
|
* @param array $arguments |
169
|
|
|
* |
170
|
|
|
* @return Validatable |
171
|
|
|
*/ |
172
|
28 |
|
public static function buildRule($ruleSpec, $arguments = array()) |
173
|
|
|
{ |
174
|
|
|
try { |
175
|
28 |
|
return static::getFactory()->rule($ruleSpec, $arguments); |
176
|
2 |
|
} catch (\Exception $exception) { |
177
|
2 |
|
throw new ComponentException($exception->getMessage(), $exception->getCode(), $exception); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $method |
183
|
|
|
* @param array $arguments |
184
|
|
|
* |
185
|
|
|
* @return self |
186
|
|
|
*/ |
187
|
28 |
|
public function __call($method, $arguments) |
188
|
|
|
{ |
189
|
28 |
|
if ('not' === $method && empty($arguments)) { |
190
|
3 |
|
return new static(new Rules\Not($this)); |
191
|
|
|
} |
192
|
|
|
|
193
|
27 |
|
return $this->addRule(static::buildRule($method, $arguments)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param mixed $input |
199
|
|
|
* @param array $extraParams |
200
|
|
|
* |
201
|
|
|
* @return AllOfException |
202
|
|
|
*/ |
203
|
11 |
|
public function reportError($input, array $extraParams = array()) |
204
|
|
|
{ |
205
|
11 |
|
$exception = new AllOfException(); |
206
|
11 |
|
$input = AllOfException::stringify($input); |
207
|
11 |
|
$name = $this->getName() ?: "\"$input\""; |
208
|
11 |
|
$params = array_merge( |
209
|
11 |
|
$extraParams, |
210
|
11 |
|
get_object_vars($this), |
211
|
11 |
|
get_class_vars(__CLASS__) |
212
|
11 |
|
); |
213
|
11 |
|
$exception->configure($name, $params); |
214
|
11 |
|
if (!is_null($this->template)) { |
215
|
3 |
|
$exception->setTemplate($this->template); |
216
|
3 |
|
} |
217
|
|
|
|
218
|
11 |
|
return $exception; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Create instance validator |
223
|
|
|
* |
224
|
|
|
* @return Validator |
225
|
|
|
*/ |
226
|
1 |
|
public static function create() |
227
|
|
|
{ |
228
|
1 |
|
$ref = new ReflectionClass(__CLASS__); |
229
|
|
|
|
230
|
1 |
|
return $ref->newInstanceArgs(func_get_args()); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|