Total Complexity | 8 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 72.72% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
17 | class Each extends Rule |
||
18 | { |
||
19 | use HasValidationErrorMessage; |
||
20 | |||
21 | private Rules $rules; |
||
22 | |||
23 | private string $incorrectInputMessage = 'Value should be array or iterable'; |
||
24 | private string $message = '{error} {value} given.'; |
||
25 | |||
26 | 5 | public function __construct(Rules $rules) |
|
27 | { |
||
28 | 5 | $this->rules = $rules; |
|
29 | 5 | } |
|
30 | |||
31 | 2 | protected function validateValue($value, DataSetInterface $dataSet = null): Result |
|
32 | { |
||
33 | 2 | $result = new Result(); |
|
34 | 2 | if (!is_iterable($value)) { |
|
35 | $result->addError($this->incorrectInputMessage); |
||
36 | return $result; |
||
37 | } |
||
38 | |||
39 | 2 | foreach ($value as $item) { |
|
40 | 2 | $itemResult = $this->rules->validate($item, $dataSet); |
|
41 | 2 | if ($itemResult->isValid() === false) { |
|
42 | 2 | foreach ($itemResult->getErrors() as $error) { |
|
43 | 2 | $result->addError( |
|
44 | 2 | $this->message, |
|
45 | 2 | ['error' => $error, 'value' => $item] |
|
46 | ); |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 | |||
51 | 2 | return $result; |
|
52 | } |
||
53 | |||
54 | public function incorrectInputMessage(string $message): self |
||
59 | } |
||
60 | |||
61 | 2 | public function getOptions(?TranslatorInterface $translator = null): array |
|
64 | } |
||
65 | } |
||
66 |