1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\Validator\Rules; |
4
|
|
|
|
5
|
|
|
use Smoren\Validator\Interfaces\IntegerRuleInterface; |
6
|
|
|
use Smoren\Validator\Structs\Check; |
7
|
|
|
|
8
|
|
|
class IntegerRule extends Rule implements IntegerRuleInterface |
9
|
|
|
{ |
10
|
|
|
public const ERROR_NOT_INTEGER = 'not_integer'; |
11
|
|
|
public const ERROR_NOT_POSITIVE = 'not_positive'; |
12
|
|
|
public const ERROR_NOT_NON_POSITIVE = 'not_non_positive'; |
13
|
|
|
public const ERROR_NOT_NON_NEGATIVE = 'not_non_negative'; |
14
|
|
|
public const ERROR_NOT_NEGATIVE = 'not_negative'; |
15
|
|
|
public const ERROR_NOT_IN_SEGMENT = 'not_in_segment'; |
16
|
|
|
public const ERROR_NOT_IN_INTERVAL = 'not_in_interval'; |
17
|
|
|
public const ERROR_NOT_EVEN = 'not_even'; |
18
|
|
|
public const ERROR_NOT_ODD = 'not_odd'; |
19
|
|
|
|
20
|
22 |
|
public function __construct() |
21
|
|
|
{ |
22
|
22 |
|
$this->add(new Check( |
23
|
22 |
|
self::ERROR_NOT_INTEGER, |
24
|
22 |
|
fn ($value) => is_int($value), |
25
|
22 |
|
[], |
26
|
22 |
|
true |
27
|
22 |
|
)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
* |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
14 |
|
public function positive(): self |
36
|
|
|
{ |
37
|
14 |
|
return $this->add(new Check( |
38
|
14 |
|
self::ERROR_NOT_POSITIVE, |
39
|
14 |
|
fn ($value) => $value > 0 |
40
|
14 |
|
)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
2 |
|
public function nonPositive(): self |
49
|
|
|
{ |
50
|
2 |
|
return $this->add(new Check( |
51
|
2 |
|
self::ERROR_NOT_NON_POSITIVE, |
52
|
2 |
|
fn ($value) => $value <= 0 |
53
|
2 |
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
* |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
2 |
|
public function nonNegative(): self |
62
|
|
|
{ |
63
|
2 |
|
return $this->add(new Check( |
64
|
2 |
|
self::ERROR_NOT_NON_NEGATIVE, |
65
|
2 |
|
fn ($value) => $value >= 0 |
66
|
2 |
|
)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
* |
72
|
|
|
* @return static |
73
|
|
|
*/ |
74
|
2 |
|
public function negative(): self |
75
|
|
|
{ |
76
|
2 |
|
return $this->add(new Check( |
77
|
2 |
|
self::ERROR_NOT_NEGATIVE, |
78
|
2 |
|
fn ($value) => $value < 0 |
79
|
2 |
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
*/ |
87
|
6 |
|
public function inSegment($start, $end): self |
88
|
|
|
{ |
89
|
6 |
|
return $this->add(new Check( |
90
|
6 |
|
self::ERROR_NOT_IN_SEGMENT, |
91
|
6 |
|
fn ($value) => $value >= $start && $value <= $end, |
92
|
6 |
|
['start' => $start, 'end' => $end] |
93
|
6 |
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
6 |
|
public function inInterval($start, $end): self |
100
|
|
|
{ |
101
|
6 |
|
return $this->add(new Check( |
102
|
6 |
|
self::ERROR_NOT_IN_INTERVAL, |
103
|
6 |
|
fn ($value) => $value > $start && $value < $end, |
104
|
6 |
|
['start' => $start, 'end' => $end] |
105
|
6 |
|
)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
* |
111
|
|
|
* @return static |
112
|
|
|
*/ |
113
|
5 |
|
public function even(): self |
114
|
|
|
{ |
115
|
5 |
|
return $this->add(new Check( |
116
|
5 |
|
self::ERROR_NOT_EVEN, |
117
|
5 |
|
fn ($value) => $value % 2 === 0, |
118
|
5 |
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
* |
124
|
|
|
* @return static |
125
|
|
|
*/ |
126
|
7 |
|
public function odd(): self |
127
|
|
|
{ |
128
|
7 |
|
return $this->add(new Check( |
129
|
7 |
|
self::ERROR_NOT_ODD, |
130
|
7 |
|
fn ($value) => $value % 2 !== 0 |
131
|
7 |
|
)); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|