Passed
Push — master ( c9351b...a8a6df )
by Smoren
02:20
created

NumericRule::nonNegative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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