Passed
Pull Request — master (#303)
by
unknown
02:26
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 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
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\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 gettype;
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 636
    public function __construct(
40
        private RuleHandlerResolverInterface $ruleHandlerResolver,
41
42
        /**
43
         * @var bool|callable|null
44
         */
45
        $defaultSkipOnEmpty = null,
46
    ) {
47 636
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
48
    }
49
50
    /**
51
     * @param DataSetInterface|mixed|RulesProviderInterface $data
52
     * @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...
53
     * @param int $rulesPropertyVisibility What visibility levels to use when reading rules from the class specified in
54
     * `$rules` argument.
55
     *
56
     * @throws ContainerExceptionInterface
57
     * @throws NotFoundExceptionInterface
58
     */
59 109
    public function validate(
60
        mixed $data,
61
        iterable|object|string|null $rules = null,
62
        int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
63
        | ReflectionProperty::IS_PROTECTED
64
        | ReflectionProperty::IS_PUBLIC
65
    ): Result {
66 109
        $data = $this->normalizeDataSet($data);
67 109
        if ($rules === null && $data instanceof RulesProviderInterface) {
68 25
            $rules = $data->getRules();
69 88
        } elseif ($rules instanceof RulesProviderInterface) {
70 2
            $rules = $rules->getRules();
71 86
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
72
            $rules = (new AttributesRulesProvider($rules, $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

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