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