-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathNestedHandler.php
More file actions
141 lines (118 loc) · 4.95 KB
/
Copy pathNestedHandler.php
File metadata and controls
141 lines (118 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Rule;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Strings\StringHelper;
use Yiisoft\Validator\DataSet\ObjectDataSet;
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\PostValidationHookInterface;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
use Yiisoft\Validator\RuleInterface;
use Yiisoft\Validator\ValidationContext;
use function array_slice;
use function is_array;
use function is_int;
use function is_object;
use function array_key_exists;
use function count;
/**
* A handler for {@see Nested} rule. Validates nested structures.
*/
final class NestedHandler implements RuleHandlerInterface
{
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof Nested) {
throw new UnexpectedRuleException(Nested::class, $rule);
}
if ($rule->getRules() === null) {
if (!is_object($value)) {
return (new Result())->addError($rule->getNoRulesWithNoObjectMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
}
$dataSet = new ObjectDataSet($value, $rule->getValidatedObjectPropertyVisibility());
return $context->validate($dataSet);
}
$value = $context->getParameter(ValidationContext::PARAMETER_VALUE_AS_ARRAY) ?? $value;
if (is_array($value)) {
$data = $value;
} elseif (is_object($value)) {
$data = (new ObjectDataSet($value, $rule->getValidatedObjectPropertyVisibility()))->getData();
if ($data === null) {
return (new Result())->addError($rule->getIncorrectDataSetTypeMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($data),
]);
}
} else {
return (new Result())->addError($rule->getIncorrectInputMessage(), [
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
'type' => get_debug_type($value),
]);
}
$compoundResult = new Result();
foreach ($rule->getRules() as $valuePath => $rules) {
if ($rule->isPropertyPathRequired() && !ArrayHelper::pathExists($data, $valuePath)) {
$valuePathList = is_int($valuePath)
? [$valuePath]
: StringHelper::parsePath($valuePath);
$compoundResult->addError(
$rule->getNoPropertyPathMessage(),
[
'path' => $valuePath,
'property' => $context->getTranslatedProperty(),
'Property' => $context->getCapitalizedTranslatedProperty(),
],
$valuePathList,
);
continue;
}
if (is_int($valuePath)) {
$validatedValue = ArrayHelper::getValueByPath($data, $valuePath);
$itemResult = $context->validate($validatedValue, $rules);
} else {
$valuePathList = StringHelper::parsePath($valuePath);
$property = end($valuePathList);
$scopeData = $data;
for ($i = 0, $limit = count($valuePathList) - 1; $i < $limit; $i++) {
$key = $valuePathList[$i];
if (!is_array($scopeData) || !array_key_exists($key, $scopeData)) {
$scopeData = [];
break;
}
$scopeData = $scopeData[$key];
}
if (!is_array($scopeData)) {
$scopeData = [];
}
$itemResult = $context->validate(
$scopeData,
[$property => $rules],
);
}
if ($itemResult->isValid()) {
continue;
}
foreach ($itemResult->getErrors() as $error) {
$valuePathList = is_int($valuePath)
? [$valuePath, ...$error->getValuePath()]
: [...StringHelper::parsePath($valuePath), ...array_slice($error->getValuePath(), 1)];
$compoundResult->addErrorWithoutPostProcessing(
$error->getMessage(),
$error->getParameters(),
$valuePathList,
);
}
}
if ($value instanceof PostValidationHookInterface) {
$value->processValidationResult($compoundResult);
}
return $compoundResult;
}
}