Passed
Pull Request — master (#192)
by Wilmer
13:59 queued 11:28
created

Validator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Validator\DataSet\ArrayDataSet;
8
use Yiisoft\Validator\DataSet\ScalarDataSet;
9
use Yiisoft\Validator\Rule\Nested;
10
11
use function is_array;
12
use function is_object;
13
14
/**
15
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
16
 */
17
final class Validator implements ValidatorInterface
18
{
19
    private ?FormatterInterface $formatter;
20
21 12
    public function __construct(?FormatterInterface $formatter = null)
22
    {
23 12
        $this->formatter = $formatter;
24
    }
25
26
    /**
27
     * @param DataSetInterface|mixed|RulesProviderInterface $data
28
     * @param Rule[][] $rules
29
     * @psalm-param iterable<string, Rule[]> $rules
30
     */
31 12
    public function validate($data, iterable $rules = []): Result
32
    {
33 12
        $data = $this->normalizeDataSet($data);
34
35 12
        if ($data instanceof RulesProviderInterface) {
36 2
            $rules = $data->getRules();
37
        }
38
39 12
        $context = new ValidationContext($data);
40 12
        $result = new Result();
41
42
        /** @psalm-var array<string, array<array-key, Rule>> $rules */
43 12
        foreach ($rules as $attribute => $attributeRules) {
44 12
            $ruleSet = new RuleSet($attributeRules);
45
46 12
            if ($this->formatter !== null) {
47
                $ruleSet = $ruleSet->withFormatter($this->formatter);
48
            }
49
50 12
            if ($attributeRules[0] instanceof Nested) {
51
                $nesteds = $attributeRules[0]->getOptions();
52
53
                foreach ($nesteds as $nested => $options) {
54
                    $values[$nested] = $data->getAttributeValue($attribute . '.' . $nested);
55
                }
56
57
                $tempResult = $ruleSet->validate(
58
                    $values,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $values does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
                    $context->withAttribute($attribute . '.' . $nested)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nested does not seem to be defined for all execution paths leading up to this point.
Loading history...
60
                );
61
62
                foreach ($tempResult->getErrors() as $error) {
63
                    $result->addError(
64
                        $error->getMessage(),
65
                        [$attribute . '.' . $nested, ...$error->getValuePath()],
66
                    );
67
                }
68
            } else {
69 12
                $tempResult = $ruleSet->validate(
70 12
                    $data->getAttributeValue($attribute),
71 12
                    $context->withAttribute($attribute)
72
                );
73
74 12
                foreach ($tempResult->getErrors() as $error) {
75 3
                    $result->addError($error->getMessage(), [$attribute, ...$error->getValuePath()]);
76
                }
77
            }
78
        }
79
80 12
        if ($data instanceof PostValidationHookInterface) {
81
            $data->processValidationResult($result);
82
        }
83
84 12
        return $result;
85
    }
86
87
    public function withFormatter(?FormatterInterface $formatter): self
88
    {
89
        $new = clone $this;
90
        $new->formatter = $formatter;
91
        return $new;
92
    }
93
94 12
    private function normalizeDataSet($data): DataSetInterface
95
    {
96 12
        if ($data instanceof DataSetInterface) {
97 5
            return $data;
98
        }
99
100 7
        if (is_object($data) || is_array($data)) {
101 1
            return new ArrayDataSet((array)$data);
102
        }
103
104 6
        return new ScalarDataSet($data);
105
    }
106
}
107