Passed
Pull Request — master (#303)
by
unknown
02:26
created

EachHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 33
c 1
b 0
f 0
dl 0
loc 60
ccs 30
cts 31
cp 0.9677
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B validate() 0 51 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use InvalidArgumentException;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Formatter;
10
use Yiisoft\Validator\FormatterInterface;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\RuleHandlerInterface;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\ValidationContext;
15
16
/**
17
 * Validates an array by checking each of its elements against a set of rules.
18
 */
19
final class EachHandler implements RuleHandlerInterface
20
{
21
    private FormatterInterface $formatter;
22
23 12
    public function __construct(?FormatterInterface $formatter = null)
24
    {
25 12
        $this->formatter = $formatter ?? new Formatter();
26
    }
27
28 11
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
29
    {
30 11
        if (!$rule instanceof Each) {
31 1
            throw new UnexpectedRuleException(Each::class, $rule);
32
        }
33
34
        /** @var Each $eachRule */
35 10
        $eachRule = $rule;
36
37 10
        $rules = $rule->getRules();
38 10
        if ($rules === []) {
39
            throw new InvalidArgumentException('Rules are required.');
40
        }
41
42 10
        $result = new Result();
43 10
        if (!is_iterable($value)) {
44 1
            $formattedMessage = $this->formatter->format(
45 1
                $rule->getIncorrectInputMessage(),
46 1
                ['attribute' => $context->getAttribute(), 'value' => $value]
47
            );
48 1
            $result->addError($formattedMessage);
49
50 1
            return $result;
51
        }
52
53 9
        foreach ($value as $index => $item) {
54
            /** @var array<mixed, \Closure|\Closure[]|RuleInterface|RuleInterface[]> $rule */
55 9
            $rule = [$index => $rules];
56 9
            $itemResult = $context->getValidator()->validate($item, $rule);
57 9
            if ($itemResult->isValid()) {
58 8
                continue;
59
            }
60
61 8
            foreach ($itemResult->getErrors() as $error) {
62 8
                if ($error->getValuePath() === []) {
63 7
                    $errorKey = [$index];
64 7
                    $formatMessage = true;
65
                } else {
66 5
                    $errorKey = [$index, ...$error->getValuePath()];
67 5
                    $formatMessage = false;
68
                }
69
70 8
                $message = !$formatMessage ? $error->getMessage() : $this->formatter->format($eachRule->getMessage(), [
71 7
                    'error' => $error->getMessage(),
72
                    'value' => $item,
73
                ]);
74 8
                $result->addError($message, $errorKey);
75
            }
76
        }
77
78 9
        return $result;
79
    }
80
}
81