1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Rules; |
6
|
|
|
|
7
|
|
|
use Smoren\Validator\Factories\Checks\NumericCheckFactory; |
8
|
|
|
use Smoren\Validator\Interfaces\NumericRuleInterface; |
9
|
|
|
|
10
|
|
|
class NumericRule extends MixedRule implements NumericRuleInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $name |
14
|
|
|
*/ |
15
|
83 |
|
public function __construct(string $name) |
16
|
|
|
{ |
17
|
83 |
|
parent::__construct($name); |
18
|
|
|
|
19
|
83 |
|
$this->check(NumericCheckFactory::getNumericCheck(), true); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
* |
25
|
|
|
* @return static |
26
|
|
|
*/ |
27
|
2 |
|
public function number(bool $stopOnViolation = true): self |
28
|
|
|
{ |
29
|
2 |
|
return $this->check(NumericCheckFactory::getNumberCheck(), $stopOnViolation); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
* |
35
|
|
|
* @return static |
36
|
|
|
*/ |
37
|
2 |
|
public function string(bool $stopOnViolation = true): self |
38
|
|
|
{ |
39
|
2 |
|
return $this->check(NumericCheckFactory::getStringCheck(), $stopOnViolation); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
* |
45
|
|
|
* @return static |
46
|
|
|
*/ |
47
|
69 |
|
public function integer(bool $stopOnViolation = true): self |
48
|
|
|
{ |
49
|
69 |
|
return $this->check(NumericCheckFactory::getIntegerCheck(), $stopOnViolation); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
* |
55
|
|
|
* @return static |
56
|
|
|
*/ |
57
|
48 |
|
public function float(bool $stopOnViolation = true): self |
58
|
|
|
{ |
59
|
48 |
|
return $this->check(NumericCheckFactory::getFloatCheck(), $stopOnViolation); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
* |
65
|
|
|
* @return static |
66
|
|
|
*/ |
67
|
6 |
|
public function truthy(): self |
68
|
|
|
{ |
69
|
6 |
|
return $this->check(NumericCheckFactory::getTruthyCheck()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
* |
75
|
|
|
* @return static |
76
|
|
|
*/ |
77
|
6 |
|
public function falsy(): self |
78
|
|
|
{ |
79
|
6 |
|
return $this->check(NumericCheckFactory::getFalsyCheck()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
56 |
|
public function positive(): self |
88
|
|
|
{ |
89
|
56 |
|
return $this->check(NumericCheckFactory::getPositiveCheck()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
* |
95
|
|
|
* @return static |
96
|
|
|
*/ |
97
|
6 |
|
public function nonPositive(): self |
98
|
|
|
{ |
99
|
6 |
|
return $this->check(NumericCheckFactory::getNonPositiveCheck()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
* |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
13 |
|
public function nonNegative(): self |
108
|
|
|
{ |
109
|
13 |
|
return $this->check(NumericCheckFactory::getNonNegativeCheck()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
* |
115
|
|
|
* @return static |
116
|
|
|
*/ |
117
|
6 |
|
public function negative(): self |
118
|
|
|
{ |
119
|
6 |
|
return $this->check(NumericCheckFactory::getNegativeCheck()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
* |
125
|
|
|
* @return static |
126
|
|
|
*/ |
127
|
11 |
|
public function greaterThan($number): NumericRuleInterface |
128
|
|
|
{ |
129
|
11 |
|
return $this->check(NumericCheckFactory::getGreaterThanCheck($number)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
6 |
|
public function greaterOrEqual($number): NumericRuleInterface |
138
|
|
|
{ |
139
|
6 |
|
return $this->check(NumericCheckFactory::getGreaterOrEqualCheck($number)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
* |
145
|
|
|
* @return static |
146
|
|
|
*/ |
147
|
13 |
|
public function lessThan($number): NumericRuleInterface |
148
|
|
|
{ |
149
|
13 |
|
return $this->check(NumericCheckFactory::getLessThanCheck($number)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritDoc} |
154
|
|
|
* |
155
|
|
|
* @return static |
156
|
|
|
*/ |
157
|
6 |
|
public function lessOrEqual($number): NumericRuleInterface |
158
|
|
|
{ |
159
|
6 |
|
return $this->check(NumericCheckFactory::getLessOrEqualCheck($number)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
* |
165
|
|
|
* @return static |
166
|
|
|
*/ |
167
|
23 |
|
public function between($start, $end): self |
168
|
|
|
{ |
169
|
23 |
|
return $this->check(NumericCheckFactory::getBetweenCheck($start, $end)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
22 |
|
public function inInterval($start, $end): self |
176
|
|
|
{ |
177
|
22 |
|
return $this->check(NumericCheckFactory::getInIntervalCheck($start, $end)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritDoc} |
182
|
|
|
* |
183
|
|
|
* @return static |
184
|
|
|
*/ |
185
|
4 |
|
public function fractional(): self |
186
|
|
|
{ |
187
|
4 |
|
return $this->check(NumericCheckFactory::getFractionalCheck()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritDoc} |
192
|
|
|
* |
193
|
|
|
* @return static |
194
|
|
|
*/ |
195
|
7 |
|
public function nonFractional(): self |
196
|
|
|
{ |
197
|
7 |
|
return $this->check(NumericCheckFactory::getNonFractionalCheck()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritDoc} |
202
|
|
|
* |
203
|
|
|
* @return static |
204
|
|
|
*/ |
205
|
4 |
|
public function finite(): self |
206
|
|
|
{ |
207
|
4 |
|
return $this->check(NumericCheckFactory::getFiniteCheck()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritDoc} |
212
|
|
|
* |
213
|
|
|
* @return static |
214
|
|
|
*/ |
215
|
4 |
|
public function infinite(): self |
216
|
|
|
{ |
217
|
4 |
|
return $this->check(NumericCheckFactory::getInfiniteCheck()); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritDoc} |
222
|
|
|
* |
223
|
|
|
* @return static |
224
|
|
|
*/ |
225
|
20 |
|
public function even(): self |
226
|
|
|
{ |
227
|
20 |
|
return $this->check(NumericCheckFactory::getEvenCheck()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritDoc} |
232
|
|
|
* |
233
|
|
|
* @return static |
234
|
|
|
*/ |
235
|
22 |
|
public function odd(): self |
236
|
|
|
{ |
237
|
22 |
|
return $this->check(NumericCheckFactory::getOddCheck()); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritDoc} |
242
|
|
|
* |
243
|
|
|
* @return static |
244
|
|
|
*/ |
245
|
2 |
|
public function nan(): self |
246
|
|
|
{ |
247
|
2 |
|
return $this->check(NumericCheckFactory::getNanCheck()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritDoc} |
252
|
|
|
* |
253
|
|
|
* @return static |
254
|
|
|
*/ |
255
|
2 |
|
public function notNan(): self |
256
|
|
|
{ |
257
|
2 |
|
return $this->check(NumericCheckFactory::getNotNanCheck()); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|