1 | <?php |
||
15 | class Validator implements ValidatorInterface |
||
16 | { |
||
17 | /** @var array */ |
||
18 | private $validators = []; |
||
19 | |||
20 | /** @var array */ |
||
21 | private $inputs = []; |
||
22 | |||
23 | /** @var array */ |
||
24 | private $values = []; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $errors = []; |
||
28 | |||
29 | /** |
||
30 | * Configure validator |
||
31 | */ |
||
32 | 28 | public function __construct() |
|
43 | |||
44 | /** |
||
45 | * Check if all validations are valid |
||
46 | * @return bool |
||
47 | */ |
||
48 | 18 | public function isValid() |
|
52 | |||
53 | /** |
||
54 | * Get all validation errors |
||
55 | * @return array |
||
56 | */ |
||
57 | 12 | public function getErrors() |
|
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | */ |
||
65 | 6 | public function getValues() |
|
66 | { |
||
67 | 6 | return $this->values; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Set validator |
||
72 | * @param string $name |
||
73 | * @param callable $callback |
||
74 | */ |
||
75 | 4 | public function setValidator($name, callable $callback) |
|
79 | |||
80 | /** |
||
81 | * Set input |
||
82 | * @param string $name |
||
83 | * @param InputInterface $input |
||
84 | */ |
||
85 | 20 | public function setInput($name, InputInterface $input) |
|
89 | |||
90 | /** |
||
91 | * Validate given validators and store errors |
||
92 | * @param array $validations |
||
93 | * @throws ValidatorException |
||
94 | */ |
||
95 | 20 | public function validate(array $validations) |
|
96 | { |
||
97 | 20 | $this->reset(); // For multiple use |
|
98 | |||
99 | 20 | foreach ($validations as $validation) { |
|
100 | 14 | if (!isset($this->inputs[$validation->getType()])) { |
|
101 | 2 | throw new ValidatorException('Type ' . $validation->getType() . ' not registered'); |
|
102 | } |
||
103 | |||
104 | 14 | $data = $this->inputs[$validation->getType()]->getData(); |
|
105 | 14 | $rules = $validation->getRules() !== null ? explode('|', $validation->getRules()) : []; |
|
106 | |||
107 | // Check if param is mandatory |
||
108 | 14 | if (in_array('required', $rules)) { |
|
109 | 10 | if (!isset($data[$validation->getKey()])) { |
|
110 | 2 | $this->errors[] = 'Validation for ' . $validation->getKey() . ' failed | required'; |
|
111 | 2 | continue; |
|
112 | } |
||
113 | 8 | unset($rules[array_search('required', $rules)]); |
|
114 | 4 | } |
|
115 | |||
116 | // Check if optional param is set |
||
117 | 14 | if (!isset($data[$validation->getKey()])) { |
|
118 | 2 | continue; |
|
119 | } |
||
120 | |||
121 | 14 | $value = $data[$validation->getKey()]; |
|
122 | 14 | if ($this->validateRules($validation, $value, $rules)) { |
|
123 | 11 | $this->values[$validation->getResultKey() ?: $validation->getKey()] = $value; |
|
124 | 4 | } |
|
125 | 10 | } |
|
126 | 18 | } |
|
127 | |||
128 | /** |
||
129 | * Validate all validation rules for given value |
||
130 | * @param Validation $validation |
||
131 | * @param mixed $value |
||
132 | * @param array $rules |
||
133 | * @return bool |
||
134 | */ |
||
135 | 14 | private function validateRules(Validation $validation, $value, array $rules) |
|
161 | |||
162 | /** |
||
163 | * Reset data |
||
164 | */ |
||
165 | 20 | private function reset() |
|
170 | } |
||
171 |