Passed
Pull Request — master (#364)
by
unknown
02:47
created

Validator::normalizeDataSet()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 4
nop 1
crap 4
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\SingleValueDataSet;
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
use function is_string;
27
28
/**
29
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
30
 */
31
final class Validator implements ValidatorInterface
32
{
33
    use PreValidateTrait;
34
35
    /**
36
     * @var callable
37
     */
38
    private $defaultSkipOnEmptyCallback;
39
40 681
    public function __construct(
41
        private RuleHandlerResolverInterface $ruleHandlerResolver,
42
        private TranslatorInterface $translator,
43
        /**
44
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
45
         * {@see validate()} method.
46
         */
47
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
48
        | ReflectionProperty::IS_PROTECTED
49
        | ReflectionProperty::IS_PUBLIC,
50
51
        /**
52
         * @var bool|callable|null
53
         */
54
        bool|callable|null $defaultSkipOnEmpty = null,
55
    ) {
56 681
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
57
    }
58
59
    /**
60
     * @param DataSetInterface|mixed|RulesProviderInterface $data
61
     * @param class-string|iterable<Closure|Closure[]|RuleInterface|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[]>|RuleInterface|RulesProviderInterface|null.
Loading history...
62
     *
63
     * @throws ContainerExceptionInterface
64
     * @throws NotFoundExceptionInterface
65
     */
66 701
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
67
    {
68 701
        $data = $this->normalizeDataSet($data);
69 701
        if ($rules === null && $data instanceof RulesProviderInterface) {
70 27
            $rules = $data->getRules();
71 678
        } elseif ($rules instanceof RulesProviderInterface) {
72 2
            $rules = $rules->getRules();
73 676
        } elseif ($rules instanceof RuleInterface) {
74 1
            $rules = [$rules];
75 675
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
76
            $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

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