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