1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Validator\DataSetInterface; |
8
|
|
|
use Yiisoft\Validator\Result; |
9
|
|
|
use Yiisoft\Validator\Rule; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* CompareValidator compares the specified attribute value with another value. |
13
|
|
|
* |
14
|
|
|
* The value being compared with a constant [[compareValue]], which is set |
15
|
|
|
* in the constructor. |
16
|
|
|
* |
17
|
|
|
* CompareValidator supports different comparison operators, specified |
18
|
|
|
* via the [[operator]] property. |
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 set the [[$type]] |
22
|
|
|
* to [[TYPE_NUMBER]] to enable numeric comparison. |
23
|
|
|
*/ |
24
|
|
|
class CompareTo extends Rule |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Constant for specifying the comparison [[type]] by numeric values. |
28
|
|
|
* |
29
|
|
|
* @see type |
30
|
|
|
*/ |
31
|
|
|
private const TYPE_STRING = 'string'; |
32
|
|
|
/** |
33
|
|
|
* Constant for specifying the comparison [[type]] by numeric values. |
34
|
|
|
* |
35
|
|
|
* @see type |
36
|
|
|
*/ |
37
|
|
|
private const TYPE_NUMBER = 'number'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var mixed the constant value to be compared with. |
41
|
|
|
*/ |
42
|
|
|
private $compareValue; |
43
|
|
|
/** |
44
|
|
|
* @var string the type of the values being compared. The follow types are supported: |
45
|
|
|
* |
46
|
|
|
* - [[TYPE_STRING|string]]: the values are being compared as strings. No conversion will be done before comparison. |
47
|
|
|
* - [[TYPE_NUMBER|number]]: the values are being compared as numbers. String values will be converted into numbers before comparison. |
48
|
|
|
*/ |
49
|
|
|
private string $type = self::TYPE_STRING; |
50
|
|
|
/** |
51
|
|
|
* @var string the operator for comparison. The following operators are supported: |
52
|
|
|
* |
53
|
|
|
* - `==`: check if two values are equal. The comparison is done is non-strict mode. |
54
|
|
|
* - `===`: check if two values are equal. The comparison is done is strict mode. |
55
|
|
|
* - `!=`: check if two values are NOT equal. The comparison is done is non-strict mode. |
56
|
|
|
* - `!==`: check if two values are NOT equal. The comparison is done is strict mode. |
57
|
|
|
* - `>`: check if value being validated is greater than the value being compared with. |
58
|
|
|
* - `>=`: check if value being validated is greater than or equal to the value being compared with. |
59
|
|
|
* - `<`: check if value being validated is less than the value being compared with. |
60
|
|
|
* - `<=`: check if value being validated is less than or equal to the value being compared with. |
61
|
|
|
* |
62
|
|
|
* When you want to compare numbers, make sure to also set [[type]] to `number`. |
63
|
|
|
*/ |
64
|
|
|
private string $operator = '=='; |
65
|
|
|
|
66
|
|
|
private array $validOperators = [ |
67
|
|
|
'==' => 1, |
68
|
|
|
'===' => 1, |
69
|
|
|
'!=' => 1, |
70
|
|
|
'!==' => 1, |
71
|
|
|
'>' => 1, |
72
|
|
|
'>=' => 1, |
73
|
|
|
'<' => 1, |
74
|
|
|
'<=' => 1, |
75
|
|
|
]; |
76
|
|
|
|
77
|
7 |
|
private function getMessage(): string |
78
|
|
|
{ |
79
|
7 |
|
switch ($this->operator) { |
80
|
7 |
|
case '==': |
81
|
3 |
|
case '===': |
82
|
5 |
|
return 'Value must be equal to "{value}".'; |
83
|
3 |
|
case '!=': |
84
|
3 |
|
case '!==': |
85
|
2 |
|
return 'Value must not be equal to "{value}".'; |
86
|
2 |
|
case '>': |
87
|
1 |
|
return 'Value must be greater than "{value}".'; |
88
|
2 |
|
case '>=': |
89
|
2 |
|
return 'Value must be greater than or equal to "{value}".'; |
90
|
1 |
|
case '<': |
91
|
1 |
|
return 'Value must be less than "{value}".'; |
92
|
1 |
|
case '<=': |
93
|
1 |
|
return 'Value must be less than or equal to "{value}".'; |
94
|
|
|
default: |
95
|
|
|
throw new \RuntimeException("Unknown operator: {$this->operator}"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function __construct($value) |
100
|
|
|
{ |
101
|
2 |
|
$this->compareValue = $value; |
102
|
2 |
|
} |
103
|
|
|
|
104
|
1 |
|
public function operator(string $operator): self |
105
|
|
|
{ |
106
|
1 |
|
if (!isset($this->validOperators[$operator])) { |
107
|
|
|
throw new \InvalidArgumentException("Operator \"$operator\" is not supported."); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$new = clone $this; |
111
|
1 |
|
$new->operator = $operator; |
112
|
1 |
|
return $new; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function asString(): self |
116
|
|
|
{ |
117
|
|
|
$new = clone $this; |
118
|
|
|
$new->type = self::TYPE_STRING; |
119
|
|
|
return $new; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function asNumber(): self |
123
|
|
|
{ |
124
|
|
|
$new = clone $this; |
125
|
|
|
$new->type = self::TYPE_NUMBER; |
126
|
|
|
return $new; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
130
|
|
|
{ |
131
|
1 |
|
$result = new Result(); |
132
|
|
|
|
133
|
1 |
|
if ($this->compareValue === null) { |
134
|
|
|
throw new \RuntimeException('CompareValidator::compareValue must be set.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
if (!$this->compareValues($this->operator, $this->type, $value, $this->compareValue)) { |
138
|
1 |
|
$result->addError( |
139
|
1 |
|
$this->translateMessage( |
140
|
1 |
|
$this->getMessage(), |
141
|
|
|
[ |
142
|
1 |
|
'value' => $this->compareValue, |
143
|
|
|
] |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Compares two values with the specified operator. |
153
|
|
|
* |
154
|
|
|
* @param string $operator the comparison operator |
155
|
|
|
* @param string $type the type of the values being compared |
156
|
|
|
* @param mixed $value the value being compared |
157
|
|
|
* @param mixed $compareValue another value being compared |
158
|
|
|
* |
159
|
|
|
* @return bool whether the comparison using the specified operator is true. |
160
|
|
|
*/ |
161
|
1 |
|
protected function compareValues(string $operator, string $type, $value, $compareValue): bool |
162
|
|
|
{ |
163
|
1 |
|
if ($type === self::TYPE_NUMBER) { |
164
|
|
|
$value = (float)$value; |
165
|
|
|
$compareValue = (float)$compareValue; |
166
|
|
|
} else { |
167
|
1 |
|
$value = (string)$value; |
168
|
1 |
|
$compareValue = (string)$compareValue; |
169
|
|
|
} |
170
|
1 |
|
switch ($operator) { |
171
|
1 |
|
case '==': |
172
|
1 |
|
return $value == $compareValue; |
173
|
1 |
|
case '===': |
174
|
1 |
|
return $value === $compareValue; |
175
|
1 |
|
case '!=': |
176
|
1 |
|
return $value != $compareValue; |
177
|
1 |
|
case '!==': |
178
|
1 |
|
return $value !== $compareValue; |
179
|
1 |
|
case '>': |
180
|
1 |
|
return $value > $compareValue; |
181
|
1 |
|
case '>=': |
182
|
1 |
|
return $value >= $compareValue; |
183
|
1 |
|
case '<': |
184
|
1 |
|
return $value < $compareValue; |
185
|
1 |
|
case '<=': |
186
|
1 |
|
return $value <= $compareValue; |
187
|
|
|
default: |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
public function getOptions(): array |
193
|
|
|
{ |
194
|
6 |
|
return array_merge( |
195
|
6 |
|
parent::getOptions(), |
196
|
|
|
[ |
197
|
6 |
|
'type' => $this->type, |
198
|
6 |
|
'operator' => $this->operator, |
199
|
6 |
|
'compareValue' => $this->compareValue, |
200
|
6 |
|
'message' => $this->translateMessage($this->getMessage(), ['value' => $this->compareValue]), |
201
|
|
|
], |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|