| Conditions | 8 |
| Paths | 6 |
| Total Lines | 36 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 20 |
| CRAP Score | 8.0069 |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 20 | 10 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
| 21 | { |
||
| 22 | 10 | if (!$rule instanceof Each) { |
|
| 23 | 1 | throw new UnexpectedRuleException(Each::class, $rule); |
|
| 24 | } |
||
| 25 | |||
| 26 | 9 | $rules = $rule->getRules(); |
|
| 27 | 9 | if ($rules === []) { |
|
| 28 | throw new InvalidArgumentException('Rules are required.'); |
||
| 29 | } |
||
| 30 | |||
| 31 | 9 | $result = new Result(); |
|
| 32 | 9 | if (!is_iterable($value)) { |
|
| 33 | 1 | $result->addError($rule->getIncorrectInputMessage()); |
|
| 34 | |||
| 35 | 1 | return $result; |
|
| 36 | } |
||
| 37 | |||
| 38 | 8 | foreach ($value as $index => $item) { |
|
| 39 | /** @var array<mixed, Closure|Closure[]|RuleInterface|RuleInterface[]> $rule */ |
||
| 40 | 8 | $rule = [$index => $rules]; |
|
| 41 | 8 | $itemResult = $context->getValidator()->validate($item, $rule); |
|
| 42 | 8 | if ($itemResult->isValid()) { |
|
| 43 | 7 | continue; |
|
| 44 | } |
||
| 45 | |||
| 46 | 7 | foreach ($itemResult->getErrors() as $error) { |
|
| 47 | 7 | $result->addError( |
|
| 48 | 7 | $error->getMessage(), |
|
| 49 | 7 | $error->getParameters(), |
|
| 50 | 7 | $error->getValuePath() === [] ? [$index] : [$index, ...$error->getValuePath()], |
|
| 51 | ); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | 8 | return $result; |
|
| 56 | } |
||
| 58 |