1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use Stringable; |
9
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
12
|
|
|
use Yiisoft\Validator\ValidationContext; |
13
|
|
|
|
14
|
|
|
use function gettype; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Compares the specified value with "target" value provided directly or within an attribute. |
18
|
|
|
* |
19
|
|
|
* @see AbstractCompare |
20
|
|
|
* @see Equal |
21
|
|
|
* @see GreaterThan |
22
|
|
|
* @see GreaterThanOrEqual |
23
|
|
|
* @see LessThan |
24
|
|
|
* @see LessThanOrEqual |
25
|
|
|
* @see Compare |
26
|
|
|
* @see NotEqual |
27
|
84 |
|
*/ |
28
|
|
|
final class CompareHandler implements RuleHandlerInterface |
29
|
84 |
|
{ |
30
|
1 |
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
31
|
|
|
{ |
32
|
|
|
if (!$rule instanceof AbstractCompare) { |
33
|
83 |
|
throw new UnexpectedRuleException(AbstractCompare::class, $rule); |
34
|
83 |
|
} |
35
|
4 |
|
|
36
|
4 |
|
$result = new Result(); |
37
|
4 |
|
if (!$this->isInputCorrect($rule->getType(), $value)) { |
38
|
|
|
return $result->addError($rule->getIncorrectInputMessage(), [ |
39
|
|
|
'attribute' => $context->getTranslatedAttribute(), |
40
|
|
|
'type' => get_debug_type($value), |
41
|
79 |
|
]); |
42
|
79 |
|
} |
43
|
|
|
|
44
|
79 |
|
/** @var mixed $targetValue */ |
45
|
|
|
$targetValue = $rule->getTargetValue(); |
46
|
8 |
|
$targetAttribute = $rule->getTargetAttribute(); |
47
|
8 |
|
|
48
|
3 |
|
if ($targetValue === null && $targetAttribute !== null) { |
49
|
3 |
|
/** @var mixed $targetValue */ |
50
|
|
|
$targetValue = $context->getDataSet()->getAttributeValue($targetAttribute); |
51
|
|
|
if (!$this->isInputCorrect($rule->getType(), $targetValue)) { |
52
|
|
|
return $result->addError($rule->getIncorrectDataSetTypeMessage(), [ |
53
|
|
|
'type' => get_debug_type($targetValue), |
54
|
76 |
|
]); |
55
|
34 |
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
42 |
|
if ($this->compareValues($rule->getType(), $rule->getOperator(), $value, $targetValue)) { |
59
|
42 |
|
return new Result(); |
60
|
42 |
|
} |
61
|
42 |
|
|
62
|
|
|
return (new Result())->addError($rule->getMessage(), [ |
63
|
|
|
'attribute' => $context->getTranslatedAttribute(), |
64
|
|
|
'targetValue' => $this->getFormattedValue($rule->getTargetValue()), |
65
|
|
|
'targetAttribute' => $targetAttribute, |
66
|
|
|
'targetAttributeValue' => $targetAttribute !== null ? $this->getFormattedValue($targetValue) : null, |
67
|
|
|
'targetValueOrAttribute' => $targetAttribute ?? $this->getFormattedValue($targetValue), |
68
|
|
|
'value' => $this->getFormattedValue($value), |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Checks whether the validated value has correct type depending on selected {@see AbstractCompare::$type}. |
74
|
|
|
* |
75
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
76
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
77
|
76 |
|
* |
78
|
|
|
* @param mixed $value The validated value. |
79
|
76 |
|
* |
80
|
2 |
|
* @return bool `true` if value is correct and `false` otherwise. |
81
|
2 |
|
*/ |
82
|
|
|
private function isInputCorrect(string $type, mixed $value): bool |
83
|
74 |
|
{ |
84
|
74 |
|
return $type !== CompareType::ORIGINAL ? $this->isValueAllowedForTypeCasting($value) : true; |
85
|
|
|
} |
86
|
|
|
|
87
|
76 |
|
/** |
88
|
17 |
|
* Checks whether the validated value is allowed for types that require type casting - {@see CompareType::NUMBER} |
89
|
10 |
|
* and {@see CompareType::STRING}. |
90
|
8 |
|
* |
91
|
6 |
|
* @param mixed $value The Validated value. |
92
|
8 |
|
* |
93
|
8 |
|
* @return bool `true` if value is allowed and `false` otherwise. |
94
|
8 |
|
*/ |
95
|
76 |
|
private function isValueAllowedForTypeCasting(mixed $value): bool |
96
|
|
|
{ |
97
|
|
|
return $value === null || |
98
|
|
|
is_scalar($value) || |
99
|
|
|
$value instanceof Stringable || |
100
|
|
|
$value instanceof DateTimeInterface; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets representation of the value for using with error parameter. |
105
|
|
|
* |
106
|
|
|
* @param mixed $value The validated value. |
107
|
|
|
* |
108
|
|
|
* @return scalar|null Formatted value. |
109
|
|
|
*/ |
110
|
|
|
private function getFormattedValue(mixed $value): int|float|string|bool|null |
111
|
|
|
{ |
112
|
|
|
if ($value === null || is_scalar($value)) { |
113
|
|
|
return $value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($value instanceof Stringable) { |
117
|
|
|
return (string) $value; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($value instanceof DateTimeInterface) { |
121
|
|
|
return $value->format('U'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return get_debug_type($value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Compares two values according to the specified type and operator. |
129
|
|
|
* |
130
|
|
|
* @param string $operator The comparison operator. One of `==`, `===`, `!=`, `!==`, `>`, `>=`, `<`, `<=`. |
131
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
132
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
133
|
|
|
* |
134
|
|
|
* @param mixed $value The validated value. |
135
|
|
|
* @param mixed $targetValue "Target" value set in rule options. |
136
|
|
|
* |
137
|
|
|
* @return bool Whether the result of comparison using the specified operator is true. |
138
|
|
|
*/ |
139
|
|
|
private function compareValues(string $type, string $operator, mixed $value, mixed $targetValue): bool |
140
|
|
|
{ |
141
|
|
|
if ($operator === '>' || $operator === '<') { |
142
|
|
|
/** @var mixed $value */ |
143
|
|
|
$value = $this->normalizeValue($type, $value); |
144
|
|
|
/** @var mixed $targetValue */ |
145
|
|
|
$targetValue = $this->normalizeValue($type, $targetValue); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return match ($operator) { |
149
|
|
|
'==' => $this->checkValuesAreEqual($type, $value, $targetValue), |
150
|
|
|
'===' => $this->checkValuesAreEqual($type, $value, $targetValue, strict: true), |
151
|
|
|
'!=' => !$this->checkValuesAreEqual($type, $value, $targetValue), |
152
|
|
|
'!==' => !$this->checkValuesAreEqual($type, $value, $targetValue, strict: true), |
153
|
|
|
'>' => $value > $targetValue, |
154
|
|
|
'>=' => $this->checkValuesAreEqual($type, $value, $targetValue) || |
155
|
|
|
$this->normalizeValue($type, $value) > $this->normalizeValue($type, $targetValue), |
156
|
|
|
'<' => $value < $targetValue, |
157
|
|
|
'<=' => $this->checkValuesAreEqual($type, $value, $targetValue) || |
158
|
|
|
$this->normalizeValue($type, $value) < $this->normalizeValue($type, $targetValue), |
159
|
|
|
}; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Checks whether a validated value equals to "target" value. For types other than {@see CompareType::ORIGINAL}, |
164
|
|
|
* handles strict comparison before type casting and takes edge cases for float numbers into account. |
165
|
|
|
* |
166
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
167
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
168
|
|
|
* |
169
|
|
|
* @param mixed $value The validated value. |
170
|
|
|
* @param mixed $targetValue "Target" value set in rule options. |
171
|
|
|
* @param bool $strict Whether the values must be equal (when set to `false`, default) / strictly equal (when set to |
172
|
|
|
* `true`). |
173
|
|
|
* |
174
|
|
|
* @return bool `true` if values are equal and `false` otherwise. |
175
|
|
|
*/ |
176
|
|
|
private function checkValuesAreEqual(string $type, mixed $value, mixed $targetValue, bool $strict = false): bool |
177
|
|
|
{ |
178
|
|
|
if ($type === CompareType::ORIGINAL) { |
179
|
|
|
return $strict ? $value === $targetValue : $value == $targetValue; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($strict && gettype($value) !== gettype($targetValue)) { |
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return match ($type) { |
187
|
|
|
CompareType::STRING => $this->normalizeString($value) === $this->normalizeString($targetValue), |
188
|
|
|
CompareType::NUMBER => $this->checkFloatsAreEqual( |
189
|
|
|
$this->normalizeNumber($value), |
190
|
|
|
$this->normalizeNumber($targetValue), |
191
|
|
|
), |
192
|
|
|
}; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Checks whether a validated float number equals to "target" float number. Handles a known problem of losing |
197
|
|
|
* precision during arithmetical operations. |
198
|
|
|
* |
199
|
|
|
* @param float $value The validated number. |
200
|
|
|
* @param float $targetValue "Target" number set in rule options. |
201
|
|
|
* |
202
|
|
|
* @return bool `true` if numbers are equal and `false` otherwise. |
203
|
|
|
* |
204
|
|
|
* @link https://floating-point-gui.de/ |
205
|
|
|
*/ |
206
|
|
|
private function checkFloatsAreEqual(float $value, float $targetValue): bool |
207
|
|
|
{ |
208
|
|
|
return abs($value - $targetValue) < PHP_FLOAT_EPSILON; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Normalizes compared value depending on selected {@see AbstractCompare::$type}. |
213
|
|
|
* |
214
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
215
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
216
|
|
|
* |
217
|
|
|
* @param mixed $value One of the compared values. Both validated and target value can be used. |
218
|
|
|
* |
219
|
|
|
* @return mixed Normalized value ready for comparison. |
220
|
|
|
*/ |
221
|
|
|
private function normalizeValue(string $type, mixed $value): mixed |
222
|
|
|
{ |
223
|
|
|
return match ($type) { |
224
|
|
|
CompareType::ORIGINAL => $value, |
225
|
|
|
CompareType::STRING => $this->normalizeString($value), |
226
|
|
|
CompareType::NUMBER => $this->normalizeNumber($value), |
227
|
|
|
}; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Normalizes number that might be stored in a different type to float number. |
232
|
|
|
* |
233
|
|
|
* @param mixed $number Raw number. Can be within an object implementing {@see Stringable} / |
234
|
|
|
* {@see DateTimeInterface} or other primitive type, such as `int`, `float`, `string`. |
235
|
|
|
* |
236
|
|
|
* @return float Float number ready for comparison. |
237
|
|
|
*/ |
238
|
|
|
private function normalizeNumber(mixed $number): float |
239
|
|
|
{ |
240
|
|
|
if ($number instanceof Stringable) { |
241
|
|
|
$number = (string) $number; |
242
|
|
|
} elseif ($number instanceof DateTimeInterface) { |
243
|
|
|
$number = $number->format('U'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return (float) $number; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Normalizes string that might be stored in a different type to simple string. |
251
|
|
|
* |
252
|
|
|
* @param mixed $string Raw string. Can be within an object implementing {@see DateTimeInterface} or other primitive |
253
|
|
|
* type, such as `int`, `float`, `string`. |
254
|
|
|
* |
255
|
|
|
* @return string String ready for comparison. |
256
|
|
|
*/ |
257
|
|
|
private function normalizeString(mixed $string): string |
258
|
|
|
{ |
259
|
|
|
if ($string instanceof DateTimeInterface) { |
260
|
|
|
$string = $string->format('U'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return (string) $string; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|