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_GREATER_OR_EQUEAL = 'not_greater_or_equal'; |
21
|
|
|
public const ERROR_NOT_LESS = 'not_less'; |
22
|
|
|
public const ERROR_NOT_LESS_OR_EQUEAL = 'not_less_or_equal'; |
23
|
|
|
public const ERROR_NOT_IN_SEGMENT = 'not_in_segment'; |
24
|
|
|
public const ERROR_NOT_IN_INTERVAL = 'not_in_interval'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* NumericRule constructor. |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct() |
30
|
|
|
{ |
31
|
2 |
|
$this->check(new Check( |
32
|
2 |
|
self::ERROR_NOT_NUMERIC, |
33
|
2 |
|
fn ($value) => is_numeric($value), |
34
|
2 |
|
[] |
35
|
2 |
|
), true); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
* |
41
|
|
|
* @return static |
42
|
|
|
*/ |
43
|
|
|
public function number(): self |
44
|
|
|
{ |
45
|
|
|
return $this->check(new Check( |
46
|
|
|
self::ERROR_NOT_NUMBER, |
47
|
|
|
fn ($value) => is_int($value) || is_float($value) |
48
|
|
|
)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
* |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
|
public function string(): self |
57
|
|
|
{ |
58
|
|
|
return $this->check(new Check( |
59
|
|
|
self::ERROR_NOT_STRING, |
60
|
|
|
fn ($value) => is_string($value) |
61
|
|
|
)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
* |
67
|
|
|
* @return static |
68
|
|
|
*/ |
69
|
23 |
|
public function positive(): self |
70
|
|
|
{ |
71
|
23 |
|
return $this->check(new Check( |
72
|
23 |
|
self::ERROR_NOT_POSITIVE, |
73
|
23 |
|
fn ($value) => $value > 0 |
74
|
23 |
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
* |
80
|
|
|
* @return static |
81
|
|
|
*/ |
82
|
2 |
|
public function nonPositive(): self |
83
|
|
|
{ |
84
|
2 |
|
return $this->check(new Check( |
85
|
2 |
|
self::ERROR_NOT_NON_POSITIVE, |
86
|
2 |
|
fn ($value) => $value <= 0 |
87
|
2 |
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
* |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
3 |
|
public function nonNegative(): self |
96
|
|
|
{ |
97
|
3 |
|
return $this->check(new Check( |
98
|
3 |
|
self::ERROR_NOT_NON_NEGATIVE, |
99
|
3 |
|
fn ($value) => $value >= 0 |
100
|
3 |
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
* |
106
|
|
|
* @return static |
107
|
|
|
*/ |
108
|
2 |
|
public function negative(): self |
109
|
|
|
{ |
110
|
2 |
|
return $this->check(new Check( |
111
|
2 |
|
self::ERROR_NOT_NEGATIVE, |
112
|
2 |
|
fn ($value) => $value < 0 |
113
|
2 |
|
)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
* |
119
|
|
|
* @return static |
120
|
|
|
*/ |
121
|
2 |
|
public function greaterTran($number): NumericRuleInterface |
122
|
|
|
{ |
123
|
2 |
|
return $this->check(new Check( |
124
|
2 |
|
self::ERROR_NOT_GREATER, |
125
|
2 |
|
fn ($value) => $value > $number, |
126
|
2 |
|
['number' => $number] |
127
|
2 |
|
)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
* |
133
|
|
|
* @return static |
134
|
|
|
*/ |
135
|
2 |
|
public function greaterOrEqual($number): NumericRuleInterface |
136
|
|
|
{ |
137
|
2 |
|
return $this->check(new Check( |
138
|
2 |
|
self::ERROR_NOT_GREATER_OR_EQUEAL, |
139
|
2 |
|
fn ($value) => $value >= $number, |
140
|
2 |
|
['number' => $number] |
141
|
2 |
|
)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritDoc} |
146
|
|
|
* |
147
|
|
|
* @return static |
148
|
|
|
*/ |
149
|
2 |
|
public function lessTran($number): NumericRuleInterface |
150
|
|
|
{ |
151
|
2 |
|
return $this->check(new Check( |
152
|
2 |
|
self::ERROR_NOT_LESS, |
153
|
2 |
|
fn ($value) => $value < $number, |
154
|
2 |
|
['number' => $number] |
155
|
2 |
|
)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
* |
161
|
|
|
* @return static |
162
|
|
|
*/ |
163
|
2 |
|
public function lessOrEqual($number): NumericRuleInterface |
164
|
|
|
{ |
165
|
2 |
|
return $this->check(new Check( |
166
|
2 |
|
self::ERROR_NOT_LESS_OR_EQUEAL, |
167
|
2 |
|
fn ($value) => $value <= $number, |
168
|
2 |
|
['number' => $number] |
169
|
2 |
|
)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
* |
175
|
|
|
* @return static |
176
|
|
|
*/ |
177
|
8 |
|
public function between($start, $end): self |
178
|
|
|
{ |
179
|
8 |
|
return $this->check(new Check( |
180
|
8 |
|
self::ERROR_NOT_IN_SEGMENT, |
181
|
8 |
|
fn ($value) => $value >= $start && $value <= $end, |
182
|
8 |
|
['start' => $start, 'end' => $end] |
183
|
8 |
|
)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritDoc} |
188
|
|
|
*/ |
189
|
7 |
|
public function inInterval($start, $end): self |
190
|
|
|
{ |
191
|
7 |
|
return $this->check(new Check( |
192
|
7 |
|
self::ERROR_NOT_IN_INTERVAL, |
193
|
7 |
|
fn ($value) => $value > $start && $value < $end, |
194
|
7 |
|
['start' => $start, 'end' => $end] |
195
|
7 |
|
)); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|