Passed
Push — master ( 03f85f...49e405 )
by Smoren
02:21
created

NumericRule   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 84.52%

Importance

Changes 0
Metric Value
eloc 61
c 0
b 0
f 0
dl 0
loc 184
ccs 71
cts 84
cp 0.8452
rs 10
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 8 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
        ), true);
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     *
30
     * @return static
31
     */
32
    public function number(): self
33
    {
34
        return $this->check(new Check(
35
            CheckName::NUMBER,
36
            CheckErrorName::NOT_NUMBER,
37
            fn ($value) => \is_int($value) || \is_float($value)
38
        ));
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     *
44
     * @return static
45
     */
46
    public function string(): self
47
    {
48
        return $this->check(new Check(
49
            CheckName::STRING,
50
            CheckErrorName::NOT_STRING,
51
            fn ($value) => \is_string($value)
52
        ));
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     *
58
     * @return static
59
     */
60 24
    public function positive(): self
61
    {
62 24
        return $this->check(new Check(
63 24
            CheckName::POSITIVE,
64 24
            CheckErrorName::NOT_POSITIVE,
65 24
            fn ($value) => $value > 0
66 24
        ));
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     *
72
     * @return static
73
     */
74 2
    public function nonPositive(): self
75
    {
76 2
        return $this->check(new Check(
77 2
            CheckName::NON_POSITIVE,
78 2
            CheckErrorName::NOT_NON_POSITIVE,
79 2
            fn ($value) => $value <= 0
80 2
        ));
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     *
86
     * @return static
87
     */
88 3
    public function nonNegative(): self
89
    {
90 3
        return $this->check(new Check(
91 3
            CheckName::NON_NEGATIVE,
92 3
            CheckErrorName::NOT_NON_NEGATIVE,
93 3
            fn ($value) => $value >= 0
94 3
        ));
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     *
100
     * @return static
101
     */
102 2
    public function negative(): self
103
    {
104 2
        return $this->check(new Check(
105 2
            CheckName::NEGATIVE,
106 2
            CheckErrorName::NOT_NEGATIVE,
107 2
            fn ($value) => $value < 0
108 2
        ));
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     *
114
     * @return static
115
     */
116 2
    public function greaterTran($number): NumericRuleInterface
117
    {
118 2
        return $this->check(new Check(
119 2
            CheckName::GREATER,
120 2
            CheckErrorName::NOT_GREATER,
121 2
            fn ($value) => $value > $number,
122 2
            ['number' => $number]
123 2
        ));
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     *
129
     * @return static
130
     */
131 2
    public function greaterOrEqual($number): NumericRuleInterface
132
    {
133 2
        return $this->check(new Check(
134 2
            CheckName::GREATER_OR_EQUEAL,
135 2
            CheckErrorName::NOT_GREATER_OR_EQUEAL,
136 2
            fn ($value) => $value >= $number,
137 2
            ['number' => $number]
138 2
        ));
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     *
144
     * @return static
145
     */
146 2
    public function lessTran($number): NumericRuleInterface
147
    {
148 2
        return $this->check(new Check(
149 2
            CheckName::LESS,
150 2
            CheckErrorName::NOT_LESS,
151 2
            fn ($value) => $value < $number,
152 2
            ['number' => $number]
153 2
        ));
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     *
159
     * @return static
160
     */
161 2
    public function lessOrEqual($number): NumericRuleInterface
162
    {
163 2
        return $this->check(new Check(
164 2
            CheckName::LESS_OR_EQUEAL,
165 2
            CheckErrorName::NOT_LESS_OR_EQUEAL,
166 2
            fn ($value) => $value <= $number,
167 2
            ['number' => $number]
168 2
        ));
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     *
174
     * @return static
175
     */
176 9
    public function between($start, $end): self
177
    {
178 9
        return $this->check(new Check(
179 9
            CheckName::BETWEEN,
180 9
            CheckErrorName::NOT_BETWEEN,
181 9
            fn ($value) => $value >= $start && $value <= $end,
182 9
            ['start' => $start, 'end' => $end]
183 9
        ));
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 7
    public function inInterval($start, $end): self
190
    {
191 7
        return $this->check(new Check(
192 7
            CheckName::IN_INTERVAL,
193 7
            CheckErrorName::NOT_IN_INTERVAL,
194 7
            fn ($value) => $value > $start && $value < $end,
195 7
            ['start' => $start, 'end' => $end]
196 7
        ));
197
    }
198
}
199