1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
8
|
|
|
use Yiisoft\Validator\Formatter; |
9
|
|
|
use Yiisoft\Validator\FormatterInterface; |
10
|
|
|
use Yiisoft\Validator\Result; |
11
|
|
|
use Yiisoft\Validator\ValidationContext; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Compares the specified value with another value. |
15
|
|
|
* |
16
|
|
|
* The value being compared with a constant {@see CompareTo::$compareValue}, which is set |
17
|
|
|
* in the constructor. |
18
|
|
|
* |
19
|
|
|
* It supports different comparison operators, specified |
20
|
|
|
* via the {@see CompareTo::$operator}. |
21
|
|
|
* |
22
|
|
|
* The default comparison function is based on string values, which means the values |
23
|
|
|
* are compared byte by byte. When comparing numbers, make sure to change {@see CompareTo::$type} to |
24
|
|
|
* {@see CompareTo::TYPE_NUMBER} to enable numeric comparison. |
25
|
|
|
*/ |
26
|
|
|
final class CompareToHandler implements RuleHandlerInterface |
27
|
|
|
{ |
28
|
|
|
private FormatterInterface $formatter; |
29
|
|
|
|
30
|
35 |
|
public function __construct(?FormatterInterface $formatter = null) |
31
|
|
|
{ |
32
|
35 |
|
$this->formatter = $formatter ?? new Formatter(); |
33
|
|
|
} |
34
|
|
|
|
35
|
35 |
|
public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
36
|
|
|
{ |
37
|
35 |
|
if (!$rule instanceof CompareTo) { |
38
|
1 |
|
throw new UnexpectedRuleException(CompareTo::class, $rule); |
39
|
|
|
} |
40
|
|
|
|
41
|
34 |
|
$result = new Result(); |
42
|
34 |
|
$compareAttribute = $rule->getCompareAttribute(); |
43
|
34 |
|
$compareValue = $rule->getCompareValue(); |
44
|
|
|
|
45
|
34 |
|
if ($compareValue === null && $compareAttribute !== null) { |
46
|
4 |
|
$compareValue = $context?->getDataSet()?->getAttributeValue($compareAttribute); |
47
|
|
|
} |
48
|
|
|
|
49
|
34 |
|
if (!$this->compareValues($rule->getOperator(), $rule->getType(), $value, $compareValue)) { |
50
|
17 |
|
$formattedMessage = $this->formatter->format( |
51
|
17 |
|
$rule->getMessage(), |
52
|
|
|
[ |
53
|
17 |
|
'attribute' => $context?->getAttribute(), |
54
|
17 |
|
'compareValue' => $rule->getCompareValue(), |
55
|
17 |
|
'compareAttribute' => $rule->getCompareAttribute(), |
56
|
17 |
|
'compareValueOrAttribute' => $compareValue ?? $compareAttribute, |
57
|
|
|
'value' => $value, |
58
|
|
|
] |
59
|
|
|
); |
60
|
17 |
|
$result->addError($formattedMessage); |
61
|
|
|
} |
62
|
|
|
|
63
|
34 |
|
return $result; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Compares two values with the specified operator. |
68
|
|
|
* |
69
|
|
|
* @param string $operator the comparison operator |
70
|
|
|
* @param string $type the type of the values being compared |
71
|
|
|
* @param mixed $value the value being compared |
72
|
|
|
* @param mixed $compareValue another value being compared |
73
|
|
|
* |
74
|
|
|
* @return bool whether the comparison using the specified operator is true. |
75
|
|
|
*/ |
76
|
34 |
|
private function compareValues(string $operator, string $type, mixed $value, mixed $compareValue): bool |
77
|
|
|
{ |
78
|
34 |
|
if ($type === CompareTo::TYPE_NUMBER) { |
79
|
|
|
$value = (float)$value; |
80
|
|
|
$compareValue = (float)$compareValue; |
81
|
|
|
} else { |
82
|
34 |
|
$value = (string)$value; |
83
|
34 |
|
$compareValue = (string)$compareValue; |
84
|
|
|
} |
85
|
34 |
|
return match ($operator) { |
86
|
6 |
|
'==' => $value == $compareValue, |
87
|
5 |
|
'===' => $value === $compareValue, |
88
|
5 |
|
'!=' => $value != $compareValue, |
89
|
4 |
|
'!==' => $value !== $compareValue, |
90
|
3 |
|
'>' => $value > $compareValue, |
91
|
3 |
|
'>=' => $value >= $compareValue, |
92
|
3 |
|
'<' => $value < $compareValue, |
93
|
5 |
|
'<=' => $value <= $compareValue, |
94
|
34 |
|
default => false, |
95
|
|
|
}; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|