Passed
Pull Request — master (#320)
by Dmitriy
02:36
created

Validator::validate()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0033

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 25
c 5
b 1
f 0
dl 0
loc 44
ccs 21
cts 22
cp 0.9545
rs 8.8977
cc 6
nc 20
nop 3
crap 6.0033
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 Yiisoft\Validator\DataSet\ArrayDataSet;
13
use Yiisoft\Validator\DataSet\MixedDataSet;
14
use Yiisoft\Validator\DataSet\ObjectDataSet;
15
use Yiisoft\Validator\Rule\Callback;
16
use Yiisoft\Validator\Rule\Trait\PreValidateTrait;
17
18
use function is_callable;
19
use function is_int;
20
21
/**
22
 * Validator validates {@link DataSetInterface} against rules set for data set attributes.
23
 */
24
final class Validator implements ValidatorInterface
25
{
26
    use PreValidateTrait;
27
28
    /**
29
     * @var callable
30
     */
31
    private $defaultSkipOnEmptyCallback;
32
33 635
    public function __construct(
34
        private RuleHandlerResolverInterface $ruleHandlerResolver,
35
36
        /**
37
         * @var bool|callable|null
38
         */
39
        $defaultSkipOnEmpty = null,
40
    ) {
41 635
        $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty);
42
    }
43
44
    /**
45
     * @param DataSetInterface|mixed|RulesProviderInterface $data
46
     * @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...
47
     *
48
     * @throws ContainerExceptionInterface
49
     * @throws NotFoundExceptionInterface
50
     */
51 107
    public function validate(
52
        mixed $data,
53
        iterable|RulesProviderInterface|null $rules = null,
54
        ?ValidationContext $context = null,
55
    ): Result {
56 107
        $data = $this->normalizeDataSet($data);
57
58 107
        $compoundResult = new Result();
59 107
        $context = new ValidationContext($context?->getValidator() ?? $this, $context?->getDataSet() ?? $data, $context?->getAttribute() ?? null, $context?->getParameters() ?? []);
60 107
        $results = [];
61
62 107
        foreach ($rules ?? [] as $attribute => $attributeRules) {
63 97
            $result = new Result();
64
65 97
            $tempRule = is_iterable($attributeRules) ? $attributeRules : [$attributeRules];
66 97
            $attributeRules = $this->normalizeRules($tempRule);
67
68 97
            if (is_int($attribute)) {
69 37
                $validatedData = $data->getData();
70 37
                $validatedContext = $context;
71
            } else {
72 64
                $validatedData = $data->getAttributeValue($attribute);
73 64
                $validatedContext = $context->withAttribute($attribute);
74
            }
75
76 97
            $tempResult = $this->validateInternal(
77
                $validatedData,
78
                $attributeRules,
79
                $validatedContext
80
            );
81
82 95
            $result = $this->addErrors($result, $tempResult->getErrors());
83 95
            $results[] = $result;
84
        }
85
86 105
        foreach ($results as $result) {
87 95
            $compoundResult = $this->addErrors($compoundResult, $result->getErrors());
88
        }
89
90 105
        if ($data instanceof PostValidationHookInterface) {
91
            $data->processValidationResult($compoundResult);
92
        }
93
94 105
        return $compoundResult;
95
    }
96
97
    /**
98
     * @param $value
99
     * @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules
100
     * @param ValidationContext $context
101
     *
102
     * @throws ContainerExceptionInterface
103
     * @throws NotFoundExceptionInterface
104
     *
105
     * @return Result
106
     */
107 97
    private function validateInternal($value, iterable $rules, ValidationContext $context): Result
108
    {
109 97
        $compoundResult = new Result();
110 97
        foreach ($rules as $rule) {
111 97
            if ($rule instanceof BeforeValidationInterface) {
112 94
                $preValidateResult = $this->preValidate($value, $context, $rule);
113 94
                if ($preValidateResult) {
114 15
                    continue;
115
                }
116
            }
117
118 92
            $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName());
119 90
            $ruleResult = $ruleHandler->validate($value, $rule, $context);
120 90
            if ($ruleResult->isValid()) {
121 36
                continue;
122
            }
123
124 74
            $context->setParameter($this->parameterPreviousRulesErrored, true);
125
126 74
            foreach ($ruleResult->getErrors() as $error) {
127 74
                $valuePath = $error->getValuePath();
128 74
                if ($context->getAttribute() !== null) {
129 53
                    $valuePath = [$context->getAttribute(), ...$valuePath];
130
                }
131 74
                $compoundResult->addError($error->getMessage(), $valuePath, $error->getParameters());
132
            }
133
        }
134 95
        return $compoundResult;
135
    }
136
137
    /**
138
     * @param array $rules
139
     *
140
     * @return iterable<RuleInterface>
141
     */
142 97
    private function normalizeRules(iterable $rules): iterable
143
    {
144 97
        foreach ($rules as $rule) {
145 97
            yield $this->normalizeRule($rule);
146
        }
147
    }
148
149 97
    private function normalizeRule($rule): RuleInterface
150
    {
151 97
        if (is_callable($rule)) {
152 3
            return new Callback($rule);
153
        }
154
155 97
        if (!$rule instanceof RuleInterface) {
156
            throw new InvalidArgumentException(
157
                sprintf(
158
                    'Rule should be either an instance of %s or a callable, %s given.',
159
                    RuleInterface::class,
160
                    get_debug_type($rule)
161
                )
162
            );
163
        }
164
165 97
        if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) {
166 92
            $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback);
167
        }
168
169 97
        return $rule;
170
    }
171
172 107
    #[Pure]
173
    private function normalizeDataSet($data): DataSetInterface
174
    {
175 107
        if ($data instanceof DataSetInterface) {
176 107
            return $data;
177
        }
178
179
        if (is_object($data)) {
180
            return new ObjectDataSet($data);
181
        }
182
183
        if (is_array($data)) {
184
            return new ArrayDataSet($data);
185
        }
186
187
        return new MixedDataSet($data);
188
    }
189
190
    /**
191
     * @param Result $result
192
     * @param Error[] $errors
193
     *
194
     * @return Result
195
     */
196 95
    private function addErrors(Result $result, array $errors): Result
197
    {
198 95
        foreach ($errors as $error) {
199 74
            $result->addError($error->getMessage(), $error->getValuePath(), $error->getParameters());
200
        }
201 95
        return $result;
202
    }
203
}
204