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