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