Passed
Pull Request — master (#199)
by Alexander
02:44
created

Each::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 7
dl 0
loc 14
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use InvalidArgumentException;
9
use Yiisoft\Validator\FormatterInterface;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule;
12
use Yiisoft\Validator\RuleSet;
13
use Yiisoft\Validator\ValidationContext;
14
15
/**
16
 * Validates an array by checking each of its elements against a set of rules.
17
 */
18
#[Attribute(Attribute::TARGET_PROPERTY)]
19
final class Each extends Rule
20
{
21
    private ?RuleSet $ruleSet = null;
22
23 7
    public function __construct(
24
        iterable $rules = [],
25
        private string $incorrectInputMessage = 'Value should be array or iterable.',
26
        private string $message = '{error} {value} given.',
27
        ?FormatterInterface $formatter = null,
28
        bool $skipOnEmpty = false,
29
        bool $skipOnError = false,
30
        $when = null,
31
    ) {
32 7
        if ($rules !== []) {
33 7
            $this->ruleSet = new RuleSet($rules);
34
        }
35
36 7
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
37
    }
38
39 3
    protected function validateValue($value, ?ValidationContext $context = null): Result
40
    {
41 3
        if ($this->ruleSet === null) {
42
            throw new InvalidArgumentException('Rules are required.');
43
        }
44
45 3
        $result = new Result();
46 3
        if (!is_iterable($value)) {
47
            $result->addError($this->incorrectInputMessage);
48
49
            return $result;
50
        }
51
52 3
        foreach ($value as $index => $item) {
53 3
            $itemResult = $this->ruleSet->validate($item, $context);
54 3
            if ($itemResult->isValid()) {
55 2
                continue;
56
            }
57
58 3
            foreach ($itemResult->getErrors() as $error) {
59 3
                if (!is_array($item)) {
60 3
                    $errorKey = [$index];
61 3
                    $formatMessage = true;
62
                } else {
63 1
                    $errorKey = [$index, ...$error->getValuePath()];
64 1
                    $formatMessage = false;
65
                }
66
67 3
                $message = !$formatMessage ? $error->getMessage() : $this->formatMessage($this->message, [
68 3
                    'error' => $error->getMessage(),
69
                    'value' => $item,
70
                ]);
71
72 3
                $result->addError($message, $errorKey);
73
            }
74
        }
75
76 3
        return $result;
77
    }
78
79 2
    public function getOptions(): array
80
    {
81 2
        return $this->ruleSet->asArray();
0 ignored issues
show
Bug introduced by
The method asArray() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        return $this->ruleSet->/** @scrutinizer ignore-call */ asArray();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
    }
83
}
84