1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Factories\Checks; |
6
|
|
|
|
7
|
|
|
use Smoren\Validator\Factories\CheckBuilder; |
8
|
|
|
use Smoren\Validator\Interfaces\CheckInterface; |
9
|
|
|
use Smoren\Validator\Structs\CheckName; |
10
|
|
|
use Smoren\Validator\Structs\Param; |
11
|
|
|
|
12
|
|
|
final class NumericCheckFactory |
13
|
|
|
{ |
14
|
106 |
|
public static function getNumericCheck(): CheckInterface |
15
|
|
|
{ |
16
|
106 |
|
return CheckBuilder::create(CheckName::NUMERIC) |
17
|
106 |
|
->withPredicate(fn ($value) => \is_numeric($value)) |
18
|
106 |
|
->build(); |
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
public static function getNumberCheck(): CheckInterface |
22
|
|
|
{ |
23
|
2 |
|
return CheckBuilder::create(CheckName::NUMBER) |
24
|
2 |
|
->withPredicate(fn ($value) => \is_int($value) || \is_float($value)) |
25
|
2 |
|
->build(); |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
public static function getStringCheck(): CheckInterface |
29
|
|
|
{ |
30
|
2 |
|
return CheckBuilder::create(CheckName::STRING) |
31
|
2 |
|
->withPredicate(fn ($value) => \is_string($value)) |
32
|
2 |
|
->build(); |
33
|
|
|
} |
34
|
|
|
|
35
|
71 |
|
public static function getIntegerCheck(): CheckInterface |
36
|
|
|
{ |
37
|
71 |
|
return CheckBuilder::create(CheckName::INTEGER) |
38
|
71 |
|
->withPredicate(fn ($value) => \is_int($value)) |
39
|
71 |
|
->build(); |
40
|
|
|
} |
41
|
|
|
|
42
|
49 |
|
public static function getFloatCheck(): CheckInterface |
43
|
|
|
{ |
44
|
49 |
|
return CheckBuilder::create(CheckName::FLOAT) |
45
|
49 |
|
->withPredicate(fn ($value) => \is_float($value)) |
46
|
49 |
|
->build(); |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
public static function getTruthyCheck(): CheckInterface |
50
|
|
|
{ |
51
|
6 |
|
return CheckBuilder::create(CheckName::TRUTHY) |
52
|
6 |
|
->withPredicate(fn ($value) => \boolval(floatval($value))) |
53
|
6 |
|
->build(); |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
public static function getFalsyCheck(): CheckInterface |
57
|
|
|
{ |
58
|
6 |
|
return CheckBuilder::create(CheckName::FALSY) |
59
|
6 |
|
->withPredicate(fn ($value) => !\boolval(floatval($value))) |
60
|
6 |
|
->build(); |
61
|
|
|
} |
62
|
|
|
|
63
|
62 |
|
public static function getPositiveCheck(): CheckInterface |
64
|
|
|
{ |
65
|
62 |
|
return CheckBuilder::create(CheckName::POSITIVE) |
66
|
62 |
|
->withPredicate(fn ($value) => $value > 0) |
67
|
62 |
|
->build(); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
public static function getNonPositiveCheck(): CheckInterface |
71
|
|
|
{ |
72
|
6 |
|
return CheckBuilder::create(CheckName::NON_POSITIVE) |
73
|
6 |
|
->withPredicate(fn ($value) => $value <= 0) |
74
|
6 |
|
->build(); |
75
|
|
|
} |
76
|
|
|
|
77
|
13 |
|
public static function getNonNegativeCheck(): CheckInterface |
78
|
|
|
{ |
79
|
13 |
|
return CheckBuilder::create(CheckName::NON_NEGATIVE) |
80
|
13 |
|
->withPredicate(fn ($value) => $value >= 0) |
81
|
13 |
|
->build(); |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
public static function getNegativeCheck(): CheckInterface |
85
|
|
|
{ |
86
|
6 |
|
return CheckBuilder::create(CheckName::NEGATIVE) |
87
|
6 |
|
->withPredicate(fn ($value) => $value < 0) |
88
|
6 |
|
->build(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param numeric $number |
93
|
|
|
* @return CheckInterface |
94
|
|
|
*/ |
95
|
16 |
|
public static function getGreaterThanCheck($number): CheckInterface |
96
|
|
|
{ |
97
|
16 |
|
return CheckBuilder::create(CheckName::GREATER) |
98
|
16 |
|
->withPredicate(fn ($value, $number) => $value > $number) |
99
|
16 |
|
->withParams([Param::EXPECTED => $number]) |
100
|
16 |
|
->build(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param numeric $number |
105
|
|
|
* @return CheckInterface |
106
|
|
|
*/ |
107
|
6 |
|
public static function getGreaterOrEqualCheck($number): CheckInterface |
108
|
|
|
{ |
109
|
6 |
|
return CheckBuilder::create(CheckName::GREATER_OR_EQUEAL) |
110
|
6 |
|
->withPredicate(fn ($value, $number) => $value >= $number) |
111
|
6 |
|
->withParams([Param::EXPECTED => $number]) |
112
|
6 |
|
->build(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param numeric $number |
117
|
|
|
* @return CheckInterface |
118
|
|
|
*/ |
119
|
18 |
|
public static function getLessThanCheck($number): CheckInterface |
120
|
|
|
{ |
121
|
18 |
|
return CheckBuilder::create(CheckName::LESS) |
122
|
18 |
|
->withPredicate(fn ($value, $number) => $value < $number) |
123
|
18 |
|
->withParams([Param::EXPECTED => $number]) |
124
|
18 |
|
->build(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param numeric $number |
129
|
|
|
* @return CheckInterface |
130
|
|
|
*/ |
131
|
6 |
|
public static function getLessOrEqualCheck($number): CheckInterface |
132
|
|
|
{ |
133
|
6 |
|
return CheckBuilder::create(CheckName::LESS_OR_EQUEAL) |
134
|
6 |
|
->withPredicate(fn ($value, $number) => $value <= $number) |
135
|
6 |
|
->withParams([Param::EXPECTED => $number]) |
136
|
6 |
|
->build(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param numeric $start |
141
|
|
|
* @param numeric $end |
142
|
|
|
* @return CheckInterface |
143
|
|
|
*/ |
144
|
27 |
|
public static function getBetweenCheck($start, $end): CheckInterface |
145
|
|
|
{ |
146
|
27 |
|
return CheckBuilder::create(CheckName::BETWEEN) |
147
|
27 |
|
->withPredicate(fn ($value, $start, $end) => $value >= $start && $value <= $end) |
148
|
27 |
|
->withParams(['start' => $start, 'end' => $end]) |
149
|
27 |
|
->build(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param numeric $start |
154
|
|
|
* @param numeric $end |
155
|
|
|
* @return CheckInterface |
156
|
|
|
*/ |
157
|
24 |
|
public static function getInOpenIntervalCheck($start, $end): CheckInterface |
158
|
|
|
{ |
159
|
24 |
|
return CheckBuilder::create(CheckName::IN_OPEN_INTERVAL) |
160
|
24 |
|
->withPredicate(fn ($value, $start, $end) => $value > $start && $value < $end) |
161
|
24 |
|
->withParams(['start' => $start, 'end' => $end]) |
162
|
24 |
|
->build(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param numeric $start |
167
|
|
|
* @param numeric $end |
168
|
|
|
* @return CheckInterface |
169
|
|
|
*/ |
170
|
2 |
|
public static function getInLeftOpenIntervalCheck($start, $end): CheckInterface |
171
|
|
|
{ |
172
|
2 |
|
return CheckBuilder::create(CheckName::IN_LEFT_HALF_OPEN_INTERVAL) |
173
|
2 |
|
->withPredicate(fn ($value, $start, $end) => $value > $start && $value <= $end) |
174
|
2 |
|
->withParams(['start' => $start, 'end' => $end]) |
175
|
2 |
|
->build(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param numeric $start |
180
|
|
|
* @param numeric $end |
181
|
|
|
* @return CheckInterface |
182
|
|
|
*/ |
183
|
2 |
|
public static function getInRightOpenIntervalCheck($start, $end): CheckInterface |
184
|
|
|
{ |
185
|
2 |
|
return CheckBuilder::create(CheckName::IN_RIGHT_HALF_OPEN_INTERVAL) |
186
|
2 |
|
->withPredicate(fn ($value, $start, $end) => $value >= $start && $value < $end) |
187
|
2 |
|
->withParams(['start' => $start, 'end' => $end]) |
188
|
2 |
|
->build(); |
189
|
|
|
} |
190
|
|
|
|
191
|
4 |
|
public static function getFractionalCheck(): CheckInterface |
192
|
|
|
{ |
193
|
4 |
|
return CheckBuilder::create(CheckName::FRACTIONAL) |
194
|
4 |
|
->withPredicate(fn ($value) => \abs($value - \round(\floatval($value))) >= \PHP_FLOAT_EPSILON) |
195
|
4 |
|
->build(); |
196
|
|
|
} |
197
|
|
|
|
198
|
7 |
|
public static function getNonFractionalCheck(): CheckInterface |
199
|
|
|
{ |
200
|
7 |
|
return CheckBuilder::create(CheckName::NON_FRACTIONAL) |
201
|
7 |
|
->withPredicate(fn ($value) => \abs($value - \round(\floatval($value))) < \PHP_FLOAT_EPSILON) |
202
|
7 |
|
->build(); |
203
|
|
|
} |
204
|
|
|
|
205
|
4 |
|
public static function getFiniteCheck(): CheckInterface |
206
|
|
|
{ |
207
|
4 |
|
return CheckBuilder::create(CheckName::FINITE) |
208
|
4 |
|
->withPredicate(fn ($value) => $value > -INF && $value < INF) |
209
|
4 |
|
->build(); |
210
|
|
|
} |
211
|
|
|
|
212
|
4 |
|
public static function getInfiniteCheck(): CheckInterface |
213
|
|
|
{ |
214
|
4 |
|
return CheckBuilder::create(CheckName::INFINITE) |
215
|
4 |
|
->withPredicate(fn ($value) => $value === -INF || $value === INF) |
216
|
4 |
|
->build(); |
217
|
|
|
} |
218
|
|
|
|
219
|
20 |
|
public static function getEvenCheck(): CheckInterface |
220
|
|
|
{ |
221
|
20 |
|
return CheckBuilder::create(CheckName::EVEN) |
222
|
20 |
|
->withPredicate(fn ($value) => $value % 2 === 0) |
223
|
20 |
|
->build(); |
224
|
|
|
} |
225
|
|
|
|
226
|
22 |
|
public static function getOddCheck(): CheckInterface |
227
|
|
|
{ |
228
|
22 |
|
return CheckBuilder::create(CheckName::ODD) |
229
|
22 |
|
->withPredicate(fn ($value) => $value % 2 !== 0) |
230
|
22 |
|
->build(); |
231
|
|
|
} |
232
|
|
|
|
233
|
2 |
|
public static function getNanCheck(): CheckInterface |
234
|
|
|
{ |
235
|
2 |
|
return CheckBuilder::create(CheckName::NAN) |
236
|
2 |
|
->withPredicate(fn ($value) => \is_nan(\floatval($value))) |
237
|
2 |
|
->build(); |
238
|
|
|
} |
239
|
|
|
|
240
|
2 |
|
public static function getNotNanCheck(): CheckInterface |
241
|
|
|
{ |
242
|
2 |
|
return CheckBuilder::create(CheckName::NOT_NAN) |
243
|
2 |
|
->withPredicate(fn ($value) => !\is_nan(\floatval($value))) |
244
|
2 |
|
->build(); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|