Passed
Pull Request — master (#320)
by Dmitriy
02:52
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\MixedDataSet;
16
use Yiisoft\Validator\DataSet\ObjectDataSet;
17
use Yiisoft\Validator\Rule\Callback;
18
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
19
20
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
21
22
use function is_callable;
23
use function is_int;
24
25
/**
26
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
27
 */
28
final class Validator implements ValidatorInterface
29
{
30
    use PreValidateTrait;
31
32
    /**
33
     * @var callable
34
     */
35
    private $defaultSkipOnEmptyCallback;
36
37 638
    public function __construct(
38
        private RuleHandlerResolverInterface $ruleHandlerResolver,
39
40
        /**
41
         * @var bool|callable|null
42
         */
43
        $defaultSkipOnEmpty = null,
44
        /**
45
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
46
         * {@see validate()} method.
47
         */
48
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
49
        | ReflectionProperty::IS_PROTECTED
50
        | ReflectionProperty::IS_PUBLIC,
51
    ) {
52 638
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
53
    }
54
55
    /**
56
     * @param DataSetInterface|mixed|RulesProviderInterface $data
57
     * @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...
58
     *
59
     * @throws ContainerExceptionInterface
60
     * @throws NotFoundExceptionInterface
61
     */
62 110
    public function validate(
63
        mixed $data,
64
        iterable|RulesProviderInterface|null $rules = null,
65
        ?ValidationContext $context = null,
66
    ): Result {
67 110
        $data = $this->normalizeDataSet($data);
68
69 110
        if ($rules === null && $data instanceof RulesProviderInterface) {
70 25
            $rules = $data->getRules();
71 94
        } elseif ($rules instanceof RulesProviderInterface) {
72 2
            $rules = $rules->getRules();
73 92
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
74
            $rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules();
0 ignored issues
show
Bug introduced by
$rules of type iterable is incompatible with the type object|string expected by parameter $source of Yiisoft\Validator\RulesP...Provider::__construct(). ( Ignorable by Annotation )

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

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