1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\Validator\Rules; |
4
|
|
|
|
5
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
6
|
|
|
use Smoren\Validator\Interfaces\NumberRuleInterface; |
7
|
|
|
use Smoren\Validator\Structs\Check; |
8
|
|
|
|
9
|
|
|
class IntegerRule extends NumberRule implements IntegerRuleInterface |
10
|
|
|
{ |
11
|
|
|
public const ERROR_NOT_EVEN = 'not_even'; |
12
|
|
|
public const ERROR_NOT_ODD = 'not_odd'; |
13
|
|
|
|
14
|
34 |
|
public function __construct() |
15
|
|
|
{ |
16
|
34 |
|
$this->add(new Check( |
17
|
34 |
|
self::ERROR_NOT_INTEGER, |
18
|
34 |
|
fn ($value) => is_int($value), |
19
|
34 |
|
[], |
20
|
34 |
|
true |
21
|
34 |
|
)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
* |
27
|
|
|
* @return static |
28
|
|
|
*/ |
29
|
16 |
|
public function positive(): self |
30
|
|
|
{ |
31
|
16 |
|
return $this->add(new Check( |
32
|
16 |
|
self::ERROR_NOT_POSITIVE, |
33
|
16 |
|
fn ($value) => $value > 0 |
34
|
16 |
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
* |
40
|
|
|
* @return static |
41
|
|
|
*/ |
42
|
2 |
|
public function nonPositive(): self |
43
|
|
|
{ |
44
|
2 |
|
return $this->add(new Check( |
45
|
2 |
|
self::ERROR_NOT_NON_POSITIVE, |
46
|
2 |
|
fn ($value) => $value <= 0 |
47
|
2 |
|
)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
* |
53
|
|
|
* @return static |
54
|
|
|
*/ |
55
|
2 |
|
public function nonNegative(): self |
56
|
|
|
{ |
57
|
2 |
|
return $this->add(new Check( |
58
|
2 |
|
self::ERROR_NOT_NON_NEGATIVE, |
59
|
2 |
|
fn ($value) => $value >= 0 |
60
|
2 |
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
2 |
|
public function negative(): self |
69
|
|
|
{ |
70
|
2 |
|
return $this->add(new Check( |
71
|
2 |
|
self::ERROR_NOT_NEGATIVE, |
72
|
2 |
|
fn ($value) => $value < 0 |
73
|
2 |
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
* |
80
|
|
|
* @return static |
81
|
|
|
*/ |
82
|
2 |
|
public function greaterTran($number): NumberRuleInterface |
83
|
|
|
{ |
84
|
2 |
|
return $this->add(new Check( |
85
|
2 |
|
self::ERROR_NOT_GREATER, |
86
|
2 |
|
fn ($value) => $value > $number, |
87
|
2 |
|
['number' => $number] |
88
|
2 |
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
* |
95
|
|
|
* @return static |
96
|
|
|
*/ |
97
|
2 |
|
public function greaterOrEqual($number): NumberRuleInterface |
98
|
|
|
{ |
99
|
2 |
|
return $this->add(new Check( |
100
|
2 |
|
self::ERROR_NOT_GREATER_OR_EQUEAL, |
101
|
2 |
|
fn ($value) => $value >= $number, |
102
|
2 |
|
['number' => $number] |
103
|
2 |
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
* |
110
|
|
|
* @return static |
111
|
|
|
*/ |
112
|
2 |
|
public function lessTran($number): NumberRuleInterface |
113
|
|
|
{ |
114
|
2 |
|
return $this->add(new Check( |
115
|
2 |
|
self::ERROR_NOT_LESS, |
116
|
2 |
|
fn ($value) => $value < $number, |
117
|
2 |
|
['number' => $number] |
118
|
2 |
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
* |
125
|
|
|
* @return static |
126
|
|
|
*/ |
127
|
2 |
|
public function lessOrEqual($number): NumberRuleInterface |
128
|
|
|
{ |
129
|
2 |
|
return $this->add(new Check( |
130
|
2 |
|
self::ERROR_NOT_LESS_OR_EQUEAL, |
131
|
2 |
|
fn ($value) => $value <= $number, |
132
|
2 |
|
['number' => $number] |
133
|
2 |
|
)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
* |
139
|
|
|
* @return static |
140
|
|
|
*/ |
141
|
7 |
|
public function inSegment($start, $end): self |
142
|
|
|
{ |
143
|
7 |
|
return $this->add(new Check( |
144
|
7 |
|
self::ERROR_NOT_IN_SEGMENT, |
145
|
7 |
|
fn ($value) => $value >= $start && $value <= $end, |
146
|
7 |
|
['start' => $start, 'end' => $end] |
147
|
7 |
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
7 |
|
public function inInterval($start, $end): self |
154
|
|
|
{ |
155
|
7 |
|
return $this->add(new Check( |
156
|
7 |
|
self::ERROR_NOT_IN_INTERVAL, |
157
|
7 |
|
fn ($value) => $value > $start && $value < $end, |
158
|
7 |
|
['start' => $start, 'end' => $end] |
159
|
7 |
|
)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
* |
165
|
|
|
* @return static |
166
|
|
|
*/ |
167
|
5 |
|
public function even(): self |
168
|
|
|
{ |
169
|
5 |
|
return $this->add(new Check( |
170
|
5 |
|
self::ERROR_NOT_EVEN, |
171
|
5 |
|
fn ($value) => $value % 2 === 0, |
172
|
5 |
|
)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritDoc} |
177
|
|
|
* |
178
|
|
|
* @return static |
179
|
|
|
*/ |
180
|
9 |
|
public function odd(): self |
181
|
|
|
{ |
182
|
9 |
|
return $this->add(new Check( |
183
|
9 |
|
self::ERROR_NOT_ODD, |
184
|
9 |
|
fn ($value) => $value % 2 !== 0 |
185
|
9 |
|
)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|