Passed
Push — master ( bc036e...f8a9bb )
by Smoren
02:19
created

NumericRule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Test Coverage

Coverage 84.71%

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 185
ccs 72
cts 85
cp 0.8471
rs 10
c 0
b 0
f 0
wmc 16

13 Methods

Rating   Name   Duplication   Size   Complexity  
A nonPositive() 0 6 1
A inInterval() 0 7 2
A string() 0 6 1
A lessOrEqual() 0 7 1
A lessTran() 0 7 1
A greaterTran() 0 7 1
A positive() 0 6 1
A __construct() 0 9 1
A greaterOrEqual() 0 7 1
A nonNegative() 0 6 1
A between() 0 7 2
A negative() 0 6 1
A number() 0 6 2
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
use Smoren\Validator\Structs\CheckErrorName;
10
use Smoren\Validator\Structs\CheckName;
11
12
class NumericRule extends Rule implements NumericRuleInterface
13
{
14
    /**
15
     * @param string $name
16
     */
17 2
    public function __construct(string $name)
18
    {
19
        parent::__construct($name);
20 2
        $this->check(new Check(
21 2
            CheckName::NUMERIC,
22 2
            CheckErrorName::NOT_NUMERIC,
23 2
            fn ($value) => is_numeric($value),
24 2
            []
25 2
        ), true);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @return static
32
     */
33
    public function number(): self
34
    {
35
        return $this->check(new Check(
36
            CheckName::NUMBER,
37
            CheckErrorName::NOT_NUMBER,
38
            fn ($value) => is_int($value) || is_float($value)
39
        ));
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @return static
46
     */
47
    public function string(): self
48
    {
49
        return $this->check(new Check(
50
            CheckName::STRING,
51
            CheckErrorName::NOT_STRING,
52
            fn ($value) => is_string($value)
53
        ));
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     *
59
     * @return static
60
     */
61 23
    public function positive(): self
62
    {
63 23
        return $this->check(new Check(
64 23
            CheckName::POSITIVE,
65 23
            CheckErrorName::NOT_POSITIVE,
66 23
            fn ($value) => $value > 0
67 23
        ));
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     *
73
     * @return static
74
     */
75 2
    public function nonPositive(): self
76
    {
77 2
        return $this->check(new Check(
78 2
            CheckName::NON_POSITIVE,
79 2
            CheckErrorName::NOT_NON_POSITIVE,
80 2
            fn ($value) => $value <= 0
81 2
        ));
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     *
87
     * @return static
88
     */
89 3
    public function nonNegative(): self
90
    {
91 3
        return $this->check(new Check(
92 3
            CheckName::NON_NEGATIVE,
93 3
            CheckErrorName::NOT_NON_NEGATIVE,
94 3
            fn ($value) => $value >= 0
95 3
        ));
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     *
101
     * @return static
102
     */
103 2
    public function negative(): self
104
    {
105 2
        return $this->check(new Check(
106 2
            CheckName::NEGATIVE,
107 2
            CheckErrorName::NOT_NEGATIVE,
108 2
            fn ($value) => $value < 0
109 2
        ));
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     *
115
     * @return static
116
     */
117 2
    public function greaterTran($number): NumericRuleInterface
118
    {
119 2
        return $this->check(new Check(
120 2
            CheckName::GREATER,
121 2
            CheckErrorName::NOT_GREATER,
122 2
            fn ($value) => $value > $number,
123 2
            ['number' => $number]
124 2
        ));
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     *
130
     * @return static
131
     */
132 2
    public function greaterOrEqual($number): NumericRuleInterface
133
    {
134 2
        return $this->check(new Check(
135 2
            CheckName::GREATER_OR_EQUEAL,
136 2
            CheckErrorName::NOT_GREATER_OR_EQUEAL,
137 2
            fn ($value) => $value >= $number,
138 2
            ['number' => $number]
139 2
        ));
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     *
145
     * @return static
146
     */
147 2
    public function lessTran($number): NumericRuleInterface
148
    {
149 2
        return $this->check(new Check(
150 2
            CheckName::LESS,
151 2
            CheckErrorName::NOT_LESS,
152 2
            fn ($value) => $value < $number,
153 2
            ['number' => $number]
154 2
        ));
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     *
160
     * @return static
161
     */
162 2
    public function lessOrEqual($number): NumericRuleInterface
163
    {
164 2
        return $this->check(new Check(
165 2
            CheckName::LESS_OR_EQUEAL,
166 2
            CheckErrorName::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
            CheckName::BETWEEN,
181 8
            CheckErrorName::NOT_BETWEEN,
182 8
            fn ($value) => $value >= $start && $value <= $end,
183 8
            ['start' => $start, 'end' => $end]
184 8
        ));
185
    }
186
187
    /**
188
     * {@inheritDoc}
189
     */
190 7
    public function inInterval($start, $end): self
191
    {
192 7
        return $this->check(new Check(
193 7
            CheckName::IN_INTERVAL,
194 7
            CheckErrorName::NOT_IN_INTERVAL,
195 7
            fn ($value) => $value > $start && $value < $end,
196 7
            ['start' => $start, 'end' => $end]
197 7
        ));
198
    }
199
}
200