Total Complexity | 8 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Coverage | 84.21% |
Changes | 0 |
1 | <?php |
||
21 | class NotEmptyValidator implements ValidatorInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $errors; |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $validData; |
||
31 | |||
32 | /** |
||
33 | * Set validator options. |
||
34 | * This validator does not accept any option. |
||
35 | * |
||
36 | * @param array $options |
||
37 | * @throws RuntimeException |
||
38 | */ |
||
39 | public function setOptions(array $options) : void |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Validates data. Returns true when data is not empty. |
||
46 | * |
||
47 | * @param array $values |
||
48 | * @return bool |
||
49 | */ |
||
50 | 1 | public function validate(array $values) : bool |
|
51 | { |
||
52 | 1 | $isEmpty = true; |
|
53 | |||
54 | 1 | foreach ($values as $key => $data) { |
|
55 | 1 | if (is_string($data)) { |
|
56 | 1 | $data = trim($data); |
|
57 | } |
||
58 | |||
59 | 1 | if (!empty($data)) { |
|
60 | 1 | $isEmpty = false; |
|
61 | 1 | $this->validData[$key] = $data; |
|
62 | } |
||
63 | } |
||
64 | |||
65 | 1 | if ($isEmpty) { |
|
66 | 1 | $this->errors[] = 'This field is mandatory and cannot be empty'; |
|
67 | 1 | return false; |
|
68 | } |
||
69 | |||
70 | 1 | return true; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Retrieve valid data. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | 1 | public function getValidData() : array |
|
79 | { |
||
80 | 1 | return $this->validData; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Gets errors from validation. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 1 | public function getErrors() : array |
|
91 | } |
||
92 | } |
||
93 |