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