Passed
Pull Request — master (#351)
by
unknown
02:56
created

Validator   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 93.42%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 34
eloc 77
dl 0
loc 180
ccs 71
cts 76
cp 0.9342
rs 9.68
c 3
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A normalizeRule() 0 21 5
F validate() 0 62 16
A validateInternal() 0 25 6
A normalizeRules() 0 4 2
A normalizeDataSet() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use BadMethodCallException;
8
use Closure;
9
use InvalidArgumentException;
10
use JetBrains\PhpStorm\Pure;
11
use Psr\Container\ContainerExceptionInterface;
12
use Psr\Container\NotFoundExceptionInterface;
13
use ReflectionProperty;
14
use Traversable;
15
use Yiisoft\Translator\TranslatorInterface;
16
use Yiisoft\Validator\DataSet\ArrayDataSet;
17
use Yiisoft\Validator\DataSet\ObjectDataSet;
18
use Yiisoft\Validator\DataSet\SingleValueDataSet;
19
use Yiisoft\Validator\Rule\Callback;
20
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
21
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
22
23
use function is_array;
24
use function is_callable;
25
use function is_int;
26
use function is_object;
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 663
    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
        $defaultSkipOnEmpty = null,
55
    ) {
56 663
        $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 683
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
67
    {
68 683
        $data = $this->normalizeDataSet($data);
69 683
        if ($rules === null && $data instanceof RulesProviderInterface) {
70 27
            $rules = $data->getRules();
71 660
        } elseif ($rules instanceof RulesProviderInterface) {
72 2
            $rules = $rules->getRules();
73 658
        } elseif ($rules instanceof RuleInterface) {
74 1
            $rules = [$rules];
75 657
        } 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 683
        $compoundResult = new Result();
80 683
        $context = new ValidationContext($this, $data);
81 683
        $results = [];
82
83 683
        foreach ($rules ?? [] as $attribute => $attributeRules) {
84 672
            $result = new Result();
85
86 672
            if (!is_iterable($attributeRules)) {
87 596
                $attributeRules = [$attributeRules];
88
            }
89
90 672
            $attributeRules = $this->normalizeRules($attributeRules);
91
92 672
            if (is_int($attribute)) {
93 583
                $validatedData = $data->getData();
94
            } else {
95
                try {
96 95
                    $validatedData = $data->getAttributeValue($attribute);
97 2
                } catch (BadMethodCallException) {
98 2
                    throw new InvalidArgumentException('Properties can\'t be used in rules with single value data set');
99
                }
100
101 93
                $context = $context->withAttribute($attribute);
102
            }
103
104 672
            $tempResult = $this->validateInternal($validatedData, $attributeRules, $context);
105
106 644
            foreach ($tempResult->getErrors() as $error) {
107 375
                $result->addError($error->getMessage(), $error->getParameters(), $error->getValuePath());
108
            }
109
110 644
            $results[] = $result;
111
        }
112
113 655
        foreach ($results as $result) {
114 644
            foreach ($result->getErrors() as $error) {
115 375
                $compoundResult->addError(
116 375
                    $this->translator->translate($error->getMessage(), $error->getParameters()),
117 375
                    $error->getParameters(),
118 375
                    $error->getValuePath()
119
                );
120
            }
121
        }
122
123 655
        if ($data instanceof PostValidationHookInterface) {
124
            $data->processValidationResult($compoundResult);
125
        }
126
127 655
        return $compoundResult;
128
    }
129
130 683
    #[Pure]
131
    private function normalizeDataSet($data): DataSetInterface
132
    {
133 683
        if ($data instanceof DataSetInterface) {
134 68
            return $data;
135
        }
136
137 620
        if (is_object($data)) {
138 38
            return new ObjectDataSet($data);
139
        }
140
141 586
        if (is_array($data)) {
142 104
            return new ArrayDataSet($data);
143
        }
144
145 505
        return new SingleValueDataSet($data);
146
    }
147
148
    /**
149
     * @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules
150
     */
151 672
    private function validateInternal($value, iterable $rules, ValidationContext $context): Result
152
    {
153 672
        $compoundResult = new Result();
154 672
        foreach ($rules as $rule) {
155 672
            if ($this->preValidate($value, $context, $rule)) {
156 27
                continue;
157
            }
158
159 666
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
160 664
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
161 638
            if ($ruleResult->isValid()) {
162 286
                continue;
163
            }
164
165 375
            $context->setParameter($this->parameterPreviousRulesErrored, true);
166
167 375
            foreach ($ruleResult->getErrors() as $error) {
168 375
                $valuePath = $error->getValuePath();
169 375
                if ($context->getAttribute() !== null) {
170 67
                    $valuePath = [$context->getAttribute(), ...$valuePath];
171
                }
172 375
                $compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePath);
173
            }
174
        }
175 644
        return $compoundResult;
176
    }
177
178
    /**
179
     * @param array $rules
180
     *
181
     * @return iterable<RuleInterface>
182
     */
183 672
    private function normalizeRules(iterable $rules): iterable
184
    {
185 672
        foreach ($rules as $rule) {
186 672
            yield $this->normalizeRule($rule);
187
        }
188
    }
189
190 672
    private function normalizeRule($rule): RuleInterface
191
    {
192 672
        if (is_callable($rule)) {
193 3
            return new Callback($rule);
194
        }
195
196 672
        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 672
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
207 616
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
208
        }
209
210 672
        return $rule;
211
    }
212
}
213