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

Validator   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 89.74%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 34
eloc 79
c 8
b 1
f 0
dl 0
loc 180
ccs 70
cts 78
cp 0.8974
rs 9.68

6 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeRule() 0 21 5
A normalizeRules() 0 4 2
A __construct() 0 13 1
F validate() 0 70 16
A validateInternal() 0 25 6
A normalizeDataSet() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use InvalidArgumentException;
8
use JetBrains\PhpStorm\Pure;
9
use ReflectionException;
10
use ReflectionProperty;
11
use Traversable;
12
use Yiisoft\Translator\TranslatorInterface;
13
use Yiisoft\Validator\DataSet\ArrayDataSet;
14
use Yiisoft\Validator\DataSet\ObjectDataSet;
15
use Yiisoft\Validator\DataSet\SingleValueDataSet;
16
use Yiisoft\Validator\Rule\Callback;
17
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
18
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
19
20
use function is_array;
21
use function is_callable;
22
use function is_int;
23
use function is_object;
24
use function is_string;
25
26
/**
27
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
28
 *
29
 * @psalm-import-type RulesType from ValidatorInterface
30
 */
31
final class Validator implements ValidatorInterface
32
{
33
    use PreValidateTrait;
34
35
    /**
36
     * @var callable
37
     */
38
    private $defaultSkipOnEmptyCallback;
39
40 695
    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
        bool|callable|null $defaultSkipOnEmpty = null,
51
    ) {
52 695
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
53
    }
54
55
    /**
56
     * @param DataSetInterface|mixed|RulesProviderInterface $data
57
     * @psalm-param RulesType $rules
58
     * @throws ReflectionException
59
     */
60 715
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
61
    {
62 715
        $data = $this->normalizeDataSet($data);
63 715
        if ($rules === null && $data instanceof RulesProviderInterface) {
64 27
            $rules = $data->getRules();
65 692
        } elseif ($rules instanceof RulesProviderInterface) {
66 2
            $rules = $rules->getRules();
67 690
        } elseif ($rules instanceof RuleInterface) {
68 1
            $rules = [$rules];
69 689
        } elseif (is_string($rules) || (is_object($rules) && !$rules instanceof Traversable)) {
70
            $rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules();
71
        }
72
73 715
        $compoundResult = new Result();
74 715
        $context = new ValidationContext($this, $data);
75 715
        $results = [];
76
77
        /**
78
         * @var mixed $attribute
79
         * @var mixed $attributeRules
80
         */
81 715
        foreach ($rules ?? [] as $attribute => $attributeRules) {
82 704
            $result = new Result();
83
84 704
            if (!is_iterable($attributeRules)) {
85 629
                $attributeRules = [$attributeRules];
86
            }
87
88 704
            $attributeRules = $this->normalizeRules($attributeRules);
89
90 704
            if (is_int($attribute)) {
91
                /** @psalm-suppress MixedAssignment */
92 613
                $validatedData = $data->getData();
93 95
            } elseif (is_string($attribute)) {
94
                /** @psalm-suppress MixedAssignment */
95 95
                $validatedData = $data->getAttributeValue($attribute);
96 95
                $context = $context->withAttribute($attribute);
97
            } else {
98
                $message = sprintf(
99
                    'An attribute can only have an integer or a string type. %s given.',
100
                    get_debug_type($attribute),
101
                );
102
103
                throw new InvalidArgumentException($message);
104
            }
105
106 704
            $tempResult = $this->validateInternal($validatedData, $attributeRules, $context);
107
108 679
            foreach ($tempResult->getErrors() as $error) {
109 395
                $result->addError($error->getMessage(), $error->getParameters(), $error->getValuePath());
110
            }
111
112 679
            $results[] = $result;
113
        }
114
115 690
        foreach ($results as $result) {
116 679
            foreach ($result->getErrors() as $error) {
117 395
                $compoundResult->addError(
118 395
                    $this->translator->translate($error->getMessage(), $error->getParameters()),
119 395
                    $error->getParameters(),
120 395
                    $error->getValuePath()
121
                );
122
            }
123
        }
124
125 690
        if ($data instanceof PostValidationHookInterface) {
126
            $data->processValidationResult($compoundResult);
127
        }
128
129 690
        return $compoundResult;
130
    }
131
132 715
    #[Pure]
133
    private function normalizeDataSet(mixed $data): DataSetInterface
134
    {
135 715
        if ($data instanceof DataSetInterface) {
136 71
            return $data;
137
        }
138
139 649
        if (is_object($data)) {
140 38
            return new ObjectDataSet($data);
141
        }
142
143 615
        if (is_array($data)) {
144 109
            return new ArrayDataSet($data);
145
        }
146
147 532
        return new SingleValueDataSet($data);
148
    }
149
150
    /**
151
     * @param iterable<RuleInterface> $rules
152
     */
153 704
    private function validateInternal(mixed $value, iterable $rules, ValidationContext $context): Result
154
    {
155 704
        $compoundResult = new Result();
156 704
        foreach ($rules as $rule) {
157 704
            if ($this->preValidate($value, $context, $rule)) {
158 27
                continue;
159
            }
160
161 698
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
162 696
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
163 673
            if ($ruleResult->isValid()) {
164 301
                continue;
165
            }
166
167 395
            $context->setParameter($this->parameterPreviousRulesErrored, true);
168
169 395
            foreach ($ruleResult->getErrors() as $error) {
170 395
                $valuePath = $error->getValuePath();
171 395
                if ($context->getAttribute() !== null) {
172 71
                    $valuePath = [$context->getAttribute(), ...$valuePath];
173
                }
174 395
                $compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePath);
175
            }
176
        }
177 679
        return $compoundResult;
178
    }
179
180
    /**
181
     * @return iterable<RuleInterface>
182
     */
183 704
    private function normalizeRules(iterable $rules): iterable
184
    {
185 704
        foreach ($rules as $rule) {
186 704
            yield $this->normalizeRule($rule);
187
        }
188
    }
189
190 704
    private function normalizeRule(mixed $rule): RuleInterface
191
    {
192 704
        if (is_callable($rule)) {
193 3
            return new Callback($rule);
194
        }
195
196 704
        if (!$rule instanceof RuleInterface) {
197
            throw new InvalidArgumentException(
198
                sprintf(
199
                    'Rule should be either an instance of %s or a callable, %s given.',
200
                    RuleInterface::class,
201
                    get_debug_type($rule)
202
                )
203
            );
204
        }
205
206 704
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
207 617
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
208
        }
209
210 704
        return $rule;
211
    }
212
}
213