|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Yiisoft\Validator\LimitInterface; |
|
10
|
|
|
use Yiisoft\Validator\Rule\Trait\LimitTrait; |
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
|
14
|
|
|
use Yiisoft\Validator\RuleWithOptionsInterface; |
|
15
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
16
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
17
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Validates that the value is a number. |
|
21
|
|
|
* |
|
22
|
|
|
* The format of the number must match the regular expression specified in {@see Number::$integerPattern} |
|
23
|
|
|
* or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()} |
|
24
|
|
|
* to ensure the number is within certain range. |
|
25
|
|
|
* |
|
26
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
29
|
|
|
final class Number implements |
|
30
|
|
|
RuleWithOptionsInterface, |
|
31
|
|
|
SkipOnErrorInterface, |
|
32
|
36 |
|
WhenInterface, |
|
33
|
|
|
SkipOnEmptyInterface, |
|
34
|
|
|
LimitInterface |
|
35
|
|
|
{ |
|
36
|
|
|
use LimitTrait; |
|
37
|
|
|
use SkipOnEmptyTrait; |
|
38
|
|
|
use SkipOnErrorTrait; |
|
39
|
|
|
use WhenTrait; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct( |
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool whether the value can only be an integer. Defaults to `false`. |
|
44
|
|
|
*/ |
|
45
|
|
|
private bool $asInteger = false, |
|
46
|
|
|
/** |
|
47
|
|
|
* @var float|int|null lower limit of the number. Defaults to `null`, meaning no lower limit. Can't be combined |
|
48
|
|
|
* with {@see $exactly}. |
|
49
|
|
|
* |
|
50
|
|
|
* @see $lessThanMin for the message used when the number is too small. |
|
51
|
|
|
*/ |
|
52
|
|
|
float|int|null $min = null, |
|
53
|
|
|
/** |
|
54
|
|
|
* @var float|int|null upper limit of the number. Defaults to `null`, meaning no upper limit. Can't be combined |
|
55
|
|
|
* with {@see $exactly}. |
|
56
|
|
|
* |
|
57
|
|
|
* @see $greaterThanMax for the message used when the number is too big. |
|
58
|
|
|
*/ |
|
59
|
|
|
float|int|null $max = null, |
|
60
|
|
|
/** |
|
61
|
|
|
* @var int|null exact number. `null` means no strict comparison. Mutually exclusive with {@see $min} |
|
62
|
|
|
* and {@see $max}. |
|
63
|
|
|
* |
|
64
|
|
|
* @see $notExactlyMessage for the message used when the number does not equal to the set one. |
|
65
|
|
|
*/ |
|
66
|
|
|
float|int|null $exactly = null, |
|
67
|
|
|
private string $incorrectInputMessage = 'The allowed types are integer, float and string.', |
|
68
|
|
|
/** |
|
69
|
|
|
* @var string error message used when the value is smaller than {@link $min}. |
|
70
|
|
|
*/ |
|
71
|
|
|
private string $lessThanMinMessage = 'Value must be no less than {min}.', |
|
72
|
|
|
/** |
|
73
|
|
|
* @var string error message used when the value is bigger than {@link $max}. |
|
74
|
|
|
*/ |
|
75
|
|
|
private string $greaterThanMaxMessage = 'Value must be no greater than {max}.', |
|
76
|
|
|
/** |
|
77
|
|
|
* @var string error message used when the value does not equal {@see $exactly}. |
|
78
|
|
|
*/ |
|
79
|
|
|
private string $notExactlyMessage = 'Value must be equal to {exactly}.', |
|
80
|
10 |
|
/** |
|
81
|
|
|
* @var string the regular expression for matching integers. |
|
82
|
10 |
|
*/ |
|
83
|
|
|
private string $integerPattern = '/^\s*[+-]?\d+\s*$/', |
|
84
|
|
|
/** |
|
85
|
106 |
|
* @var string the regular expression for matching numbers. It defaults to a pattern |
|
86
|
|
|
* that matches floating numbers with optional exponential part (e.g. -1.23e-10). |
|
87
|
106 |
|
*/ |
|
88
|
|
|
private string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/', |
|
89
|
|
|
|
|
|
|
|
|
|
90
|
83 |
|
/** |
|
91
|
|
|
* @var bool|callable|null |
|
92
|
83 |
|
*/ |
|
93
|
|
|
private $skipOnEmpty = null, |
|
94
|
|
|
private bool $skipOnError = false, |
|
95
|
57 |
|
/** |
|
96
|
|
|
* @var WhenType |
|
97
|
57 |
|
*/ |
|
98
|
|
|
private Closure|null $when = null, |
|
99
|
|
|
) { |
|
100
|
14 |
|
$this->initLimitProperties( |
|
101
|
|
|
$min, |
|
102
|
14 |
|
$max, |
|
103
|
|
|
$exactly, |
|
104
|
|
|
$lessThanMinMessage, |
|
105
|
39 |
|
$greaterThanMaxMessage, |
|
106
|
|
|
$notExactlyMessage, |
|
107
|
39 |
|
requireLimits: false, |
|
108
|
|
|
allowNegativeLimits: true, |
|
109
|
|
|
); |
|
110
|
20 |
|
} |
|
111
|
|
|
|
|
112
|
20 |
|
public function getName(): string |
|
113
|
|
|
{ |
|
114
|
|
|
return 'number'; |
|
115
|
42 |
|
} |
|
116
|
|
|
|
|
117
|
42 |
|
public function isAsInteger(): bool |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->asInteger; |
|
120
|
64 |
|
} |
|
121
|
|
|
|
|
122
|
64 |
|
public function getIncorrectInputMessage(): string |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->incorrectInputMessage; |
|
125
|
37 |
|
} |
|
126
|
|
|
|
|
127
|
37 |
|
public function getIntegerPattern(): string |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->integerPattern; |
|
130
|
14 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function getNumberPattern(): string |
|
133
|
14 |
|
{ |
|
134
|
14 |
|
return $this->numberPattern; |
|
135
|
14 |
|
} |
|
136
|
|
|
|
|
137
|
14 |
|
public function getNotNumberMessage(): string |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.'; |
|
140
|
|
|
} |
|
141
|
14 |
|
|
|
142
|
|
|
public function getOptions(): array |
|
143
|
|
|
{ |
|
144
|
|
|
return array_merge($this->getLimitOptions(), [ |
|
145
|
14 |
|
'asInteger' => $this->asInteger, |
|
146
|
14 |
|
'incorrectInputMessage' => [ |
|
147
|
|
|
'template' => $this->incorrectInputMessage, |
|
148
|
|
|
'parameters' => [], |
|
149
|
14 |
|
], |
|
150
|
14 |
|
'notNumberMessage' => [ |
|
151
|
|
|
'template' => $this->getNotNumberMessage(), |
|
152
|
14 |
|
'parameters' => [], |
|
153
|
14 |
|
], |
|
154
|
14 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
155
|
14 |
|
'skipOnError' => $this->skipOnError, |
|
156
|
|
|
'integerPattern' => $this->integerPattern, |
|
157
|
|
|
'numberPattern' => $this->numberPattern, |
|
158
|
|
|
]); |
|
159
|
120 |
|
} |
|
160
|
|
|
|
|
161
|
120 |
|
public function getHandlerClassName(): string |
|
162
|
|
|
{ |
|
163
|
|
|
return NumberHandler::class; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|