1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Rules; |
6
|
|
|
|
7
|
|
|
use Smoren\Validator\Checks\Check; |
8
|
|
|
use Smoren\Validator\Interfaces\NumericRuleInterface; |
9
|
|
|
|
10
|
|
|
class NumericRule extends Rule implements NumericRuleInterface |
11
|
|
|
{ |
12
|
|
|
public const ERROR_NOT_NUMERIC = 'not_numeric'; |
13
|
|
|
public const ERROR_NOT_NUMBER = 'not_number'; |
14
|
|
|
public const ERROR_NOT_STRING = 'not_string'; |
15
|
|
|
public const ERROR_NOT_POSITIVE = 'not_positive'; |
16
|
|
|
public const ERROR_NOT_NON_POSITIVE = 'not_non_positive'; |
17
|
|
|
public const ERROR_NOT_NON_NEGATIVE = 'not_non_negative'; |
18
|
|
|
public const ERROR_NOT_NEGATIVE = 'not_negative'; |
19
|
|
|
public const ERROR_NOT_GREATER = 'not_greater'; |
20
|
|
|
public const ERROR_NOT_EQUEAL = 'equal'; |
21
|
|
|
public const ERROR_NOT_SAME = 'same'; |
22
|
|
|
public const ERROR_NOT_GREATER_OR_EQUEAL = 'not_greater_or_equal'; |
23
|
|
|
public const ERROR_NOT_LESS = 'not_less'; |
24
|
|
|
public const ERROR_NOT_LESS_OR_EQUEAL = 'not_less_or_equal'; |
25
|
|
|
public const ERROR_NOT_IN_SEGMENT = 'not_in_segment'; |
26
|
|
|
public const ERROR_NOT_IN_INTERVAL = 'not_in_interval'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* NumericRule constructor. |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct() |
32
|
|
|
{ |
33
|
2 |
|
$this->check(new Check( |
34
|
2 |
|
self::ERROR_NOT_NUMERIC, |
35
|
2 |
|
fn ($value) => is_numeric($value), |
36
|
2 |
|
[] |
37
|
2 |
|
), true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
*/ |
45
|
|
|
public function number(): self |
46
|
|
|
{ |
47
|
|
|
return $this->check(new Check( |
48
|
|
|
self::ERROR_NOT_NUMBER, |
49
|
|
|
fn ($value) => is_int($value) || is_float($value) |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
* |
56
|
|
|
* @return static |
57
|
|
|
*/ |
58
|
|
|
public function string(): self |
59
|
|
|
{ |
60
|
|
|
return $this->check(new Check( |
61
|
|
|
self::ERROR_NOT_STRING, |
62
|
|
|
fn ($value) => is_string($value) |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
23 |
|
public function positive(): self |
72
|
|
|
{ |
73
|
23 |
|
return $this->check(new Check( |
74
|
23 |
|
self::ERROR_NOT_POSITIVE, |
75
|
23 |
|
fn ($value) => $value > 0 |
76
|
23 |
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
2 |
|
public function nonPositive(): self |
85
|
|
|
{ |
86
|
2 |
|
return $this->check(new Check( |
87
|
2 |
|
self::ERROR_NOT_NON_POSITIVE, |
88
|
2 |
|
fn ($value) => $value <= 0 |
89
|
2 |
|
)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
* |
95
|
|
|
* @return static |
96
|
|
|
*/ |
97
|
3 |
|
public function nonNegative(): self |
98
|
|
|
{ |
99
|
3 |
|
return $this->check(new Check( |
100
|
3 |
|
self::ERROR_NOT_NON_NEGATIVE, |
101
|
3 |
|
fn ($value) => $value >= 0 |
102
|
3 |
|
)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
* |
108
|
|
|
* @return static |
109
|
|
|
*/ |
110
|
2 |
|
public function negative(): self |
111
|
|
|
{ |
112
|
2 |
|
return $this->check(new Check( |
113
|
2 |
|
self::ERROR_NOT_NEGATIVE, |
114
|
2 |
|
fn ($value) => $value < 0 |
115
|
2 |
|
)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public function equal($number): NumericRuleInterface |
124
|
|
|
{ |
125
|
|
|
return $this->check(new Check( |
126
|
|
|
self::ERROR_NOT_EQUEAL, |
127
|
|
|
fn ($value) => $value == $number, |
128
|
|
|
['number' => $number] |
129
|
|
|
)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
* |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
|
|
public function same($number): NumericRuleInterface |
138
|
|
|
{ |
139
|
|
|
return $this->check(new Check( |
140
|
|
|
self::ERROR_NOT_SAME, |
141
|
|
|
fn ($value) => $value === $number, |
142
|
|
|
['number' => $number] |
143
|
|
|
)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
* |
149
|
|
|
* @return static |
150
|
|
|
*/ |
151
|
2 |
|
public function greaterTran($number): NumericRuleInterface |
152
|
|
|
{ |
153
|
2 |
|
return $this->check(new Check( |
154
|
2 |
|
self::ERROR_NOT_GREATER, |
155
|
2 |
|
fn ($value) => $value > $number, |
156
|
2 |
|
['number' => $number] |
157
|
2 |
|
)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritDoc} |
162
|
|
|
* |
163
|
|
|
* @return static |
164
|
|
|
*/ |
165
|
2 |
|
public function greaterOrEqual($number): NumericRuleInterface |
166
|
|
|
{ |
167
|
2 |
|
return $this->check(new Check( |
168
|
2 |
|
self::ERROR_NOT_GREATER_OR_EQUEAL, |
169
|
2 |
|
fn ($value) => $value >= $number, |
170
|
2 |
|
['number' => $number] |
171
|
2 |
|
)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
* |
177
|
|
|
* @return static |
178
|
|
|
*/ |
179
|
2 |
|
public function lessTran($number): NumericRuleInterface |
180
|
|
|
{ |
181
|
2 |
|
return $this->check(new Check( |
182
|
2 |
|
self::ERROR_NOT_LESS, |
183
|
2 |
|
fn ($value) => $value < $number, |
184
|
2 |
|
['number' => $number] |
185
|
2 |
|
)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritDoc} |
190
|
|
|
* |
191
|
|
|
* @return static |
192
|
|
|
*/ |
193
|
2 |
|
public function lessOrEqual($number): NumericRuleInterface |
194
|
|
|
{ |
195
|
2 |
|
return $this->check(new Check( |
196
|
2 |
|
self::ERROR_NOT_LESS_OR_EQUEAL, |
197
|
2 |
|
fn ($value) => $value <= $number, |
198
|
2 |
|
['number' => $number] |
199
|
2 |
|
)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritDoc} |
204
|
|
|
* |
205
|
|
|
* @return static |
206
|
|
|
*/ |
207
|
8 |
|
public function between($start, $end): self |
208
|
|
|
{ |
209
|
8 |
|
return $this->check(new Check( |
210
|
8 |
|
self::ERROR_NOT_IN_SEGMENT, |
211
|
8 |
|
fn ($value) => $value >= $start && $value <= $end, |
212
|
8 |
|
['start' => $start, 'end' => $end] |
213
|
8 |
|
)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritDoc} |
218
|
|
|
*/ |
219
|
7 |
|
public function inInterval($start, $end): self |
220
|
|
|
{ |
221
|
7 |
|
return $this->check(new Check( |
222
|
7 |
|
self::ERROR_NOT_IN_INTERVAL, |
223
|
7 |
|
fn ($value) => $value > $start && $value < $end, |
224
|
7 |
|
['start' => $start, 'end' => $end] |
225
|
7 |
|
)); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|