1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Validation context that might be taken into account when performing validation. |
12
|
|
|
* |
13
|
|
|
* @psalm-import-type RulesType from ValidatorInterface |
14
|
|
|
*/ |
15
|
|
|
final class ValidationContext |
16
|
|
|
{ |
17
|
|
|
private ?ValidatorInterface $validator = null; |
18
|
|
|
|
19
|
827 |
|
/** |
20
|
|
|
* @var mixed The raw validated data. |
21
|
|
|
*/ |
22
|
|
|
private mixed $rawData = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var DataSetInterface|null Data set the attribute belongs to. Null if data set not set. |
26
|
|
|
*/ |
27
|
43 |
|
private ?DataSetInterface $dataSet = null; |
28
|
|
|
|
29
|
43 |
|
/** |
30
|
|
|
* @var string|null Validated attribute name. Null if a single value is validated. |
31
|
|
|
*/ |
32
|
|
|
private ?string $attribute = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
11 |
|
* @param array $parameters Arbitrary parameters. |
36
|
|
|
*/ |
37
|
11 |
|
public function __construct( |
38
|
|
|
private array $parameters = [] |
39
|
|
|
) { |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setValidatorAndRawDataOnce(ValidatorInterface $validator, mixed $rawData): self |
43
|
503 |
|
{ |
44
|
|
|
if ($this->validator !== null) { |
45
|
503 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->validator = $validator; |
49
|
|
|
$this->rawData = $rawData; |
50
|
|
|
|
51
|
140 |
|
return $this; |
52
|
|
|
} |
53
|
140 |
|
|
54
|
140 |
|
/** |
55
|
|
|
* Validate data in current context. |
56
|
|
|
* |
57
|
|
|
* @param mixed $data Data set to validate. If {@see RulesProviderInterface} instance provided and rules are |
58
|
|
|
* not specified explicitly, they are read from the {@see RulesProviderInterface::getRules()}. |
59
|
|
|
* @param callable|iterable|object|string|null $rules Rules to apply. If specified, rules are not read from data set |
60
|
3 |
|
* even if it is an instance of {@see RulesProviderInterface}. |
61
|
|
|
* |
62
|
3 |
|
* @psalm-param RulesType $rules |
63
|
|
|
*/ |
64
|
|
|
public function validate(mixed $data, callable|iterable|object|string|null $rules = null): Result |
65
|
|
|
{ |
66
|
|
|
$this->checkValidatorAndRawData(); |
67
|
|
|
|
68
|
|
|
$currentDataSet = $this->dataSet; |
69
|
|
|
$currentAttribute = $this->attribute; |
70
|
|
|
|
71
|
|
|
$result = $this->validator->validate($data, $rules, $this); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->dataSet = $currentDataSet; |
74
|
|
|
$this->attribute = $currentAttribute; |
75
|
6 |
|
|
76
|
|
|
return $result; |
77
|
6 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
503 |
|
* @return mixed The raw validated data. |
81
|
|
|
*/ |
82
|
503 |
|
public function getRawData(): mixed |
83
|
|
|
{ |
84
|
|
|
$this->checkValidatorAndRawData(); |
85
|
786 |
|
return $this->rawData; |
86
|
|
|
} |
87
|
786 |
|
|
88
|
|
|
/** |
89
|
|
|
* @return DataSetInterface Data set the attribute belongs to. |
90
|
|
|
*/ |
91
|
|
|
public function getDataSet(): DataSetInterface |
92
|
|
|
{ |
93
|
|
|
if ($this->dataSet === null) { |
94
|
|
|
throw new RuntimeException('Data set in validation context is not set.'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->dataSet; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param DataSetInterface $dataSet Data set the attribute belongs to. |
102
|
|
|
*/ |
103
|
|
|
public function setDataSet(DataSetInterface $dataSet): self |
104
|
|
|
{ |
105
|
|
|
$this->dataSet = $dataSet; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string|null Validated attribute name. Null if a single value is validated. |
111
|
|
|
*/ |
112
|
|
|
public function getAttribute(): ?string |
113
|
|
|
{ |
114
|
|
|
return $this->attribute; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string|null $attribute Validated attribute name. Null if a single value is validated. |
119
|
|
|
*/ |
120
|
|
|
public function setAttribute(?string $attribute): self |
121
|
|
|
{ |
122
|
|
|
$this->attribute = $attribute; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get named parameter. |
128
|
|
|
* |
129
|
|
|
* @param string $key Parameter name. |
130
|
|
|
* @param mixed $default Default value to return in case parameter with a given name does not exist. |
131
|
|
|
* |
132
|
|
|
* @return mixed Parameter value. |
133
|
|
|
* |
134
|
|
|
* @see ArrayHelper::getValue() |
135
|
|
|
*/ |
136
|
|
|
public function getParameter(string $key, mixed $default = null): mixed |
137
|
|
|
{ |
138
|
|
|
return ArrayHelper::getValue($this->parameters, $key, $default); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setParameter(string $key, mixed $value): self |
142
|
|
|
{ |
143
|
|
|
$this->parameters[$key] = $value; |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function isAttributeMissing(): bool |
148
|
|
|
{ |
149
|
|
|
return $this->attribute !== null && $this->dataSet !== null && !$this->dataSet->hasAttribute($this->attribute); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @psalm-assert ValidatorInterface $this->validator |
154
|
|
|
*/ |
155
|
|
|
private function checkValidatorAndRawData(): void |
156
|
|
|
{ |
157
|
|
|
if ($this->validator === null) { |
158
|
|
|
throw new RuntimeException('Validator and raw data in validation context is not set.'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.