1 | <?php |
||
20 | class Chain |
||
21 | { |
||
22 | /** |
||
23 | * The key we want to validate. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $key; |
||
28 | |||
29 | /** |
||
30 | * The name that we can use in error messages. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $name; |
||
35 | |||
36 | /** |
||
37 | * The array of all rules for this chain. |
||
38 | * |
||
39 | * @var Rule[] |
||
40 | */ |
||
41 | protected $rules = []; |
||
42 | |||
43 | /** |
||
44 | * The message stack to append messages to. |
||
45 | * |
||
46 | * @var MessageStack |
||
47 | */ |
||
48 | protected $messageStack; |
||
49 | |||
50 | /** |
||
51 | * Construct the chain. |
||
52 | * |
||
53 | * @param string $key |
||
54 | * @param string $name |
||
55 | * @param bool $required |
||
56 | * @param bool $allowEmpty |
||
57 | */ |
||
58 | 151 | public function __construct($key, $name, $required, $allowEmpty) |
|
59 | { |
||
60 | 151 | $this->key = $key; |
|
61 | 151 | $this->name = $name; |
|
62 | |||
63 | 151 | $this->addRule(new Rule\Required($required)); |
|
64 | 151 | $this->addRule(new Rule\NotEmpty($allowEmpty)); |
|
65 | 151 | } |
|
66 | |||
67 | /** |
||
68 | * Overwrite the default __clone behaviour to make sure the rules are cloned too. |
||
69 | */ |
||
70 | 3 | public function __clone() |
|
71 | { |
||
72 | 3 | $rules = []; |
|
73 | 3 | foreach ($this->rules as $rule) { |
|
74 | 3 | $rules[] = clone $rule; |
|
75 | 3 | } |
|
76 | 3 | $this->rules = $rules; |
|
77 | 3 | } |
|
78 | |||
79 | /** |
||
80 | * Validate the value to consist only out of alphanumeric characters. |
||
81 | * |
||
82 | * @param bool $allowWhitespace |
||
83 | * @return $this |
||
84 | */ |
||
85 | 7 | public function alnum($allowWhitespace = false) |
|
89 | |||
90 | /** |
||
91 | * Validate that the value only consists our of alphabetic characters. |
||
92 | * |
||
93 | * @param bool $allowWhitespace |
||
94 | * @return $this |
||
95 | */ |
||
96 | 7 | public function alpha($allowWhitespace = false) |
|
100 | |||
101 | /** |
||
102 | * Validate that the value is between $min and $max (inclusive). |
||
103 | * |
||
104 | * @param int $min |
||
105 | * @param int $max |
||
106 | * @return $this |
||
107 | */ |
||
108 | 6 | public function between($min, $max) |
|
112 | |||
113 | /** |
||
114 | * Validate that the value is a boolean. |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | 9 | public function bool() |
|
122 | |||
123 | /** |
||
124 | * Validate by executing a callback function, and returning its result. |
||
125 | * |
||
126 | * @param callable $callable |
||
127 | * @return $this |
||
128 | */ |
||
129 | 5 | public function callback(callable $callable) |
|
133 | |||
134 | /** |
||
135 | * Validates that the value is a date. If format is passed, it *must* be in that format. |
||
136 | * |
||
137 | * @param string|null $format |
||
138 | * @return $this |
||
139 | */ |
||
140 | 8 | public function datetime($format = null) |
|
144 | |||
145 | /** |
||
146 | * Validates that all characters of the value are decimal digits. |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | 4 | public function digits() |
|
154 | |||
155 | /** |
||
156 | * Validates a value to be a nested array, which can then be validated using a new Validator instance. |
||
157 | * |
||
158 | * @param callable $callback |
||
159 | * @return $this |
||
160 | */ |
||
161 | 5 | public function each(callable $callback) |
|
165 | |||
166 | /** |
||
167 | * Validates that the value is a valid email address (format only). |
||
168 | * @return $this |
||
169 | */ |
||
170 | 6 | public function email() |
|
174 | |||
175 | /** |
||
176 | * Validates that the value is equal to $value. |
||
177 | * |
||
178 | * @param string $value |
||
179 | * @return $this |
||
180 | */ |
||
181 | 2 | public function equals($value) |
|
185 | |||
186 | /** |
||
187 | * Validates that the value is greater than $value. |
||
188 | * |
||
189 | * @param int $value |
||
190 | * @return $this |
||
191 | */ |
||
192 | 3 | public function greaterThan($value) |
|
196 | |||
197 | /** |
||
198 | * Validates that the value is in the array with optional "loose" checking. |
||
199 | * |
||
200 | * @param string $hashAlgorithm |
||
201 | * @param bool $allowUppercase |
||
202 | * @return $this |
||
203 | * @see \Particle\Validator\Rule\Hash |
||
204 | 4 | */ |
|
205 | public function hash($hashAlgorithm, $allowUppercase = false) |
||
209 | |||
210 | /** |
||
211 | * Validates that the value is in the array with optional "loose" checking. |
||
212 | * |
||
213 | * @param array $array |
||
214 | * @param bool $strict |
||
215 | 10 | * @return $this |
|
216 | */ |
||
217 | 10 | public function inArray(array $array, $strict = true) |
|
221 | |||
222 | /** |
||
223 | * Validates the value represents a valid integer |
||
224 | * |
||
225 | * @return $this |
||
226 | 11 | * @see \Particle\Validator\Rule\Integer |
|
227 | */ |
||
228 | 11 | public function integer() |
|
232 | |||
233 | /** |
||
234 | * Validate the value to be of precisely length $length. |
||
235 | * |
||
236 | * @param int $length |
||
237 | 13 | * @return $this |
|
238 | */ |
||
239 | 13 | public function length($length) |
|
243 | |||
244 | /** |
||
245 | * Validates that the length of the value is between $min and $max. |
||
246 | * |
||
247 | * If $max is null, it has no upper limit. The default is inclusive. |
||
248 | * |
||
249 | * @param int $min |
||
250 | * @param int|null $max |
||
251 | 7 | * @return $this |
|
252 | */ |
||
253 | 7 | public function lengthBetween($min, $max) |
|
257 | |||
258 | /** |
||
259 | * Validates that the value is less than $value. |
||
260 | * |
||
261 | * @param int $value |
||
262 | 3 | * @return $this |
|
263 | */ |
||
264 | 3 | public function lessThan($value) |
|
268 | |||
269 | /** |
||
270 | * Mount a rule object onto this chain. |
||
271 | * |
||
272 | * @param Rule $rule |
||
273 | 1 | * @return $this |
|
274 | */ |
||
275 | 1 | public function mount(Rule $rule) |
|
279 | |||
280 | /** |
||
281 | * Validates that the value is either a integer or a float. |
||
282 | * |
||
283 | 11 | * @return $this |
|
284 | */ |
||
285 | 11 | public function numeric() |
|
289 | |||
290 | /** |
||
291 | * Validates that the value matches the regular expression $regex. |
||
292 | * |
||
293 | * @param string $regex |
||
294 | 2 | * @return $this |
|
295 | */ |
||
296 | 2 | public function regex($regex) |
|
300 | |||
301 | /** |
||
302 | * Validates that the value is a valid URL. The schemes array is to selectively whitelist URL schemes. |
||
303 | * |
||
304 | * @param array $schemes |
||
305 | 7 | * @return $this |
|
306 | */ |
||
307 | 7 | public function url(array $schemes = []) |
|
311 | |||
312 | /** |
||
313 | * Validates that the value is a valid UUID |
||
314 | * |
||
315 | * @param int $version |
||
316 | 12 | * @return $this |
|
317 | */ |
||
318 | 12 | public function uuid($version = Rule\Uuid::UUID_V4) |
|
322 | |||
323 | /** |
||
324 | * Set a callable or boolean value which may be used to alter the required requirement on validation time. |
||
325 | * |
||
326 | * This may be incredibly helpful when doing conditional validation. |
||
327 | * |
||
328 | * @param callable|bool $required |
||
329 | 5 | * @return $this |
|
330 | */ |
||
331 | 5 | public function required($required) |
|
336 | |||
337 | /** |
||
338 | * Set a callable or boolean value which may be used to alter the allow empty requirement on validation time. |
||
339 | * |
||
340 | * This may be incredibly helpful when doing conditional validation. |
||
341 | * |
||
342 | * @param callable|bool $allowEmpty |
||
343 | 4 | * @return $this |
|
344 | */ |
||
345 | 4 | public function allowEmpty($allowEmpty) |
|
350 | |||
351 | /** |
||
352 | * Attach a representation of this Chain to the Output\Structure $structure. |
||
353 | * |
||
354 | * @internal |
||
355 | * @param Structure $structure |
||
356 | * @param MessageStack $messageStack |
||
357 | 2 | * @return Structure |
|
358 | */ |
||
359 | 2 | public function output(Structure $structure, MessageStack $messageStack) |
|
371 | |||
372 | /** |
||
373 | * Validates the values in the $values array and appends messages to $messageStack. Returns the result as a bool. |
||
374 | * |
||
375 | * @param MessageStack $messageStack |
||
376 | * @param Container $input |
||
377 | * @param Container $output |
||
378 | 148 | * @return bool |
|
379 | */ |
||
380 | 148 | public function validate(MessageStack $messageStack, Container $input, Container $output) |
|
399 | |||
400 | /** |
||
401 | * Shortcut method for storing a rule on this chain, and returning the chain. |
||
402 | * |
||
403 | * @param Rule $rule |
||
404 | 151 | * @return $this |
|
405 | */ |
||
406 | 151 | protected function addRule(Rule $rule) |
|
412 | |||
413 | /** |
||
414 | * Returns the first rule, which is always the required rule. |
||
415 | * |
||
416 | 5 | * @return Rule\Required |
|
417 | */ |
||
418 | 5 | protected function getRequiredRule() |
|
422 | |||
423 | /** |
||
424 | * Returns the second rule, which is always the allow empty rule. |
||
425 | * |
||
426 | 4 | * @return Rule\NotEmpty |
|
427 | */ |
||
428 | 4 | protected function getNotEmptyRule() |
|
432 | } |
||
433 |