1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
12
|
|
|
use Yiisoft\Validator\DumpedRuleInterface; |
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Defines validation options to check that the value is a number. |
19
|
|
|
* |
20
|
|
|
* The format of the number must match the regular expression specified in {@see AbstractNumber::$pattern}. Optionally, |
21
|
|
|
* you may configure the {@see AbstractNumber::$min} and {@see AbstractNumber::$max} to ensure the number is within a |
22
|
|
|
* certain range. |
23
|
|
|
* |
24
|
|
|
* @see NumberHandler |
25
|
|
|
* |
26
|
|
|
* @psalm-import-type WhenType from WhenInterface |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractNumber implements |
29
|
|
|
DumpedRuleInterface, |
30
|
|
|
SkipOnErrorInterface, |
31
|
|
|
WhenInterface, |
32
|
|
|
SkipOnEmptyInterface |
33
|
|
|
{ |
34
|
|
|
use SkipOnEmptyTrait; |
35
|
|
|
use SkipOnErrorTrait; |
36
|
|
|
use WhenTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A default for {@see $incorrectInputMessage}. |
40
|
|
|
*/ |
41
|
|
|
protected const DEFAULT_INCORRECT_INPUT_MESSAGE = 'The allowed types are integer, float and string.'; |
42
|
|
|
/** |
43
|
|
|
* A default for {@see $lessThanMinMessage}. |
44
|
|
|
*/ |
45
|
|
|
protected const DEFAULT_LESS_THAN_MIN_MESSAGE = 'Value must be no less than {min}.'; |
46
|
|
|
/** |
47
|
|
|
* A default for {@see $greaterThanMaxMessage}. |
48
|
|
|
*/ |
49
|
|
|
protected const DEFAULT_GREATER_THAN_MAX_MESSAGE = 'Value must be no greater than {max}.'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string The regular expression for matching numbers. |
53
|
|
|
* @psalm-var non-empty-string |
54
|
|
|
*/ |
55
|
|
|
private string $pattern; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param float|int|null $min Lower limit of the number. Defaults to `null`, meaning no lower limit. See |
59
|
|
|
* {@see $lessThanMinMessage} for the customized message used when the number is too small. |
60
|
|
|
* @param float|int|null $max Upper limit of the number. Defaults to `null`, meaning no upper limit. See |
61
|
|
|
* {@see $greaterThanMaxMessage} for the customized message used when the number is too big. |
62
|
|
|
* @param string $incorrectInputMessage Error message used when the value is not numeric. |
63
|
|
|
* |
64
|
|
|
* You may use the following placeholders in the message: |
65
|
|
|
* |
66
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
67
|
|
|
* - `{type}`: the type of the value being validated. |
68
|
|
|
* @param string $notNumberMessage Error message used when the value does not match {@see $pattern}. |
69
|
|
|
* |
70
|
|
|
* You may use the following placeholders in the message: |
71
|
|
|
* |
72
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
73
|
|
|
* - `{value}`: actual value. |
74
|
|
|
* @param string $lessThanMinMessage Error message used when the value is smaller than {@link $min}. |
75
|
|
|
* |
76
|
|
|
* You may use the following placeholders in the message: |
77
|
|
|
* |
78
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
79
|
|
|
* - `{min}`: minimum value. |
80
|
|
|
* - `{value}`: actual value. |
81
|
|
|
* @param string $greaterThanMaxMessage Error message used when the value is bigger than {@link $max}. |
82
|
|
|
* |
83
|
|
|
* You may use the following placeholders in the message: |
84
|
|
|
* |
85
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
86
|
|
|
* - `{max}`: maximum value. |
87
|
|
|
* - `{value}`: actual value. |
88
|
|
|
* @param string $pattern The regular expression for matching numbers. |
89
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. See |
90
|
|
|
* {@see SkipOnEmptyInterface}. |
91
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See |
92
|
|
|
* {@see SkipOnErrorInterface}. |
93
|
|
|
* @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}. |
94
|
|
|
* |
95
|
|
|
* @psalm-param WhenType $when |
96
|
|
|
*/ |
97
|
|
|
public function __construct( |
98
|
|
|
private float|int|null $min, |
99
|
|
|
private float|int|null $max, |
100
|
|
|
private string $incorrectInputMessage, |
101
|
|
|
private string $notNumberMessage, |
102
|
|
|
private string $lessThanMinMessage, |
103
|
|
|
private string $greaterThanMaxMessage, |
104
|
|
|
string $pattern, |
105
|
|
|
private mixed $skipOnEmpty, |
106
|
|
|
private bool $skipOnError, |
107
|
|
|
private Closure|null $when, |
108
|
|
|
) { |
109
|
|
|
if ($pattern === '') { |
110
|
|
|
throw new InvalidArgumentException('Pattern can\'t be empty.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->pattern = $pattern; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getName(): string |
117
|
|
|
{ |
118
|
|
|
return static::class; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get lower limit of the number. `null` means no lower limit. |
123
|
|
|
* |
124
|
|
|
* @return float|int|null Lower limit of the number. |
125
|
|
|
* |
126
|
|
|
* @see $min |
127
|
|
|
*/ |
128
|
|
|
public function getMin(): float|int|null |
129
|
|
|
{ |
130
|
|
|
return $this->min; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get upper limit of the number. `null` means no upper limit. |
135
|
|
|
* |
136
|
|
|
* @return float|int|null Upper limit of the number. |
137
|
|
|
* |
138
|
|
|
* @see $max |
139
|
|
|
*/ |
140
|
|
|
public function getMax(): float|int|null |
141
|
|
|
{ |
142
|
|
|
return $this->max; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get error message used when the value is not numeric. |
147
|
|
|
* |
148
|
|
|
* @return string Error message. |
149
|
|
|
* |
150
|
|
|
* @see $incorrectInputMessage |
151
|
|
|
*/ |
152
|
|
|
public function getIncorrectInputMessage(): string |
153
|
|
|
{ |
154
|
|
|
return $this->incorrectInputMessage; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get error message used when the value does not match {@see $pattern} |
159
|
|
|
* |
160
|
|
|
* @return string Error message. |
161
|
|
|
* |
162
|
|
|
* @see $notNumberMessage |
163
|
|
|
*/ |
164
|
|
|
public function getNotNumberMessage(): string |
165
|
|
|
{ |
166
|
|
|
return $this->notNumberMessage; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get error message used when the value is smaller than {@link $min}. |
171
|
|
|
* |
172
|
|
|
* @return string Error message. |
173
|
|
|
* |
174
|
|
|
* @see $lessThanMinMessage |
175
|
|
|
*/ |
176
|
|
|
public function getLessThanMinMessage(): string |
177
|
|
|
{ |
178
|
|
|
return $this->lessThanMinMessage; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get error message used when the value is bigger than {@link $max}. |
183
|
|
|
* |
184
|
|
|
* @return string Error message. |
185
|
|
|
* |
186
|
|
|
* @see $greaterThanMaxMessage |
187
|
|
|
*/ |
188
|
|
|
public function getGreaterThanMaxMessage(): string |
189
|
|
|
{ |
190
|
|
|
return $this->greaterThanMaxMessage; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* The regular expression for matching numbers. |
195
|
|
|
* |
196
|
|
|
* @return string The regular expression. |
197
|
|
|
* @psalm-return non-empty-string |
198
|
|
|
* |
199
|
|
|
* @see $pattern |
200
|
|
|
*/ |
201
|
|
|
public function getPattern(): string |
202
|
|
|
{ |
203
|
|
|
return $this->pattern; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getOptions(): array |
207
|
|
|
{ |
208
|
|
|
return [ |
209
|
|
|
'min' => $this->min, |
210
|
|
|
'max' => $this->max, |
211
|
|
|
'incorrectInputMessage' => [ |
212
|
|
|
'template' => $this->incorrectInputMessage, |
213
|
|
|
'parameters' => [], |
214
|
|
|
], |
215
|
|
|
'notNumberMessage' => [ |
216
|
|
|
'template' => $this->notNumberMessage, |
217
|
|
|
'parameters' => [], |
218
|
|
|
], |
219
|
|
|
'lessThanMinMessage' => [ |
220
|
|
|
'template' => $this->lessThanMinMessage, |
221
|
|
|
'parameters' => ['min' => $this->min], |
222
|
|
|
], |
223
|
|
|
'greaterThanMaxMessage' => [ |
224
|
|
|
'template' => $this->greaterThanMaxMessage, |
225
|
|
|
'parameters' => ['max' => $this->max], |
226
|
|
|
], |
227
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
228
|
|
|
'skipOnError' => $this->skipOnError, |
229
|
|
|
'pattern' => $this->pattern, |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getHandler(): string |
234
|
|
|
{ |
235
|
|
|
return NumberHandler::class; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|