Passed
Push — master ( 5c2907...37dc07 )
by Sergei
24:35 queued 21:56
created

Validator::normalizeRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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\Translator\TranslatorInterface;
15
use Yiisoft\Validator\DataSet\ArrayDataSet;
16
use Yiisoft\Validator\DataSet\ObjectDataSet;
17
use Yiisoft\Validator\DataSet\MixedDataSet;
18
use Yiisoft\Validator\Rule\Callback;
19
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
20
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
21
22
use function is_array;
23
use function is_callable;
24
use function is_int;
25
use function is_object;
26
27
/**
28
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
29
 */
30
final class Validator implements ValidatorInterface
31
{
32
    use PreValidateTrait;
33
34
    /**
35
     * @var callable
36
     */
37
    private $defaultSkipOnEmptyCallback;
38
39 660
    public function __construct(
40
        private RuleHandlerResolverInterface $ruleHandlerResolver,
41
        private TranslatorInterface $translator,
42
        /**
43
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
44
         * {@see validate()} method.
45
         */
46
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
47
        | ReflectionProperty::IS_PROTECTED
48
        | ReflectionProperty::IS_PUBLIC,
49
50
        /**
51
         * @var bool|callable|null
52
         */
53
        $defaultSkipOnEmpty = null,
54
    ) {
55 660
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
56
    }
57
58
    /**
59
     * @param DataSetInterface|mixed|RulesProviderInterface $data
60
     * @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...
61
     *
62
     * @throws ContainerExceptionInterface
63
     * @throws NotFoundExceptionInterface
64
     */
65 680
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
66
    {
67 680
        $data = $this->normalizeDataSet($data);
68 680
        if ($rules === null && $data instanceof RulesProviderInterface) {
69 26
            $rules = $data->getRules();
70 658
        } elseif ($rules instanceof RulesProviderInterface) {
71 2
            $rules = $rules->getRules();
72 656
        } 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 680
        $compoundResult = new Result();
77 680
        $context = new ValidationContext($this, $data);
78 680
        $results = [];
79
80 680
        foreach ($rules ?? [] as $attribute => $attributeRules) {
81 669
            $result = new Result();
82
83 669
            if (!is_iterable($attributeRules)) {
84 595
                $attributeRules = [$attributeRules];
85
            }
86
87 669
            $attributeRules = $this->normalizeRules($attributeRules);
88
89 669
            if (is_int($attribute)) {
90 580
                $validatedData = $data->getData();
91
            } else {
92 93
                $validatedData = $data->getAttributeValue($attribute);
93 93
                $context = $context->withAttribute($attribute);
94
            }
95
96 669
            $tempResult = $this->validateInternal($validatedData, $attributeRules, $context);
97
98 641
            foreach ($tempResult->getErrors() as $error) {
99 373
                $result->addError($error->getMessage(), $error->getParameters(), $error->getValuePath());
100
            }
101
102 641
            $results[] = $result;
103
        }
104
105 652
        foreach ($results as $result) {
106 641
            foreach ($result->getErrors() as $error) {
107 373
                $compoundResult->addError(
108 373
                    $this->translator->translate($error->getMessage(), $error->getParameters()),
109 373
                    $error->getParameters(),
110 373
                    $error->getValuePath()
111
                );
112
            }
113
        }
114
115 652
        if ($data instanceof PostValidationHookInterface) {
116
            $data->processValidationResult($compoundResult);
117
        }
118
119 652
        return $compoundResult;
120
    }
121
122 680
    #[Pure]
123
    private function normalizeDataSet($data): DataSetInterface
124
    {
125 680
        if ($data instanceof DataSetInterface) {
126 68
            return $data;
127
        }
128
129 617
        if (is_object($data)) {
130 37
            return new ObjectDataSet($data);
131
        }
132
133 584
        if (is_array($data)) {
134 104
            return new ArrayDataSet($data);
135
        }
136
137 503
        return new MixedDataSet($data);
138
    }
139
140
    /**
141
     * @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules
142
     */
143 669
    private function validateInternal($value, iterable $rules, ValidationContext $context): Result
144
    {
145 669
        $compoundResult = new Result();
146 669
        foreach ($rules as $rule) {
147 669
            if ($this->preValidate($value, $context, $rule)) {
148 27
                continue;
149
            }
150
151 663
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
152 661
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
153 635
            if ($ruleResult->isValid()) {
154 285
                continue;
155
            }
156
157 373
            $context->setParameter($this->parameterPreviousRulesErrored, true);
158
159 373
            foreach ($ruleResult->getErrors() as $error) {
160 373
                $valuePath = $error->getValuePath();
161 373
                if ($context->getAttribute() !== null) {
162 66
                    $valuePath = [$context->getAttribute(), ...$valuePath];
163
                }
164 373
                $compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePath);
165
            }
166
        }
167 641
        return $compoundResult;
168
    }
169
170
    /**
171
     * @param array $rules
172
     *
173
     * @return iterable<RuleInterface>
174
     */
175 669
    private function normalizeRules(iterable $rules): iterable
176
    {
177 669
        foreach ($rules as $rule) {
178 669
            yield $this->normalizeRule($rule);
179
        }
180
    }
181
182 669
    private function normalizeRule($rule): RuleInterface
183
    {
184 669
        if (is_callable($rule)) {
185 3
            return new Callback($rule);
186
        }
187
188 669
        if (!$rule instanceof RuleInterface) {
189
            throw new InvalidArgumentException(
190
                sprintf(
191
                    'Rule should be either an instance of %s or a callable, %s given.',
192
                    RuleInterface::class,
193
                    get_debug_type($rule)
194
                )
195
            );
196
        }
197
198 669
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
199 613
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
200
        }
201
202 669
        return $rule;
203
    }
204
}
205