1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Validator\Result; |
9
|
|
|
use Yiisoft\Validator\ValidationContext; |
10
|
|
|
use Yiisoft\Validator\ValidatorInterface; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_object; |
13
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
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 NestedHandler implements RuleHandlerInterface |
41
|
|
|
{ |
42
|
12 |
|
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
43
|
|
|
{ |
44
|
12 |
|
if (!$rule instanceof Nested) { |
45
|
1 |
|
throw new UnexpectedRuleException(Nested::class, $rule); |
46
|
|
|
} |
47
|
|
|
|
48
|
11 |
|
$compoundResult = new Result(); |
49
|
11 |
|
if (!is_object($value) && !is_array($value)) { |
50
|
1 |
|
$message = sprintf('Value should be an array or an object. %s given.', gettype($value)); |
51
|
1 |
|
$compoundResult->addError($message); |
52
|
|
|
|
53
|
1 |
|
return $compoundResult; |
54
|
|
|
} |
55
|
|
|
|
56
|
10 |
|
$value = (array)$value; |
57
|
|
|
|
58
|
10 |
|
$results = []; |
59
|
10 |
|
foreach ($rule->rules as $valuePath => $rules) { |
60
|
10 |
|
$result = new Result((string)$valuePath); |
61
|
|
|
|
62
|
10 |
|
if ($rule->errorWhenPropertyPathIsNotFound && !ArrayHelper::pathExists($value, $valuePath)) { |
63
|
2 |
|
$compoundResult->addError($rule->propertyPathIsNotFoundMessage, ['path' => $valuePath], $valuePath); |
64
|
|
|
|
65
|
2 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
8 |
|
$rules = is_array($rules) ? $rules : [$rules]; |
69
|
8 |
|
$validatedValue = ArrayHelper::getValueByPath($value, $valuePath); |
70
|
|
|
|
71
|
8 |
|
$itemResult = $context->getValidator()->validate($validatedValue, $rules); |
|
|
|
|
72
|
|
|
|
73
|
8 |
|
if ($itemResult->isValid()) { |
74
|
3 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
foreach ($itemResult->getErrors() as $error) { |
78
|
5 |
|
$result->mergeError($error); |
79
|
|
|
} |
80
|
5 |
|
$results[] = $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
foreach ($results as $result) { |
84
|
5 |
|
foreach ($result->getErrors() as $error) { |
85
|
5 |
|
$compoundResult->mergeError($error); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
10 |
|
return $compoundResult; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.