Passed
Pull Request — master (#316)
by Dmitriy
02:46
created

Validator::addErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Closure;
8
use InvalidArgumentException;
9
use JetBrains\PhpStorm\Pure;
10
use Psr\Container\ContainerExceptionInterface;
11
use Psr\Container\NotFoundExceptionInterface;
12
use ReflectionProperty;
13
use Traversable;
14
use Yiisoft\Validator\DataSet\ArrayDataSet;
15
use Yiisoft\Validator\DataSet\ObjectDataSet;
16
use Yiisoft\Validator\DataSet\MixedDataSet;
17
use Yiisoft\Validator\Rule\Callback;
18
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
19
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
20
21
use function is_array;
22
use function is_callable;
23
use function is_int;
24
use function is_object;
25
26
/**
27
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
28
 */
29
final class Validator implements ValidatorInterface
30
{
31
    use PreValidateTrait;
32
33
    /**
34
     * @var callable
35
     */
36
    private $defaultSkipOnEmptyCallback;
37
38 643
    public function __construct(
39
        private RuleHandlerResolverInterface $ruleHandlerResolver,
40
        /**
41
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
42
         * {@see validate()} method.
43
         */
44
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
45
        | ReflectionProperty::IS_PROTECTED
46
        | ReflectionProperty::IS_PUBLIC,
47
48
        /**
49
         * @var bool|callable|null
50
         */
51
        $defaultSkipOnEmpty = null,
52
    ) {
53 643
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
54
    }
55
56
    /**
57
     * @param DataSetInterface|mixed|RulesProviderInterface $data
58
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null $rules
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|iterable<Cl...sProviderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null.
Loading history...
59
     *
60
     * @throws ContainerExceptionInterface
61
     * @throws NotFoundExceptionInterface
62
     */
63 114
    public function validate(
64
        mixed $data,
65
        iterable|object|string|null $rules = null
66
    ): Result {
67 114
        $data = $this->normalizeDataSet($data);
68 114
        if ($rules === null && $data instanceof RulesProviderInterface) {
69 25
            $rules = $data->getRules();
70 93
        } elseif ($rules instanceof RulesProviderInterface) {
71 2
            $rules = $rules->getRules();
72 91
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
73
            $rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules();
0 ignored issues
show
Bug introduced by
It seems like $rules can also be of type iterable; however, parameter $source of Yiisoft\Validator\RulesP...Provider::__construct() does only seem to accept object|string, maybe add an additional type check? ( Ignorable by Annotation )

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

73
            $rules = (new AttributesRulesProvider(/** @scrutinizer ignore-type */ $rules, $this->rulesPropertyVisibility))->getRules();
Loading history...
74
        }
75
76 114
        $compoundResult = new Result();
77 114
        $context = new ValidationContext($this, $data);
78 114
        $results = [];
79
80 114
        foreach ($rules ?? [] as $attribute => $attributeRules) {
81 104
            $result = new Result();
82
83 104
            $tempRule = is_iterable($attributeRules) ? $attributeRules : [$attributeRules];
84 104
            $attributeRules = $this->normalizeRules($tempRule);
85
86 104
            if (is_int($attribute)) {
87 41
                $validatedData = $data->getData();
88 41
                $validatedContext = $context;
89
            } else {
90 67
                $validatedData = $data->getAttributeValue($attribute);
91 67
                $validatedContext = $context->withAttribute($attribute);
92
            }
93
94 104
            $tempResult = $this->validateInternal(
95
                $validatedData,
96
                $attributeRules,
97
                $validatedContext
98
            );
99
100 99
            foreach ($tempResult->getErrors() as $error) {
101 77
                $result->addError($error->getMessage(), $error->getValuePath());
102
            }
103 99
            $results[] = $result;
104
        }
105
106 109
        foreach ($results as $result) {
107 99
            foreach ($result->getErrors() as $error) {
108 77
                $compoundResult->addError($error->getMessage(), $error->getValuePath());
109
            }
110
        }
111
112 109
        if ($data instanceof PostValidationHookInterface) {
113
            $data->processValidationResult($compoundResult);
114
        }
115
116 109
        return $compoundResult;
117
    }
118
119 114
    #[Pure]
120
    private function normalizeDataSet($data): DataSetInterface
121
    {
122 114
        if ($data instanceof DataSetInterface) {
123 47
            return $data;
124
        }
125
126 72
        if (is_object($data)) {
127 27
            return new ObjectDataSet($data);
128
        }
129
130 49
        if (is_array($data)) {
131 16
            return new ArrayDataSet($data);
132
        }
133
134 43
        return new MixedDataSet($data);
135
    }
136
137
    /**
138
     * @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules
139
     *
140
     * @throws ContainerExceptionInterface
141
     * @throws NotFoundExceptionInterface
142
     */
143 104
    private function validateInternal($value, iterable $rules, ValidationContext $context): Result
144
    {
145 104
        $compoundResult = new Result();
146 104
        foreach ($rules as $rule) {
147 104
            if ($rule instanceof BeforeValidationInterface) {
148 101
                $preValidateResult = $this->preValidate($value, $context, $rule);
149 101
                if ($preValidateResult) {
150 15
                    continue;
151
                }
152
            }
153
154 99
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
155 97
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
156 94
            if ($ruleResult->isValid()) {
157 37
                continue;
158
            }
159
160 77
            $context->setParameter($this->parameterPreviousRulesErrored, true);
161
162 77
            foreach ($ruleResult->getErrors() as $error) {
163 77
                $valuePath = $error->getValuePath();
164 77
                if ($context->getAttribute() !== null) {
165 53
                    $valuePath = [$context->getAttribute(), ...$valuePath];
166
                }
167 77
                $compoundResult->addError($error->getMessage(), $valuePath);
168
            }
169
        }
170 99
        return $compoundResult;
171
    }
172
173
    /**
174
     * @param array $rules
175
     *
176
     * @return iterable<RuleInterface>
177
     */
178 104
    private function normalizeRules(iterable $rules): iterable
179
    {
180 104
        foreach ($rules as $rule) {
181 104
            yield $this->normalizeRule($rule);
182
        }
183
    }
184
185 104
    private function normalizeRule($rule): RuleInterface
186
    {
187 104
        if (is_callable($rule)) {
188 3
            return new Callback($rule);
189
        }
190
191 104
        if (!$rule instanceof RuleInterface) {
192
            throw new InvalidArgumentException(
193
                sprintf(
194
                    'Rule should be either an instance of %s or a callable, %s given.',
195
                    RuleInterface::class,
196
                    get_debug_type($rule)
197
                )
198
            );
199
        }
200
201 104
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
202 99
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
203
        }
204
205 104
        return $rule;
206
    }
207
}
208