Total Complexity | 8 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Coverage | 100% |
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 | 1 | * @param array $options |
|
37 | * @throws RuntimeException |
||
38 | 1 | */ |
|
39 | public function setOptions(array $options) : void |
||
42 | 1 | } |
|
43 | |||
44 | /** |
||
45 | 1 | * Validates data. Returns true when data is not empty. |
|
46 | 1 | * |
|
47 | 1 | * @param array $values |
|
48 | * @return bool |
||
49 | */ |
||
50 | public function validate(array $values) : bool |
||
51 | 1 | { |
|
52 | 1 | $isEmpty = true; |
|
53 | 1 | ||
54 | foreach ($values as $key => $data) { |
||
55 | if (is_string($data)) { |
||
56 | 1 | $data = trim($data); |
|
57 | } |
||
58 | |||
59 | if (!empty($data)) { |
||
60 | $isEmpty = false; |
||
61 | $this->validData[$key] = $data; |
||
62 | } |
||
63 | } |
||
64 | 1 | ||
65 | if ($isEmpty) { |
||
66 | 1 | $this->errors[] = 'This field is mandatory and cannot be empty'; |
|
67 | return false; |
||
68 | } |
||
69 | |||
70 | return true; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | 1 | * Retrieve valid data. |
|
75 | * |
||
76 | 1 | * @return array |
|
77 | */ |
||
78 | public function getValidData() : array |
||
79 | { |
||
80 | return $this->validData; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Gets errors from validation. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getErrors() : array |
||
91 | } |
||
92 | } |
||
93 |