|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
8
|
|
|
use Yiisoft\Strings\StringHelper; |
|
9
|
|
|
use Yiisoft\Validator\DataSet\ObjectDataSet; |
|
10
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
|
11
|
|
|
use Yiisoft\Validator\Result; |
|
12
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
|
13
|
|
|
use Yiisoft\Validator\RuleInterface; |
|
14
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
15
|
|
|
|
|
16
|
|
|
use function is_array; |
|
17
|
|
|
use function is_int; |
|
18
|
|
|
use function is_object; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A handler for {@see Nested} rule. Validates nested structures. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class NestedHandler implements RuleHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
26
|
|
|
{ |
|
27
|
|
|
if (!$rule instanceof Nested) { |
|
28
|
|
|
throw new UnexpectedRuleException(Nested::class, $rule); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** @var mixed $value */ |
|
32
|
|
|
$value = $context->getParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY) ?? $value; |
|
33
|
|
|
|
|
34
|
|
|
if ($rule->getRules() === null) { |
|
35
|
|
|
if (!is_object($value)) { |
|
36
|
|
|
return (new Result())->addError($rule->getNoRulesWithNoObjectMessage(), [ |
|
37
|
|
|
'attribute' => $context->getTranslatedAttribute(), |
|
38
|
|
|
'type' => get_debug_type($value), |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$dataSet = new ObjectDataSet($value, $rule->getValidatedObjectPropertyVisibility()); |
|
43
|
|
|
|
|
44
|
|
|
return $context->validate($dataSet); |
|
45
|
|
|
} |
|
46
|
53 |
|
|
|
47
|
|
|
if (is_array($value)) { |
|
48
|
53 |
|
$data = $value; |
|
49
|
1 |
|
} elseif (is_object($value)) { |
|
50
|
|
|
$data = (new ObjectDataSet($value, $rule->getValidatedObjectPropertyVisibility()))->getData(); |
|
51
|
|
|
if ($data === null) { |
|
52
|
52 |
|
return (new Result())->addError($rule->getIncorrectDataSetTypeMessage(), [ |
|
53
|
|
|
'type' => get_debug_type($data), |
|
54
|
52 |
|
]); |
|
55
|
10 |
|
} |
|
56
|
5 |
|
} else { |
|
57
|
5 |
|
return (new Result())->addError($rule->getIncorrectInputMessage(), [ |
|
58
|
5 |
|
'attribute' => $context->getTranslatedAttribute(), |
|
59
|
|
|
'type' => get_debug_type($value), |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
5 |
|
|
|
63
|
|
|
$compoundResult = new Result(); |
|
64
|
5 |
|
|
|
65
|
|
|
foreach ($rule->getRules() as $valuePath => $rules) { |
|
66
|
|
|
if ($rule->isPropertyPathRequired() && !ArrayHelper::pathExists($data, $valuePath)) { |
|
|
|
|
|
|
67
|
42 |
|
if (is_int($valuePath)) { |
|
68
|
33 |
|
$valuePathList = [$valuePath]; |
|
69
|
9 |
|
} else { |
|
70
|
|
|
/** @var list<string> $valuePathList */ |
|
71
|
4 |
|
$valuePathList = StringHelper::parsePath($valuePath); |
|
72
|
4 |
|
} |
|
73
|
3 |
|
|
|
74
|
4 |
|
$compoundResult->addError( |
|
75
|
|
|
$rule->getNoPropertyPathMessage(), |
|
76
|
|
|
[ |
|
77
|
|
|
'path' => $valuePath, |
|
78
|
5 |
|
'attribute' => $context->getTranslatedAttribute(), |
|
79
|
5 |
|
], |
|
80
|
5 |
|
$valuePathList, |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
continue; |
|
84
|
34 |
|
} |
|
85
|
|
|
|
|
86
|
34 |
|
/** @var mixed $validatedValue */ |
|
87
|
33 |
|
$validatedValue = ArrayHelper::getValueByPath($data, $valuePath); |
|
|
|
|
|
|
88
|
4 |
|
|
|
89
|
1 |
|
$itemResult = $context->validate($validatedValue, $rules); |
|
90
|
|
|
if ($itemResult->isValid()) { |
|
91
|
|
|
continue; |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
foreach ($itemResult->getErrors() as $error) { |
|
95
|
4 |
|
if (is_int($valuePath)) { |
|
96
|
4 |
|
$valuePathList = [$valuePath]; |
|
97
|
|
|
} else { |
|
98
|
|
|
/** @var list<string> $valuePathList */ |
|
99
|
4 |
|
$valuePathList = StringHelper::parsePath($valuePath); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!empty($valuePathList)) { |
|
103
|
|
|
array_push($valuePathList, ...$error->getValuePath()); |
|
|
|
|
|
|
104
|
4 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
$compoundResult->addError($error->getMessage(), $error->getParameters(), $valuePathList); |
|
107
|
|
|
} |
|
108
|
29 |
|
} |
|
109
|
29 |
|
|
|
110
|
|
|
return $compoundResult; |
|
111
|
29 |
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|