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

NestedValidator::validate()   B

Complexity

Conditions 11
Paths 25

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
c 1
b 0
f 0
nc 25
nop 4
dl 0
loc 44
ccs 25
cts 25
cp 1
crap 11
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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