|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Valdi package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Valdi; |
|
13
|
|
|
|
|
14
|
|
|
use Valdi\Validator\Required; |
|
15
|
|
|
|
|
16
|
|
|
class Validator { |
|
17
|
|
|
|
|
18
|
|
|
protected $availableValidators; |
|
19
|
|
|
|
|
20
|
|
|
protected function setupValidators() { |
|
21
|
|
|
$this->availableValidators = array( |
|
22
|
|
|
'required' => new Required() |
|
23
|
|
|
); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function validateField($validators, $value) { |
|
27
|
|
|
$result = array(); |
|
28
|
|
|
foreach ($validators as $validator) { |
|
29
|
|
|
$name = $validator; |
|
30
|
|
|
$parameters = array(); |
|
31
|
|
|
if (is_array($validator)) { |
|
32
|
|
|
$parameters = $validator; |
|
33
|
|
|
$name = array_shift($parameters); |
|
34
|
|
|
} |
|
35
|
|
|
if (!array_key_exists($name, $this->availableValidators)) { |
|
36
|
|
|
throw new ValidatorException($name.' not found as available validator.'); |
|
37
|
|
|
} |
|
38
|
|
|
$result[$name] = $this->availableValidators[$name]->validate($value, $parameters); |
|
39
|
|
|
} |
|
40
|
|
|
return $result; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function gatherFailedFields($fields) { |
|
44
|
|
|
$failedFields = array(); |
|
45
|
|
|
foreach ($fields as $field => $result) { |
|
46
|
|
|
foreach ($result as $subResult) { |
|
47
|
|
|
if (!$subResult) { |
|
48
|
|
|
$failedFields[] = $field; |
|
49
|
|
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return $failedFields; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function __construct() { |
|
57
|
|
|
$this->setupValidators(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function addValidator(ValidatorInterface $validator) { |
|
61
|
|
|
$this->validators[$validator->getType()] = $validator; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function validate(array $validators, array $data) { |
|
65
|
|
|
$fields = array(); |
|
66
|
|
|
foreach ($validators as $field => $fieldValidators) { |
|
67
|
|
|
if (!array_key_exists($field, $data)) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
$value = isset($data[$field]) ? $data[$field] : null; |
|
71
|
|
|
$fields[$field] = $this->validateField($fieldValidators, $value); |
|
72
|
|
|
} |
|
73
|
|
|
$failed = $this->gatherFailedFields($fields); |
|
74
|
|
|
return array( |
|
75
|
|
|
'valid' => count($failed) === 0, |
|
76
|
|
|
'fields' => $fields, |
|
77
|
|
|
'failed' => $failed |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.