Test Failed
Pull Request — master (#316)
by Dmitriy
02:25
created

Validator::addErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 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 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
         * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in
43
         * {@see validate()} method.
44
         */
45
        private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE
46
        | ReflectionProperty::IS_PROTECTED
47
        | ReflectionProperty::IS_PUBLIC,
48
49
        /**
50
         * @var bool|callable|null
51
         */
52
        $defaultSkipOnEmpty = null,
53
    ) {
54 636
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
55
    }
56
57
    /**
58
     * @param DataSetInterface|mixed|RulesProviderInterface $data
59
     * @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...
60
     *
61
     * @throws ContainerExceptionInterface
62
     * @throws NotFoundExceptionInterface
63
     */
64 109
    public function validate(
65
        mixed $data,
66
        iterable|object|string|null $rules = null
67
    ): Result {
68 109
        $data = $this->normalizeDataSet($data);
69 109
        if ($rules === null && $data instanceof RulesProviderInterface) {
70 25
            $rules = $data->getRules();
71 88
        } elseif ($rules instanceof RulesProviderInterface) {
72 2
            $rules = $rules->getRules();
73 86
        } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) {
74
            $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

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