Total Complexity | 7 |
Total Lines | 67 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | abstract class Validation |
||
11 | { |
||
12 | /** |
||
13 | * Holds messages generated during validation. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | private $messages = []; |
||
18 | |||
19 | /** |
||
20 | * @param $field |
||
21 | * @param $data |
||
22 | * @return mixed |
||
23 | */ |
||
24 | abstract public function run($field, $data); |
||
25 | |||
26 | /** |
||
27 | * Utility function that evaluates the result of a validation test and prepares validation messages. |
||
28 | * Receives a boolean from the validation function and a message to be displayed when the value is false. It also |
||
29 | * receives the options array so it could override the standard message that the validation code generates. |
||
30 | * |
||
31 | * @param array $field |
||
32 | * @param boolean $result |
||
33 | * @param string $message |
||
34 | * @return boolean |
||
35 | */ |
||
36 | 14 | protected function evaluateResult($field, $result, $message) |
|
37 | { |
||
38 | 14 | $this->messages = []; |
|
39 | 14 | if ($result) { |
|
40 | 10 | return true; |
|
41 | } else { |
||
42 | 14 | if (is_array($field['name'])) { |
|
43 | 2 | foreach ($field['name'] as $name) { |
|
44 | 2 | $this->messages[$name][] = $field['options']['message'] ?? $message; |
|
45 | } |
||
46 | } else { |
||
47 | 12 | $this->messages[$field['name']][] = $field['options']['message'] ?? $message; |
|
48 | } |
||
49 | 14 | return false; |
|
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Extracts the value of the field description from the data passed. |
||
55 | * |
||
56 | * @param array $field |
||
57 | * @param array $data |
||
58 | * @return mixed |
||
59 | */ |
||
60 | 12 | protected function getFieldValue($field, $data) |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the validation messages generated. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 14 | public function getMessages() |
|
79 |