Conditions | 7 |
Paths | 6 |
Total Lines | 29 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 7.3229 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
19 | 4 | public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result |
|
20 | { |
||
21 | 4 | if (!$rule instanceof Each) { |
|
22 | 1 | throw new UnexpectedRuleException(Each::class, $rule); |
|
23 | } |
||
24 | |||
25 | 3 | if ($rule->rules === null) { |
|
26 | throw new InvalidArgumentException('Rules are required.'); |
||
27 | } |
||
28 | |||
29 | 3 | $result = new Result(); |
|
30 | 3 | if (!is_iterable($value)) { |
|
31 | $result->addError($rule->incorrectInputMessage); |
||
32 | |||
33 | return $result; |
||
34 | } |
||
35 | |||
36 | 3 | foreach ($value as $index => $item) { |
|
37 | 3 | $itemResult = $validator->validate($item, [$index => $rule->rules]); |
|
38 | 3 | if ($itemResult->isValid()) { |
|
39 | 3 | continue; |
|
40 | } |
||
41 | |||
42 | 2 | foreach ($itemResult->getErrors() as $error) { |
|
43 | 2 | $result->merge($error); |
|
44 | } |
||
45 | } |
||
46 | |||
47 | 3 | return $result; |
|
48 | } |
||
50 |