1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Rules; |
6
|
|
|
|
7
|
|
|
use Smoren\Validator\Factories\CheckBuilder; |
8
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
9
|
|
|
use Smoren\Validator\Interfaces\NumericRuleInterface; |
10
|
|
|
use Smoren\Validator\Structs\CheckName; |
11
|
|
|
use Smoren\Validator\Structs\Param; |
12
|
|
|
|
13
|
|
|
class NumericRule extends MixedRule implements NumericRuleInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param string $name |
17
|
|
|
*/ |
18
|
83 |
|
public function __construct(string $name) |
19
|
|
|
{ |
20
|
83 |
|
parent::__construct($name); |
21
|
|
|
|
22
|
83 |
|
$this->check( |
23
|
83 |
|
$this->getNumericCheck(), |
24
|
83 |
|
true |
25
|
83 |
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritDoc} |
30
|
|
|
* |
31
|
|
|
* @return static |
32
|
|
|
*/ |
33
|
2 |
|
public function number(bool $stopOnViolation = true): self |
34
|
|
|
{ |
35
|
2 |
|
return $this->check( |
36
|
2 |
|
$this->getNumberCheck(), |
37
|
2 |
|
$stopOnViolation |
38
|
2 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
* |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
2 |
|
public function string(bool $stopOnViolation = true): self |
47
|
|
|
{ |
48
|
2 |
|
return $this->check( |
49
|
2 |
|
$this->getStringCheck(), |
50
|
2 |
|
$stopOnViolation |
51
|
2 |
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
* |
57
|
|
|
* @return static |
58
|
|
|
*/ |
59
|
69 |
|
public function integer(bool $stopOnViolation = true): self |
60
|
|
|
{ |
61
|
69 |
|
return $this->check( |
62
|
69 |
|
$this->getIntegerCheck(), |
63
|
69 |
|
$stopOnViolation |
64
|
69 |
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
* |
70
|
|
|
* @return static |
71
|
|
|
*/ |
72
|
48 |
|
public function float(bool $stopOnViolation = true): self |
73
|
|
|
{ |
74
|
48 |
|
return $this->check( |
75
|
48 |
|
$this->getFloatCheck(), |
76
|
48 |
|
$stopOnViolation |
77
|
48 |
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
* |
83
|
|
|
* @return static |
84
|
|
|
*/ |
85
|
6 |
|
public function truthy(): self |
86
|
|
|
{ |
87
|
6 |
|
return $this->check( |
88
|
6 |
|
CheckBuilder::create(CheckName::TRUTHY) |
89
|
6 |
|
->withPredicate(fn ($value) => \boolval(floatval($value))) |
90
|
6 |
|
->build() |
91
|
6 |
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
* |
97
|
|
|
* @return static |
98
|
|
|
*/ |
99
|
6 |
|
public function falsy(): self |
100
|
|
|
{ |
101
|
6 |
|
return $this->check( |
102
|
6 |
|
CheckBuilder::create(CheckName::FALSY) |
103
|
6 |
|
->withPredicate(fn ($value) => !\boolval(floatval($value))) |
104
|
6 |
|
->build() |
105
|
6 |
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
* |
111
|
|
|
* @return static |
112
|
|
|
*/ |
113
|
56 |
|
public function positive(): self |
114
|
|
|
{ |
115
|
56 |
|
return $this->check( |
116
|
56 |
|
CheckBuilder::create(CheckName::POSITIVE) |
117
|
56 |
|
->withPredicate(fn ($value) => $value > 0) |
118
|
56 |
|
->build() |
119
|
56 |
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
* |
125
|
|
|
* @return static |
126
|
|
|
*/ |
127
|
6 |
|
public function nonPositive(): self |
128
|
|
|
{ |
129
|
6 |
|
return $this->check( |
130
|
6 |
|
CheckBuilder::create(CheckName::NON_POSITIVE) |
131
|
6 |
|
->withPredicate(fn ($value) => $value <= 0) |
132
|
6 |
|
->build() |
133
|
6 |
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
* |
139
|
|
|
* @return static |
140
|
|
|
*/ |
141
|
13 |
|
public function nonNegative(): self |
142
|
|
|
{ |
143
|
13 |
|
return $this->check( |
144
|
13 |
|
CheckBuilder::create(CheckName::NON_NEGATIVE) |
145
|
13 |
|
->withPredicate(fn ($value) => $value >= 0) |
146
|
13 |
|
->build() |
147
|
13 |
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
* |
153
|
|
|
* @return static |
154
|
|
|
*/ |
155
|
6 |
|
public function negative(): self |
156
|
|
|
{ |
157
|
6 |
|
return $this->check( |
158
|
6 |
|
CheckBuilder::create(CheckName::NEGATIVE) |
159
|
6 |
|
->withPredicate(fn ($value) => $value < 0) |
160
|
6 |
|
->build() |
161
|
6 |
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
* |
167
|
|
|
* @return static |
168
|
|
|
*/ |
169
|
11 |
|
public function greaterThan($number): NumericRuleInterface |
170
|
|
|
{ |
171
|
11 |
|
return $this->check( |
172
|
11 |
|
CheckBuilder::create(CheckName::GREATER) |
173
|
11 |
|
->withPredicate(fn ($value, $number) => $value > $number) |
174
|
11 |
|
->withParams([Param::EXPECTED => $number]) |
175
|
11 |
|
->build() |
176
|
11 |
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritDoc} |
181
|
|
|
* |
182
|
|
|
* @return static |
183
|
|
|
*/ |
184
|
6 |
|
public function greaterOrEqual($number): NumericRuleInterface |
185
|
|
|
{ |
186
|
6 |
|
return $this->check( |
187
|
6 |
|
CheckBuilder::create(CheckName::GREATER_OR_EQUEAL) |
188
|
6 |
|
->withPredicate(fn ($value, $number) => $value >= $number) |
189
|
6 |
|
->withParams([Param::EXPECTED => $number]) |
190
|
6 |
|
->build() |
191
|
6 |
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* {@inheritDoc} |
196
|
|
|
* |
197
|
|
|
* @return static |
198
|
|
|
*/ |
199
|
13 |
|
public function lessThan($number): NumericRuleInterface |
200
|
|
|
{ |
201
|
13 |
|
return $this->check( |
202
|
13 |
|
CheckBuilder::create(CheckName::LESS) |
203
|
13 |
|
->withPredicate(fn ($value, $number) => $value < $number) |
204
|
13 |
|
->withParams([Param::EXPECTED => $number]) |
205
|
13 |
|
->build() |
206
|
13 |
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritDoc} |
211
|
|
|
* |
212
|
|
|
* @return static |
213
|
|
|
*/ |
214
|
6 |
|
public function lessOrEqual($number): NumericRuleInterface |
215
|
|
|
{ |
216
|
6 |
|
return $this->check( |
217
|
6 |
|
CheckBuilder::create(CheckName::LESS_OR_EQUEAL) |
218
|
6 |
|
->withPredicate(fn ($value, $number) => $value <= $number) |
219
|
6 |
|
->withParams([Param::EXPECTED => $number]) |
220
|
6 |
|
->build() |
221
|
6 |
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritDoc} |
226
|
|
|
* |
227
|
|
|
* @return static |
228
|
|
|
*/ |
229
|
23 |
|
public function between($start, $end): self |
230
|
|
|
{ |
231
|
23 |
|
return $this->check( |
232
|
23 |
|
CheckBuilder::create(CheckName::BETWEEN) |
233
|
23 |
|
->withPredicate(fn ($value, $start, $end) => $value >= $start && $value <= $end) |
234
|
23 |
|
->withParams(['start' => $start, 'end' => $end]) |
235
|
23 |
|
->build() |
236
|
23 |
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* {@inheritDoc} |
241
|
|
|
*/ |
242
|
22 |
|
public function inInterval($start, $end): self |
243
|
|
|
{ |
244
|
22 |
|
return $this->check( |
245
|
22 |
|
CheckBuilder::create(CheckName::IN_INTERVAL) |
246
|
22 |
|
->withPredicate(fn ($value, $start, $end) => $value > $start && $value < $end) |
247
|
22 |
|
->withParams(['start' => $start, 'end' => $end]) |
248
|
22 |
|
->build() |
249
|
22 |
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritDoc} |
254
|
|
|
* |
255
|
|
|
* @return static |
256
|
|
|
*/ |
257
|
4 |
|
public function fractional(): self |
258
|
|
|
{ |
259
|
4 |
|
return $this->check( |
260
|
4 |
|
CheckBuilder::create(CheckName::FRACTIONAL) |
261
|
4 |
|
->withPredicate(fn ($value) => \abs($value - \round(\floatval($value))) >= \PHP_FLOAT_EPSILON) |
262
|
4 |
|
->build() |
263
|
4 |
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* {@inheritDoc} |
268
|
|
|
* |
269
|
|
|
* @return static |
270
|
|
|
*/ |
271
|
7 |
|
public function nonFractional(): self |
272
|
|
|
{ |
273
|
7 |
|
return $this->check( |
274
|
7 |
|
CheckBuilder::create(CheckName::NON_FRACTIONAL) |
275
|
7 |
|
->withPredicate(fn ($value) => \abs($value - \round(\floatval($value))) < \PHP_FLOAT_EPSILON) |
276
|
7 |
|
->build() |
277
|
7 |
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritDoc} |
282
|
|
|
* |
283
|
|
|
* @return static |
284
|
|
|
*/ |
285
|
4 |
|
public function finite(): self |
286
|
|
|
{ |
287
|
4 |
|
return $this->check( |
288
|
4 |
|
CheckBuilder::create(CheckName::FINITE) |
289
|
4 |
|
->withPredicate(fn ($value) => $value > -INF && $value < INF) |
290
|
4 |
|
->build() |
291
|
4 |
|
); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* {@inheritDoc} |
296
|
|
|
* |
297
|
|
|
* @return static |
298
|
|
|
*/ |
299
|
4 |
|
public function infinite(): self |
300
|
|
|
{ |
301
|
4 |
|
return $this->check( |
302
|
4 |
|
CheckBuilder::create(CheckName::INFINITE) |
303
|
4 |
|
->withPredicate(fn ($value) => $value === -INF || $value === INF) |
304
|
4 |
|
->build() |
305
|
4 |
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritDoc} |
310
|
|
|
* |
311
|
|
|
* @return static |
312
|
|
|
*/ |
313
|
20 |
|
public function even(): self |
314
|
|
|
{ |
315
|
20 |
|
return $this->check( |
316
|
20 |
|
CheckBuilder::create(CheckName::EVEN) |
317
|
20 |
|
->withPredicate(fn ($value) => $value % 2 === 0) |
318
|
20 |
|
->build() |
319
|
20 |
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* {@inheritDoc} |
324
|
|
|
* |
325
|
|
|
* @return static |
326
|
|
|
*/ |
327
|
22 |
|
public function odd(): self |
328
|
|
|
{ |
329
|
22 |
|
return $this->check( |
330
|
22 |
|
CheckBuilder::create(CheckName::ODD) |
331
|
22 |
|
->withPredicate(fn ($value) => $value % 2 !== 0) |
332
|
22 |
|
->build() |
333
|
22 |
|
); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* {@inheritDoc} |
338
|
|
|
* |
339
|
|
|
* @return static |
340
|
|
|
*/ |
341
|
2 |
|
public function nan(): self |
342
|
|
|
{ |
343
|
2 |
|
return $this->check( |
344
|
2 |
|
CheckBuilder::create(CheckName::NAN) |
345
|
2 |
|
->withPredicate(fn ($value) => \is_nan(\floatval($value))) |
346
|
2 |
|
->withDependOnChecks([$this->getNumericCheck()]) |
347
|
2 |
|
->build() |
348
|
2 |
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* {@inheritDoc} |
353
|
|
|
* |
354
|
|
|
* @return static |
355
|
|
|
*/ |
356
|
2 |
|
public function notNan(): self |
357
|
|
|
{ |
358
|
2 |
|
return $this->check( |
359
|
2 |
|
CheckBuilder::create(CheckName::NOT_NAN) |
360
|
2 |
|
->withPredicate(fn ($value) => !\is_nan(\floatval($value))) |
361
|
2 |
|
->withDependOnChecks([$this->getNumericCheck()]) |
362
|
2 |
|
->build() |
363
|
2 |
|
); |
364
|
|
|
} |
365
|
|
|
|
366
|
83 |
|
protected function getNumericCheck(): CheckInterface |
367
|
|
|
{ |
368
|
83 |
|
return CheckBuilder::create(CheckName::NUMERIC) |
369
|
83 |
|
->withPredicate(fn ($value) => \is_numeric($value)) |
370
|
83 |
|
->build(); |
371
|
|
|
} |
372
|
|
|
|
373
|
2 |
|
protected function getNumberCheck(): CheckInterface |
374
|
|
|
{ |
375
|
2 |
|
return CheckBuilder::create(CheckName::NUMBER) |
376
|
2 |
|
->withPredicate(fn ($value) => \is_int($value) || \is_float($value)) |
377
|
2 |
|
->build(); |
378
|
|
|
} |
379
|
|
|
|
380
|
2 |
|
protected function getStringCheck(): CheckInterface |
381
|
|
|
{ |
382
|
2 |
|
return CheckBuilder::create(CheckName::STRING) |
383
|
2 |
|
->withPredicate(fn ($value) => \is_string($value)) |
384
|
2 |
|
->build(); |
385
|
|
|
} |
386
|
|
|
|
387
|
69 |
|
protected function getIntegerCheck(): CheckInterface |
388
|
|
|
{ |
389
|
69 |
|
return CheckBuilder::create(CheckName::INTEGER) |
390
|
69 |
|
->withPredicate(fn ($value) => \is_int($value)) |
391
|
69 |
|
->build(); |
392
|
|
|
} |
393
|
|
|
|
394
|
48 |
|
protected function getFloatCheck(): CheckInterface |
395
|
|
|
{ |
396
|
48 |
|
return CheckBuilder::create(CheckName::FLOAT) |
397
|
48 |
|
->withPredicate(fn ($value) => \is_float($value)) |
398
|
48 |
|
->build(); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|