Passed
Pull Request — master (#222)
by Alexander
04:47 queued 02:23
created

NestedValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 46
ccs 25
cts 25
cp 1
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 44 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule\Nested;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule\RuleValidatorInterface;
10
use Yiisoft\Validator\ValidationContext;
11
use Yiisoft\Validator\ValidatorInterface;
12
use function is_array;
13
use function is_object;
14
15
/**
16
 * Can be used for validation of nested structures.
17
 *
18
 * For example, we have an inbound request with the following structure:
19
 *
20
 * ```php
21
 * $request = [
22
 *     'author' => [
23
 *         'name' => 'Dmitry',
24
 *         'age' => 18,
25
 *     ],
26
 * ];
27
 * ```
28
 *
29
 * So to make validation we can configure it like this:
30
 *
31
 * ```php
32
 * $rule = new Nested([
33
 *     'author' => new Nested([
34
 *         'name' => [new HasLength(min: 3)],
35
 *         'age' => [new Number(min: 18)],
36
 *     )];
37
 * ]);
38
 * ```
39
 */
40
final class NestedValidator implements RuleValidatorInterface
41
{
42 11
    public function validate(mixed $value, object $rule, ValidatorInterface $validator, ?ValidationContext $context = null): Result
43
    {
44 11
        $compoundResult = new Result();
45 11
        if (!is_object($value) && !is_array($value)) {
46 1
            $message = sprintf('Value should be an array or an object. %s given.', gettype($value));
47 1
            $compoundResult->addError($message);
48
49 1
            return $compoundResult;
50
        }
51
52 10
        $value = (array)$value;
53
54 10
        $results = [];
55 10
        foreach ($rule->rules as $valuePath => $rules) {
56 10
            $result = new Result((string)$valuePath);
57
58 10
            if ($rule->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) {
59 2
                $compoundResult->addError($rule->propertyPathIsNotFoundMessage, ['path' => $valuePath], $valuePath);
60
61 2
                continue;
62
            }
63
64 8
            $rules = is_array($rules) ? $rules : [$rules];
65 8
            $validatedValue = ArrayHelper::getValueByPath($value, $valuePath);
66
67 8
            $itemResult = $validator->validate($validatedValue, $rules);
68
69 8
            if ($itemResult->isValid()) {
70 3
                continue;
71
            }
72
73 5
            foreach ($itemResult->getErrors() as $error) {
74 5
                $result->merge($error);
75
            }
76 5
            $results[] = $result;
77
        }
78
79 10
        foreach ($results as $result) {
80 5
            foreach ($result->getErrors() as $error) {
81 5
                $compoundResult->merge($error);
82
            }
83
        }
84
85 10
        return $compoundResult;
86
    }
87
}
88