|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Particle. |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/particle-php for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com) |
|
7
|
|
|
* @license https://github.com/particle-php/validator/blob/master/LICENSE New BSD License |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Particle\Validator; |
|
10
|
|
|
|
|
11
|
|
|
use Particle\Validator\Output\Structure; |
|
12
|
|
|
use Particle\Validator\Output\Subject; |
|
13
|
|
|
use Particle\Validator\Value\Container; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Represents a collection of Rules which may break the chain of validation (but usually don't). |
|
17
|
|
|
* |
|
18
|
|
|
* @package Particle\Validator |
|
19
|
|
|
*/ |
|
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
|
140 |
|
public function __construct($key, $name, $required, $allowEmpty) |
|
59
|
|
|
{ |
|
60
|
140 |
|
$this->key = $key; |
|
61
|
140 |
|
$this->name = $name; |
|
62
|
|
|
|
|
63
|
140 |
|
$this->addRule(new Rule\Required($required)); |
|
64
|
140 |
|
$this->addRule(new Rule\NotEmpty($allowEmpty)); |
|
65
|
140 |
|
} |
|
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) |
|
86
|
|
|
{ |
|
87
|
7 |
|
return $this->addRule(new Rule\Alnum($allowWhitespace)); |
|
88
|
|
|
} |
|
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) |
|
97
|
|
|
{ |
|
98
|
7 |
|
return $this->addRule(new Rule\Alpha($allowWhitespace)); |
|
99
|
|
|
} |
|
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) |
|
109
|
|
|
{ |
|
110
|
6 |
|
return $this->addRule(new Rule\Between($min, $max)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Validate that the value is a boolean. |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
9 |
|
public function bool() |
|
119
|
|
|
{ |
|
120
|
9 |
|
return $this->addRule(new Rule\Boolean()); |
|
121
|
|
|
} |
|
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) |
|
130
|
|
|
{ |
|
131
|
5 |
|
return $this->addRule(new Rule\Callback($callable)); |
|
132
|
|
|
} |
|
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) |
|
141
|
|
|
{ |
|
142
|
8 |
|
return $this->addRule(new Rule\Datetime($format)); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Validates that all characters of the value are decimal digits. |
|
147
|
|
|
* |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
4 |
|
public function digits() |
|
151
|
|
|
{ |
|
152
|
4 |
|
return $this->addRule(new Rule\Digits()); |
|
153
|
|
|
} |
|
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) |
|
162
|
|
|
{ |
|
163
|
5 |
|
return $this->addRule(new Rule\Each($callback)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Validates that the value is a valid email address (format only). |
|
168
|
|
|
* @return $this |
|
169
|
|
|
*/ |
|
170
|
6 |
|
public function email() |
|
171
|
|
|
{ |
|
172
|
6 |
|
return $this->addRule(new Rule\Email()); |
|
173
|
|
|
} |
|
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) |
|
182
|
|
|
{ |
|
183
|
2 |
|
return $this->addRule(new Rule\Equal($value)); |
|
184
|
|
|
} |
|
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) |
|
193
|
|
|
{ |
|
194
|
3 |
|
return $this->addRule(new Rule\GreaterThan($value)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Validates that the value is in the array with optional "loose" checking. |
|
199
|
|
|
* |
|
200
|
|
|
* @param array $array |
|
201
|
|
|
* @param bool $strict |
|
202
|
|
|
* @return $this |
|
203
|
|
|
*/ |
|
204
|
4 |
|
public function inArray(array $array, $strict = true) |
|
205
|
|
|
{ |
|
206
|
4 |
|
return $this->addRule(new Rule\InArray($array, $strict)); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Validates the value represents a valid integer |
|
211
|
|
|
* |
|
212
|
|
|
* @return $this |
|
213
|
|
|
* @see \Particle\Validator\Rule\Integer |
|
214
|
|
|
*/ |
|
215
|
10 |
|
public function integer() |
|
216
|
|
|
{ |
|
217
|
10 |
|
return $this->addRule(new Rule\Integer()); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Validate the value to be of precisely length $length. |
|
222
|
|
|
* |
|
223
|
|
|
* @param int $length |
|
224
|
|
|
* @return $this |
|
225
|
|
|
*/ |
|
226
|
13 |
|
public function length($length) |
|
227
|
|
|
{ |
|
228
|
13 |
|
return $this->addRule(new Rule\Length($length)); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Validates that the length of the value is between $min and $max. |
|
233
|
|
|
* |
|
234
|
|
|
* If $max is null, it has no upper limit. The default is inclusive. |
|
235
|
|
|
* |
|
236
|
|
|
* @param int $min |
|
237
|
|
|
* @param int|null $max |
|
238
|
|
|
* @return $this |
|
239
|
|
|
*/ |
|
240
|
7 |
|
public function lengthBetween($min, $max) |
|
241
|
|
|
{ |
|
242
|
7 |
|
return $this->addRule(new Rule\LengthBetween($min, $max)); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Validates that the value is less than $value. |
|
247
|
|
|
* |
|
248
|
|
|
* @param int $value |
|
249
|
|
|
* @return $this |
|
250
|
|
|
*/ |
|
251
|
3 |
|
public function lessThan($value) |
|
252
|
|
|
{ |
|
253
|
3 |
|
return $this->addRule(new Rule\LessThan($value)); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Mount a rule object onto this chain. |
|
258
|
|
|
* |
|
259
|
|
|
* @param Rule $rule |
|
260
|
|
|
* @return $this |
|
261
|
|
|
*/ |
|
262
|
1 |
|
public function mount(Rule $rule) |
|
263
|
|
|
{ |
|
264
|
1 |
|
return $this->addRule($rule); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Validates that the value is either a integer or a float. |
|
269
|
|
|
* |
|
270
|
|
|
* @return $this |
|
271
|
|
|
*/ |
|
272
|
11 |
|
public function numeric() |
|
273
|
|
|
{ |
|
274
|
11 |
|
return $this->addRule(new Rule\Numeric()); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Validates that the value matches the regular expression $regex. |
|
279
|
|
|
* |
|
280
|
|
|
* @param string $regex |
|
281
|
|
|
* @return $this |
|
282
|
|
|
*/ |
|
283
|
2 |
|
public function regex($regex) |
|
284
|
|
|
{ |
|
285
|
2 |
|
return $this->addRule(new Rule\Regex($regex)); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Validates that the value is a valid URL. The schemes array is to selectively whitelist URL schemes. |
|
290
|
|
|
* |
|
291
|
|
|
* @param array $schemes |
|
292
|
|
|
* @return $this |
|
293
|
|
|
*/ |
|
294
|
7 |
|
public function url(array $schemes = []) |
|
295
|
|
|
{ |
|
296
|
7 |
|
return $this->addRule(new Rule\Url($schemes)); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Validates that the value is a valid UUID |
|
301
|
|
|
* |
|
302
|
|
|
* @param int $version |
|
303
|
|
|
* @return $this |
|
304
|
|
|
*/ |
|
305
|
12 |
|
public function uuid($version = Rule\Uuid::UUID_V4) |
|
306
|
|
|
{ |
|
307
|
12 |
|
return $this->addRule(new Rule\Uuid($version)); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Set a callable or boolean value which may be used to alter the required requirement on validation time. |
|
312
|
|
|
* |
|
313
|
|
|
* This may be incredibly helpful when doing conditional validation. |
|
314
|
|
|
* |
|
315
|
|
|
* @param callable|bool $required |
|
316
|
|
|
* @return $this |
|
317
|
|
|
*/ |
|
318
|
5 |
|
public function required($required) |
|
319
|
|
|
{ |
|
320
|
5 |
|
$this->getRequiredRule()->setRequired($required); |
|
321
|
5 |
|
return $this; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Set a callable or boolean value which may be used to alter the allow empty requirement on validation time. |
|
326
|
|
|
* |
|
327
|
|
|
* This may be incredibly helpful when doing conditional validation. |
|
328
|
|
|
* |
|
329
|
|
|
* @param callable|bool $allowEmpty |
|
330
|
|
|
* @return $this |
|
331
|
|
|
*/ |
|
332
|
4 |
|
public function allowEmpty($allowEmpty) |
|
333
|
|
|
{ |
|
334
|
4 |
|
$this->getNotEmptyRule()->setAllowEmpty($allowEmpty); |
|
335
|
4 |
|
return $this; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Attach a representation of this Chain to the Output\Structure $structure. |
|
340
|
|
|
* |
|
341
|
|
|
* @internal |
|
342
|
|
|
* @param Structure $structure |
|
343
|
|
|
* @param MessageStack $messageStack |
|
344
|
|
|
* @return Structure |
|
345
|
|
|
*/ |
|
346
|
2 |
|
public function output(Structure $structure, MessageStack $messageStack) |
|
347
|
|
|
{ |
|
348
|
2 |
|
$subject = new Subject($this->key, $this->name); |
|
349
|
|
|
|
|
350
|
2 |
|
foreach ($this->rules as $rule) { |
|
351
|
2 |
|
$rule->output($subject, $messageStack); |
|
352
|
2 |
|
} |
|
353
|
|
|
|
|
354
|
2 |
|
$structure->addSubject($subject); |
|
355
|
|
|
|
|
356
|
2 |
|
return $structure; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Validates the values in the $values array and appends messages to $messageStack. Returns the result as a bool. |
|
361
|
|
|
* |
|
362
|
|
|
* @param MessageStack $messageStack |
|
363
|
|
|
* @param Container $input |
|
364
|
|
|
* @param Container $output |
|
365
|
|
|
* @return bool |
|
366
|
|
|
*/ |
|
367
|
137 |
|
public function validate(MessageStack $messageStack, Container $input, Container $output) |
|
368
|
|
|
{ |
|
369
|
137 |
|
$valid = true; |
|
370
|
137 |
|
foreach ($this->rules as $rule) { |
|
371
|
137 |
|
$rule->setMessageStack($messageStack); |
|
372
|
137 |
|
$rule->setParameters($this->key, $this->name); |
|
373
|
|
|
|
|
374
|
137 |
|
$valid = $rule->isValid($this->key, $input) && $valid; |
|
375
|
|
|
|
|
376
|
137 |
|
if ($rule->shouldBreakChain()) { |
|
377
|
15 |
|
break; |
|
378
|
|
|
} |
|
379
|
137 |
|
} |
|
380
|
|
|
|
|
381
|
137 |
|
if ($valid && $input->has($this->key)) { |
|
382
|
74 |
|
$output->set($this->key, $input->get($this->key)); |
|
383
|
74 |
|
} |
|
384
|
137 |
|
return $valid; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Shortcut method for storing a rule on this chain, and returning the chain. |
|
389
|
|
|
* |
|
390
|
|
|
* @param Rule $rule |
|
391
|
|
|
* @return $this |
|
392
|
|
|
*/ |
|
393
|
140 |
|
protected function addRule(Rule $rule) |
|
394
|
|
|
{ |
|
395
|
140 |
|
$this->rules[] = $rule; |
|
396
|
|
|
|
|
397
|
140 |
|
return $this; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Returns the first rule, which is always the required rule. |
|
402
|
|
|
* |
|
403
|
|
|
* @return Rule\Required |
|
404
|
|
|
*/ |
|
405
|
5 |
|
protected function getRequiredRule() |
|
406
|
|
|
{ |
|
407
|
5 |
|
return $this->rules[0]; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* Returns the second rule, which is always the allow empty rule. |
|
412
|
|
|
* |
|
413
|
|
|
* @return Rule\NotEmpty |
|
414
|
|
|
*/ |
|
415
|
4 |
|
protected function getNotEmptyRule() |
|
416
|
|
|
{ |
|
417
|
4 |
|
return $this->rules[1]; |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
|