Passed
Push — master ( 1aecc0...74f07c )
by Smoren
02:22
created

NumericRule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Test Coverage

Coverage 85.92%

Importance

Changes 0
Metric Value
wmc 16
eloc 60
c 0
b 0
f 0
dl 0
loc 189
ccs 61
cts 71
cp 0.8592
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A nonPositive() 0 5 1
A inInterval() 0 6 2
A string() 0 5 1
A lessOrEqual() 0 6 1
A lessTran() 0 6 1
A greaterTran() 0 6 1
A positive() 0 5 1
A greaterOrEqual() 0 6 1
A nonNegative() 0 5 1
A between() 0 6 2
A negative() 0 5 1
A number() 0 5 2
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 1
    public function __construct()
30
    {
31 1
        $this->addCheck(new Check(
32 1
            self::ERROR_NOT_NUMERIC,
33 1
            fn ($value) => is_numeric($value),
34 1
            []
35 1
        ), true);
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @return static
42
     */
43
    public function number(): self
44
    {
45
        return $this->addCheck(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->addCheck(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->addCheck(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->addCheck(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->addCheck(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->addCheck(new Check(
111 2
            self::ERROR_NOT_NEGATIVE,
112 2
            fn ($value) => $value < 0
113 2
        ));
114
    }
115
116
117
    /**
118
     * {@inheritDoc}
119
     *
120
     * @return static
121
     */
122 2
    public function greaterTran($number): NumericRuleInterface
123
    {
124 2
        return $this->addCheck(new Check(
125 2
            self::ERROR_NOT_GREATER,
126 2
            fn ($value) => $value > $number,
127 2
            ['number' => $number]
128 2
        ));
129
    }
130
131
132
    /**
133
     * {@inheritDoc}
134
     *
135
     * @return static
136
     */
137 2
    public function greaterOrEqual($number): NumericRuleInterface
138
    {
139 2
        return $this->addCheck(new Check(
140 2
            self::ERROR_NOT_GREATER_OR_EQUEAL,
141 2
            fn ($value) => $value >= $number,
142 2
            ['number' => $number]
143 2
        ));
144
    }
145
146
147
    /**
148
     * {@inheritDoc}
149
     *
150
     * @return static
151
     */
152 2
    public function lessTran($number): NumericRuleInterface
153
    {
154 2
        return $this->addCheck(new Check(
155 2
            self::ERROR_NOT_LESS,
156 2
            fn ($value) => $value < $number,
157 2
            ['number' => $number]
158 2
        ));
159
    }
160
161
162
    /**
163
     * {@inheritDoc}
164
     *
165
     * @return static
166
     */
167 2
    public function lessOrEqual($number): NumericRuleInterface
168
    {
169 2
        return $this->addCheck(new Check(
170 2
            self::ERROR_NOT_LESS_OR_EQUEAL,
171 2
            fn ($value) => $value <= $number,
172 2
            ['number' => $number]
173 2
        ));
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     *
179
     * @return static
180
     */
181 8
    public function between($start, $end): self
182
    {
183 8
        return $this->addCheck(new Check(
184 8
            self::ERROR_NOT_IN_SEGMENT,
185 8
            fn ($value) => $value >= $start && $value <= $end,
186 8
            ['start' => $start, 'end' => $end]
187 8
        ));
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193 7
    public function inInterval($start, $end): self
194
    {
195 7
        return $this->addCheck(new Check(
196 7
            self::ERROR_NOT_IN_INTERVAL,
197 7
            fn ($value) => $value > $start && $value < $end,
198 7
            ['start' => $start, 'end' => $end]
199 7
        ));
200
    }
201
}
202