Passed
Push — master ( 17adcb...9acb8c )
by Smoren
12:18
created

NumericRule::number()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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