Passed
Pull Request — master (#322)
by
unknown
02:36
created

Validator   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 33
eloc 69
c 5
b 1
f 0
dl 0
loc 171
ccs 65
cts 70
cp 0.9286
rs 9.76

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
C validate() 0 42 12
A normalizeDataSet() 0 16 4
A addErrors() 0 6 2
A normalizeRule() 0 21 5
B validateInternal() 0 25 7
A normalizeRules() 0 4 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\ObjectDataSet;
16
use Yiisoft\Validator\DataSet\MixedDataSet;
17
use Yiisoft\Validator\Rule\Callback;
18
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
19
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider;
20
21
use function is_array;
22
use function is_callable;
23
use function is_int;
24
use function is_object;
25
26
/**
27
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
28
 */
29
final class Validator implements ValidatorInterface
30
{
31
    use PreValidateTrait;
32
33
    /**
34
     * @var callable
35
     */
36
    private $defaultSkipOnEmptyCallback;
37
38 654
    public function __construct(
39
        private RuleHandlerResolverInterface $ruleHandlerResolver,
40
        /**
41
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
42
         * {@see validate()} method.
43
         */
44
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
45
        | ReflectionProperty::IS_PROTECTED
46
        | ReflectionProperty::IS_PUBLIC,
47
48
        /**
49
         * @var bool|callable|null
50
         */
51
        $defaultSkipOnEmpty = null,
52
    ) {
53 654
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
54
    }
55
56
    /**
57
     * @param DataSetInterface|mixed|RulesProviderInterface $data
58
     * @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...
59
     *
60
     * @throws ContainerExceptionInterface
61
     * @throws NotFoundExceptionInterface
62
     */
63 126
    public function validate(mixed $data, iterable|object|string|null $rules = null): Result
64
    {
65 126
        $data = $this->normalizeDataSet($data);
66 126
        if ($rules === null && $data instanceof RulesProviderInterface) {
67 25
            $rules = $data->getRules();
68 105
        } elseif ($rules instanceof RulesProviderInterface) {
69 2
            $rules = $rules->getRules();
70 103
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
71
            $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

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