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\NumericRuleInterface; |
9
|
|
|
use Smoren\Validator\Structs\CheckName; |
10
|
|
|
use Smoren\Validator\Structs\Param; |
11
|
|
|
|
12
|
|
|
class NumericMixedRule extends MixedRule implements NumericRuleInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $name |
16
|
|
|
*/ |
17
|
50 |
|
public function __construct(string $name) |
18
|
|
|
{ |
19
|
50 |
|
parent::__construct($name); |
20
|
|
|
|
21
|
50 |
|
$this->check( |
22
|
50 |
|
CheckBuilder::create(CheckName::NUMERIC) |
23
|
50 |
|
->withPredicate(fn ($value) => \is_numeric($value)) |
24
|
50 |
|
->build(), |
25
|
50 |
|
true |
26
|
50 |
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
|
|
*/ |
34
|
2 |
|
public function number(): self |
35
|
|
|
{ |
36
|
2 |
|
return $this->check( |
37
|
2 |
|
CheckBuilder::create(CheckName::NUMBER) |
38
|
2 |
|
->withPredicate(fn ($value) => \is_int($value) || \is_float($value)) |
39
|
2 |
|
->build() |
40
|
2 |
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
2 |
|
public function string(): self |
49
|
|
|
{ |
50
|
2 |
|
return $this->check( |
51
|
2 |
|
CheckBuilder::create(CheckName::STRING) |
52
|
2 |
|
->withPredicate(fn ($value) => \is_string($value)) |
53
|
2 |
|
->build() |
54
|
2 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
* |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
6 |
|
public function truthy(): self |
63
|
|
|
{ |
64
|
6 |
|
return $this->check( |
65
|
6 |
|
CheckBuilder::create(CheckName::TRUTHY) |
66
|
6 |
|
->withPredicate(fn ($value) => \boolval(floatval($value))) |
67
|
6 |
|
->build() |
68
|
6 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
* |
74
|
|
|
* @return static |
75
|
|
|
*/ |
76
|
6 |
|
public function falsy(): self |
77
|
|
|
{ |
78
|
6 |
|
return $this->check( |
79
|
6 |
|
CheckBuilder::create(CheckName::FALSY) |
80
|
6 |
|
->withPredicate(fn ($value) => !\boolval(floatval($value))) |
81
|
6 |
|
->build() |
82
|
6 |
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
* |
88
|
|
|
* @return static |
89
|
|
|
*/ |
90
|
33 |
|
public function positive(): self |
91
|
|
|
{ |
92
|
33 |
|
return $this->check( |
93
|
33 |
|
CheckBuilder::create(CheckName::POSITIVE) |
94
|
33 |
|
->withPredicate(fn ($value) => $value > 0) |
95
|
33 |
|
->build() |
96
|
33 |
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
* |
102
|
|
|
* @return static |
103
|
|
|
*/ |
104
|
6 |
|
public function nonPositive(): self |
105
|
|
|
{ |
106
|
6 |
|
return $this->check( |
107
|
6 |
|
CheckBuilder::create(CheckName::NON_POSITIVE) |
108
|
6 |
|
->withPredicate(fn ($value) => $value <= 0) |
109
|
6 |
|
->build() |
110
|
6 |
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
* |
116
|
|
|
* @return static |
117
|
|
|
*/ |
118
|
11 |
|
public function nonNegative(): self |
119
|
|
|
{ |
120
|
11 |
|
return $this->check( |
121
|
11 |
|
CheckBuilder::create(CheckName::NON_NEGATIVE) |
122
|
11 |
|
->withPredicate(fn ($value) => $value >= 0) |
123
|
11 |
|
->build() |
124
|
11 |
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritDoc} |
129
|
|
|
* |
130
|
|
|
* @return static |
131
|
|
|
*/ |
132
|
6 |
|
public function negative(): self |
133
|
|
|
{ |
134
|
6 |
|
return $this->check( |
135
|
6 |
|
CheckBuilder::create(CheckName::NEGATIVE) |
136
|
6 |
|
->withPredicate(fn ($value) => $value < 0) |
137
|
6 |
|
->build() |
138
|
6 |
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
* |
144
|
|
|
* @return static |
145
|
|
|
*/ |
146
|
11 |
|
public function greaterTran($number): NumericRuleInterface |
147
|
|
|
{ |
148
|
11 |
|
return $this->check( |
149
|
11 |
|
CheckBuilder::create(CheckName::GREATER) |
150
|
11 |
|
->withPredicate(fn ($value, $number) => $value > $number) |
151
|
11 |
|
->withParams([Param::EXPECTED => $number]) |
152
|
11 |
|
->build() |
153
|
11 |
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
* |
159
|
|
|
* @return static |
160
|
|
|
*/ |
161
|
6 |
|
public function greaterOrEqual($number): NumericRuleInterface |
162
|
|
|
{ |
163
|
6 |
|
return $this->check( |
164
|
6 |
|
CheckBuilder::create(CheckName::GREATER_OR_EQUEAL) |
165
|
6 |
|
->withPredicate(fn ($value, $number) => $value >= $number) |
166
|
6 |
|
->withParams([Param::EXPECTED => $number]) |
167
|
6 |
|
->build() |
168
|
6 |
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritDoc} |
173
|
|
|
* |
174
|
|
|
* @return static |
175
|
|
|
*/ |
176
|
13 |
|
public function lessTran($number): NumericRuleInterface |
177
|
|
|
{ |
178
|
13 |
|
return $this->check( |
179
|
13 |
|
CheckBuilder::create(CheckName::LESS) |
180
|
13 |
|
->withPredicate(fn ($value, $number) => $value < $number) |
181
|
13 |
|
->withParams([Param::EXPECTED => $number]) |
182
|
13 |
|
->build() |
183
|
13 |
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritDoc} |
188
|
|
|
* |
189
|
|
|
* @return static |
190
|
|
|
*/ |
191
|
6 |
|
public function lessOrEqual($number): NumericRuleInterface |
192
|
|
|
{ |
193
|
6 |
|
return $this->check( |
194
|
6 |
|
CheckBuilder::create(CheckName::LESS_OR_EQUEAL) |
195
|
6 |
|
->withPredicate(fn ($value, $number) => $value <= $number) |
196
|
6 |
|
->withParams([Param::EXPECTED => $number]) |
197
|
6 |
|
->build() |
198
|
6 |
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritDoc} |
203
|
|
|
* |
204
|
|
|
* @return static |
205
|
|
|
*/ |
206
|
11 |
|
public function between($start, $end): self |
207
|
|
|
{ |
208
|
11 |
|
return $this->check( |
209
|
11 |
|
CheckBuilder::create(CheckName::BETWEEN) |
210
|
11 |
|
->withPredicate(fn ($value, $start, $end) => $value >= $start && $value <= $end) |
211
|
11 |
|
->withParams(['start' => $start, 'end' => $end]) |
212
|
11 |
|
->build() |
213
|
11 |
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritDoc} |
218
|
|
|
*/ |
219
|
11 |
|
public function inInterval($start, $end): self |
220
|
|
|
{ |
221
|
11 |
|
return $this->check( |
222
|
11 |
|
CheckBuilder::create(CheckName::IN_INTERVAL) |
223
|
11 |
|
->withPredicate(fn ($value, $start, $end) => $value > $start && $value < $end) |
224
|
11 |
|
->withParams(['start' => $start, 'end' => $end]) |
225
|
11 |
|
->build() |
226
|
11 |
|
); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|