Passed
Pull Request — master (#279)
by Sergei
02:18
created

EachHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 32
c 1
b 0
f 0
dl 0
loc 60
ccs 25
cts 31
cp 0.8065
rs 10

2 Methods

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