-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathValidator.php
406 lines (363 loc) · 12 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/*
* This file is part of the Pajama package.
*
* (c) Cameron McKay <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cdmckay\Pajama;
use Cdmckay\Pajama\ValidatorContext;
/**
* A class for validating array models, typically the <code>$_POST</code> or <code>$_GET</code> superglobals.
*/
final class Validator {
/**
* @var array The model being validated.
*/
private $model;
/**
* @var array The normalized rules for this instance.
*/
private $rules;
/**
* @var ValidatorContext A reference to the context for this instance.
*/
private $context;
/**
* @var array An array containing all the validation methods.
*/
private static $methods = array();
/**
* Creates a new Validator instance.
*
* @param array $model The model to validate.
* @param array $rules The rules the model is validated against.
*/
private function __construct($model, $rules) {
$this->model = self::flatten($model);
$this->rules = $this->normalizeRules($rules);
$this->context = new ValidatorContext($this);
}
/**
* Flatten an array, appending all sub-array keys into the top-level name.
*
* For example, `$a['foo']['bar'] = 'baz'` becomes `$a['foo[bar]'] = 'baz'`.
*
* @param array $model
* @return array
*/
private static function flatten($model) {
$repeat = false;
foreach ($model as $name => $value) {
if (is_array($value)) {
$repeat = true;
foreach ($value as $sub_name => $sub_value) {
$model["{$name}[$sub_name]"] = $sub_value;
}
unset($model[$name]);
}
}
if ($repeat) {
$model = self::flatten($model);
}
return $model;
}
/**
* Validates the model, returning a reusable Validator object in the process.
*
* Example (using callbacks):
* <code>
* Validator::validate(array(
* 'model' => $_POST,
* 'rules' => array(...),
* 'validHandler' => function() {
* // Model validated.
* },
* 'invalidHandler' => function() {
* // Model failed validation.
* },
* ));
* </code>
*
* Example (using methods):
* <code>
* $validator = Validator::validate(array(
* 'model' => $_POST,
* 'rules' => array(...),
* ));
* if ($validator->model()) {
* // Model validated.
* } else {
* // Model failed validation.
* }
* </code>
*
* Possible options include:
*
* - <b>model</b> (required) The model to validate.
* - <b>rules</b> (required) The rules the model is validated against.
* - <b>validHandler</b> A callable that gets called if the model is valid.
* - <b>invalidHandler</b> A callable that gets called if the model fails validation.
*
* @param array $options An array of options.
* @return Validator An reusable Validator instance.
*/
public static function validate($options) {
$noop = function() {};
$valid_handler = $options['validHandler'] ?: $noop;
$invalid_handler = $options['invalidHandler'] ?: $noop;
$validator = new Validator($options['model'], $options['rules']);
if ($validator->model()) {
$valid_handler($validator);
} else {
$invalid_handler($validator);
}
return $validator;
}
/**
* Normalizes a rule array, removing all rules that are false.
*
* @param array $rules
* @return array
*/
private function normalizeRules($rules) {
foreach ($rules as $name => $rule) {
$normalized_rule = $this->normalizeRule($rule);
foreach ($normalized_rule as $method_name => $param) {
if ($param === false) {
unset($normalized_rule[$method_name]);
}
}
$rules[$name] = $normalized_rule;
}
return $rules;
}
/**
* Normalizes a rule, converting string rules into their array equivalents.
*
* @param array|string $value
* @return array
*/
private function normalizeRule($value) {
$normalized_value = $value;
if (is_string($value)) {
$normalized_value = array();
$method_names = preg_split('/\s/', $value);
foreach ($method_names as $method_name) {
$normalized_value[$method_name] = true;
}
}
return $normalized_value;
}
/**
* Validates a single field.
*
* This is the rough equivalent of the 'element' method in the jQuery Validation plugin.
*
* Example:
* <code>
* $validator = Validator::validate(array(...));
* if ($validator->field('first_name')) {
* // The 'first_name' field is valid.
* }
* </code>
*
* @param string $name The name of the field to validate.
* @return bool True if valid, false otherwise.
*/
public function field($name) {
$value = isset($this->model[$name]) ? $this->model[$name] : null;
$rule = $this->rules[$name];
$valid = true;
foreach ($rule as $method_name => $param) {
$method = self::$methods[$method_name];
$valid = $valid && (is_null($method) || $method($this->context, $value, $param));
if (!$valid) break;
}
return $valid;
}
/**
* Validates the model.
*
* This is the rough equivalent of the 'form' method in the jQuery Validation plugin.
*
* Example:
* <code>
* $validator = Validator::validate(array(...));
* if ($validator->model()) {
* // Model validated.
* }
* </code>
*
* @return bool True if the model is valid, false otherwise.
*/
public function model() {
$valid = true;
foreach ($this->rules as $name => $rule) {
$valid = $valid && $this->field($name);
if (!$valid) break;
}
return $valid;
}
/**
* Returns an associative array of all fields in the model that failed validation.
*
* Example:
* <code>
* $validator = Validator::validate(array(...));
* foreach ($validator->invalidFields() as $name => $value) {
* error_log($name . ' did not validate.');
* }
* </code>
*
* @return array An associative array of all invalid fields.
*/
public function invalidFields() {
$invalids = array();
foreach ($this->rules as $name => $rule) {
if (!$this->field($name)) {
$invalids[$name] = isset($this->model[$name]) ? $this->model[$name] : null;
}
}
return $invalids;
}
/**
* Returns the number of fields that failed validation.
*
* Example:
* <code>
* $validator = Validator::validate(array(...));
* error_log($validator->numberOfInvalidFields() . ' failed validation.');
* </code>
*
* @return int The number of fields that failed validation.
*/
public function numberOfInvalidFields() {
return count($this->invalidFields($this->model));
}
/**
* Returns the flattened model the Validator was constructed with.
*
* @return array The flattened model.
*/
public function getModel() {
return $this->model;
}
/**
* Returns the normalized rules the Validator was constructed with.
*
* @return array The normalized rules.
*/
public function getRules() {
return $this->rules;
}
/**
* Returns the array of validation methods.
*
* @return array The array containing all the validation methods.
*/
public static function getMethods() {
return self::$methods;
}
/**
* Adds a new validation method.
*
* Example:
* <code>
* Validator::addMethod('alphanumeric', function(ValidatorContext $context, $value) {
* return $context->optional($value) || ctype_alnum($value);
* });
* </code>
*
* @param string $method_name The name of the method, i.e. 'range' or 'creditcard'.
* @param callable $method The validation method, typically an anonymous function.
*/
public static function addMethod($method_name, $method) {
self::$methods[$method_name] = $method;
}
}
Validator::addMethod('required', function(ValidatorContext $context, $value, $param) {
$required = $context->resolve($value, $param);
return $required ? !$context->optional($value) : true;
});
Validator::addMethod('minlength', function(ValidatorContext $context, $value, $param) {
$length = is_array($value) ? count($value) : strlen($value);
return $context->optional($value) || $length >= $param;
});
Validator::addMethod('maxlength', function(ValidatorContext $context, $value, $param) {
$length = is_array($value) ? count($value) : strlen($value);
return $context->optional($value) || $length <= $param;
});
Validator::addMethod('rangelength', function(ValidatorContext $context, $value, $param) {
$length = is_array($value) ? count($value) : strlen($value);
return $context->optional($value) || $length >= $param[0] && $length <= $param[1];
});
Validator::addMethod('min', function(ValidatorContext $context, $value, $param) {
return $context->optional($value) || $value >= $param;
});
Validator::addMethod('max', function(ValidatorContext $context, $value, $param) {
return $context->optional($value) || $value <= $param;
});
Validator::addMethod('range', function(ValidatorContext $context, $value, $param) {
return $context->optional($value) || $value >= $param[0] && $value <= $param[1];
});
Validator::addMethod('email', function(ValidatorContext $context, $value) {
return $context->optional($value) || filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
});
Validator::addMethod('url', function(ValidatorContext $context, $value) {
if ($context->optional($value)) {
return true;
}
$has_permitted_protocol =
substr($value, 0, 4) === 'http' ||
substr($value, 0, 5) === 'https' ||
substr($value, 0, 3) === 'ftp';
return $has_permitted_protocol && filter_var($value, FILTER_VALIDATE_URL) !== false;
});
Validator::addMethod('date', function(ValidatorContext $context, $value) {
return $context->optional($value) || strtotime($value) !== false;
});
Validator::addMethod('dateISO', function(ValidatorContext $context, $value) {
return $context->optional($value) || preg_match('/^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/', $value);
});
Validator::addMethod('number', function(ValidatorContext $context, $value) {
return $context->optional($value) || is_numeric($value);
});
Validator::addMethod('digits', function(ValidatorContext $context, $value) {
return $context->optional($value) || preg_match('/^\d+$/', $value);
});
Validator::addMethod('creditcard', function(ValidatorContext $context, $value) {
if ($context->optional($value)) {
return true;
}
if (preg_match('/[^0-9 \-]+/', $value)) {
return false;
}
$value = preg_replace('/\/D/', '', $value);
$check = 0;
$even = false;
for ($n = strlen($value) - 1; $n >= 0; $n--) {
$digit = intval($value[$n]);
if ($even && ($digit *= 2) > 9) {
$digit -= 9;
}
$check += $digit;
$even = !$even;
}
return ($check % 10) === 0;
});
Validator::addMethod('equalTo', function(ValidatorContext $context, $value, $param) {
if ($context->optional($value)) {
return true;
}
$valid = false;
$parts = $context->parseSelector($param);
if ($parts !== null) {
$name = $parts['name'];
$model = $context->getValidator()->getModel();
$valid = $value === isset($model[$name]) ? $model[$name] : null;
}
return $valid;
});