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\Rule\Trait\RuleNameTrait; |
|
|
|
|
10
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
11
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Validates that the value is a number. |
15
|
|
|
* |
16
|
|
|
* The format of the number must match the regular expression specified in {@see Number::$integerPattern} |
17
|
|
|
* or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()} |
18
|
|
|
* to ensure the number is within certain range. |
19
|
|
|
*/ |
20
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
21
|
|
|
final class Number implements ParametrizedRuleInterface |
22
|
|
|
{ |
23
|
|
|
use HandlerClassNameTrait; |
24
|
|
|
use RuleNameTrait; |
25
|
|
|
|
26
|
2 |
|
public function __construct( |
27
|
|
|
/** |
28
|
|
|
* @var bool whether the value can only be an integer. Defaults to false. |
29
|
|
|
*/ |
30
|
|
|
private bool $asInteger = false, |
31
|
|
|
/** |
32
|
|
|
* @var float|int lower limit of the number. Defaults to null, meaning no lower limit. |
33
|
|
|
* |
34
|
|
|
* @see tooSmallMessage for the customized message used when the number is too small. |
35
|
|
|
*/ |
36
|
|
|
private $min = null, |
37
|
|
|
/** |
38
|
|
|
* @var float|int upper limit of the number. Defaults to null, meaning no upper limit. |
39
|
|
|
* |
40
|
|
|
* @see tooBigMessage for the customized message used when the number is too big. |
41
|
|
|
*/ |
42
|
|
|
private $max = null, |
43
|
|
|
/** |
44
|
|
|
* @var string user-defined error message used when the value is smaller than {@link $min}. |
45
|
|
|
*/ |
46
|
|
|
private string $tooSmallMessage = 'Value must be no less than {min}.', |
47
|
|
|
/** |
48
|
|
|
* @var string user-defined error message used when the value is bigger than {@link $max}. |
49
|
|
|
*/ |
50
|
|
|
private string $tooBigMessage = 'Value must be no greater than {max}.', |
51
|
|
|
/** |
52
|
|
|
* @var string the regular expression for matching integers. |
53
|
|
|
*/ |
54
|
|
|
private string $integerPattern = '/^\s*[+-]?\d+\s*$/', |
55
|
|
|
/** |
56
|
|
|
* @var string the regular expression for matching numbers. It defaults to a pattern |
57
|
|
|
* that matches floating numbers with optional exponential part (e.g. -1.23e-10). |
58
|
|
|
*/ |
59
|
|
|
private string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/', |
60
|
|
|
private bool $skipOnEmpty = false, |
61
|
|
|
private bool $skipOnError = false, |
62
|
|
|
private ?Closure $when = null, |
63
|
|
|
) { |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
72 |
|
public function isAsInteger(): bool |
70
|
|
|
{ |
71
|
72 |
|
return $this->asInteger; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return float|int|null |
76
|
|
|
*/ |
77
|
46 |
|
public function getMin(): float|int|null |
78
|
|
|
{ |
79
|
46 |
|
return $this->min; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return float|int|null |
84
|
|
|
*/ |
85
|
38 |
|
public function getMax(): float|int|null |
86
|
|
|
{ |
87
|
38 |
|
return $this->max; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
11 |
|
public function getTooSmallMessage(): string |
94
|
|
|
{ |
95
|
11 |
|
return $this->tooSmallMessage; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
5 |
|
public function getTooBigMessage(): string |
102
|
|
|
{ |
103
|
5 |
|
return $this->tooBigMessage; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
29 |
|
public function getIntegerPattern(): string |
110
|
|
|
{ |
111
|
29 |
|
return $this->integerPattern; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
38 |
|
public function getNumberPattern(): string |
118
|
|
|
{ |
119
|
38 |
|
return $this->numberPattern; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isSkipOnEmpty(): bool |
126
|
|
|
{ |
127
|
|
|
return $this->skipOnEmpty; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function isSkipOnError(): bool |
134
|
|
|
{ |
135
|
|
|
return $this->skipOnError; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return Closure|null |
140
|
|
|
*/ |
141
|
|
|
public function getWhen(): ?Closure |
142
|
|
|
{ |
143
|
|
|
return $this->when; |
144
|
|
|
} |
145
|
|
|
|
146
|
9 |
|
public function getNotANumberMessage(): string |
147
|
|
|
{ |
148
|
9 |
|
return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.'; |
149
|
|
|
} |
150
|
|
|
|
151
|
9 |
|
public function getOptions(): array |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
9 |
|
'asInteger' => $this->asInteger, |
155
|
9 |
|
'min' => $this->min, |
156
|
9 |
|
'max' => $this->max, |
157
|
|
|
'notANumberMessage' => [ |
158
|
9 |
|
'message' => $this->getNotANumberMessage(), |
159
|
|
|
], |
160
|
|
|
'tooSmallMessage' => [ |
161
|
9 |
|
'message' => $this->tooSmallMessage, |
162
|
9 |
|
'parameters' => ['min' => $this->min], |
163
|
|
|
], |
164
|
|
|
'tooBigMessage' => [ |
165
|
9 |
|
'message' => $this->tooBigMessage, |
166
|
9 |
|
'parameters' => ['max' => $this->max], |
167
|
|
|
], |
168
|
9 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
169
|
9 |
|
'skipOnError' => $this->skipOnError, |
170
|
9 |
|
'integerPattern' => $this->integerPattern, |
171
|
9 |
|
'numberPattern' => $this->numberPattern, |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|