1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Stringable; |
8
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
9
|
|
|
use Yiisoft\Validator\Result; |
10
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
11
|
|
|
use Yiisoft\Validator\ValidationContext; |
12
|
|
|
|
13
|
|
|
use function gettype; |
14
|
|
|
use function in_array; |
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->getType(), $rule->getTargetValue()), |
65
|
|
|
'targetAttribute' => $targetAttribute, |
66
|
|
|
'targetAttributeValue' => $targetAttribute !== null ? $this->getFormattedValue($rule->getType(), $targetValue) : null, |
67
|
|
|
'targetValueOrAttribute' => $targetAttribute ?? $this->getFormattedValue($rule->getType(), $targetValue), |
68
|
|
|
'value' => $this->getFormattedValue($rule->getType(), $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 || is_scalar($value) || $value instanceof Stringable; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets representation of the value for using with error parameter. |
102
|
|
|
* |
103
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
104
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
105
|
|
|
* |
106
|
|
|
* @param mixed $value The мalidated value. |
107
|
|
|
* |
108
|
|
|
* @return scalar|null Formatted value. |
109
|
|
|
*/ |
110
|
|
|
private function getFormattedValue(string $type, 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 && $type !== CompareType::ORIGINAL) { |
117
|
|
|
return (string) $value; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return get_debug_type($value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Compares two values according to the specified type and operator. |
125
|
|
|
* |
126
|
|
|
* @param string $operator The comparison operator. One of `==`, `===`, `!=`, `!==`, `>`, `>=`, `<`, `<=`. |
127
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
128
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
129
|
|
|
* |
130
|
|
|
* @param mixed $value The validated value. |
131
|
|
|
* @param mixed $targetValue "Target" value set in rule options. |
132
|
|
|
* |
133
|
|
|
* @return bool Whether the result of comparison using the specified operator is true. |
134
|
|
|
*/ |
135
|
|
|
private function compareValues(string $type, string $operator, mixed $value, mixed $targetValue): bool |
136
|
|
|
{ |
137
|
|
|
if (!in_array($operator, ['==', '===', '!=', '!=='])) { |
138
|
|
|
if ($type === CompareType::STRING) { |
139
|
|
|
$value = (string) $value; |
140
|
|
|
$targetValue = (string) $targetValue; |
141
|
|
|
} elseif ($type === CompareType::NUMBER) { |
142
|
|
|
$value = $this->normalizeNumber($value); |
143
|
|
|
$targetValue = $this->normalizeNumber($targetValue); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return match ($operator) { |
148
|
|
|
'==' => $this->checkValuesAreEqual($type, $value, $targetValue), |
149
|
|
|
'===' => $this->checkValuesAreEqual($type, $value, $targetValue, strict: true), |
150
|
|
|
'!=' => !$this->checkValuesAreEqual($type, $value, $targetValue), |
151
|
|
|
'!==' => !$this->checkValuesAreEqual($type, $value, $targetValue, strict: true), |
152
|
|
|
'>' => $value > $targetValue, |
153
|
|
|
'>=' => $value >= $targetValue, |
154
|
|
|
'<' => $value < $targetValue, |
155
|
|
|
'<=' => $value <= $targetValue, |
156
|
|
|
}; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Checks whether a validated value equals to "target" value. For types other than {@see CompareType::ORIGINAL}, |
161
|
|
|
* handles strict comparison before type casting and takes edge cases for float numbers into account. |
162
|
|
|
* |
163
|
|
|
* @param string $type The type of the values being compared ({@see AbstractCompare::$type}). |
164
|
|
|
* @psalm-param CompareType::ORIGINAL | CompareType::STRING | CompareType::NUMBER $type |
165
|
|
|
* |
166
|
|
|
* @param mixed $value The validated value. |
167
|
|
|
* @param mixed $targetValue "Target" value set in rule options. |
168
|
|
|
* @param bool $strict Whether the values must be equal (when set to `false`, default) / strictly equal (when set to |
169
|
|
|
* `true`). |
170
|
|
|
* |
171
|
|
|
* @return bool `true` if values are equal and `false` otherwise. |
172
|
|
|
*/ |
173
|
|
|
private function checkValuesAreEqual(string $type, mixed $value, mixed $targetValue, bool $strict = false): bool |
174
|
|
|
{ |
175
|
|
|
if ($type === CompareType::ORIGINAL) { |
176
|
|
|
return $strict ? $value === $targetValue : $value == $targetValue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($strict && gettype($value) !== gettype($targetValue)) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return match ($type) { |
184
|
|
|
CompareType::STRING => (string) $value === (string) $targetValue, |
185
|
|
|
CompareType::NUMBER => $this->checkFloatsAreEqual( |
186
|
|
|
$this->normalizeNumber($value), |
187
|
|
|
$this->normalizeNumber($targetValue), |
188
|
|
|
), |
189
|
|
|
}; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Checks whether a validated float number equals to "target" float number. Handles a known problem of losing |
194
|
|
|
* precision during arithmetical operations. |
195
|
|
|
* |
196
|
|
|
* @param float $value The validated number. |
197
|
|
|
* @param float $targetValue "Target" number set in rule options. |
198
|
|
|
* |
199
|
|
|
* @return bool `true` if numbers are equal and `false` otherwise. |
200
|
|
|
* |
201
|
|
|
* @link https://floating-point-gui.de/ |
202
|
|
|
*/ |
203
|
|
|
private function checkFloatsAreEqual(float $value, float $targetValue): bool |
204
|
|
|
{ |
205
|
|
|
return abs($value - $targetValue) < PHP_FLOAT_EPSILON; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Normalizes number that might be stored in a different type to float number. |
210
|
|
|
* |
211
|
|
|
* @param mixed $number Raw number. Can be within an object implementing {@see Stringable} interface or other |
212
|
|
|
* primitive type, such as `int`, `float`, `string`. |
213
|
|
|
* |
214
|
|
|
* @return float Float number ready for comparison. |
215
|
|
|
*/ |
216
|
|
|
private function normalizeNumber(mixed $number): float |
217
|
|
|
{ |
218
|
|
|
if ($number instanceof Stringable) { |
219
|
|
|
$number = (string) $number; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return (float) $number; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|