Passed
Push — master ( b3288f...d572ef )
by Smoren
02:21
created

NumericRule::lessThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
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\Factories\CheckBuilder;
8
use Smoren\Validator\Interfaces\NumericRuleInterface;
9
use Smoren\Validator\Structs\CheckName;
10
use Smoren\Validator\Structs\Param;
11
12
class NumericRule extends MixedRule implements NumericRuleInterface
13
{
14
    /**
15
     * @param string $name
16
     */
17 79
    public function __construct(string $name)
18
    {
19 79
        parent::__construct($name);
20
21 79
        $this->check(
22 79
            CheckBuilder::create(CheckName::NUMERIC)
23 79
                ->withPredicate(fn ($value) => \is_numeric($value))
24 79
                ->build(),
25 79
            true
26 79
        );
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @return static
33
     */
34 2
    public function number(bool $stopOnViolation = true): self
35
    {
36 2
        return $this->check(
37 2
            CheckBuilder::create(CheckName::NUMBER)
38 2
                ->withPredicate(fn ($value) => \is_int($value) || \is_float($value))
39 2
                ->build(),
40 2
            $stopOnViolation
41 2
        );
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     *
47
     * @return static
48
     */
49 2
    public function string(bool $stopOnViolation = true): self
50
    {
51 2
        return $this->check(
52 2
            CheckBuilder::create(CheckName::STRING)
53 2
                ->withPredicate(fn ($value) => \is_string($value))
54 2
                ->build(),
55 2
            $stopOnViolation
56 2
        );
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     *
62
     * @return static
63
     */
64 69
    public function integer(bool $stopOnViolation = true): self
65
    {
66 69
        return $this->check(
67 69
            CheckBuilder::create(CheckName::INTEGER)
68 69
                ->withPredicate(fn ($value) => \is_int($value))
69 69
                ->build(),
70 69
            $stopOnViolation
71 69
        );
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     *
77
     * @return static
78
     */
79 48
    public function float(bool $stopOnViolation = true): self
80
    {
81 48
        return $this->check(
82 48
            CheckBuilder::create(CheckName::FLOAT)
83 48
                ->withPredicate(fn ($value) => \is_float($value))
84 48
                ->build(),
85 48
            $stopOnViolation
86 48
        );
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     *
92
     * @return static
93
     */
94 6
    public function truthy(): self
95
    {
96 6
        return $this->check(
97 6
            CheckBuilder::create(CheckName::TRUTHY)
98 6
                ->withPredicate(fn ($value) => \boolval(floatval($value)))
99 6
                ->build()
100 6
        );
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     *
106
     * @return static
107
     */
108 6
    public function falsy(): self
109
    {
110 6
        return $this->check(
111 6
            CheckBuilder::create(CheckName::FALSY)
112 6
                ->withPredicate(fn ($value) => !\boolval(floatval($value)))
113 6
                ->build()
114 6
        );
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     *
120
     * @return static
121
     */
122 56
    public function positive(): self
123
    {
124 56
        return $this->check(
125 56
            CheckBuilder::create(CheckName::POSITIVE)
126 56
                ->withPredicate(fn ($value) => $value > 0)
127 56
                ->build()
128 56
        );
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     *
134
     * @return static
135
     */
136 6
    public function nonPositive(): self
137
    {
138 6
        return $this->check(
139 6
            CheckBuilder::create(CheckName::NON_POSITIVE)
140 6
                ->withPredicate(fn ($value) => $value <= 0)
141 6
                ->build()
142 6
        );
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     *
148
     * @return static
149
     */
150 13
    public function nonNegative(): self
151
    {
152 13
        return $this->check(
153 13
            CheckBuilder::create(CheckName::NON_NEGATIVE)
154 13
                ->withPredicate(fn ($value) => $value >= 0)
155 13
                ->build()
156 13
        );
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     *
162
     * @return static
163
     */
164 6
    public function negative(): self
165
    {
166 6
        return $this->check(
167 6
            CheckBuilder::create(CheckName::NEGATIVE)
168 6
                ->withPredicate(fn ($value) => $value < 0)
169 6
                ->build()
170 6
        );
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     *
176
     * @return static
177
     */
178 11
    public function greaterThan($number): NumericRuleInterface
179
    {
180 11
        return $this->check(
181 11
            CheckBuilder::create(CheckName::GREATER)
182 11
                ->withPredicate(fn ($value, $number) => $value > $number)
183 11
                ->withParams([Param::EXPECTED => $number])
184 11
                ->build()
185 11
        );
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     *
191
     * @return static
192
     */
193 6
    public function greaterOrEqual($number): NumericRuleInterface
194
    {
195 6
        return $this->check(
196 6
            CheckBuilder::create(CheckName::GREATER_OR_EQUEAL)
197 6
                ->withPredicate(fn ($value, $number) => $value >= $number)
198 6
                ->withParams([Param::EXPECTED => $number])
199 6
                ->build()
200 6
        );
201
    }
202
203
    /**
204
     * {@inheritDoc}
205
     *
206
     * @return static
207
     */
208 13
    public function lessThan($number): NumericRuleInterface
209
    {
210 13
        return $this->check(
211 13
            CheckBuilder::create(CheckName::LESS)
212 13
                ->withPredicate(fn ($value, $number) => $value < $number)
213 13
                ->withParams([Param::EXPECTED => $number])
214 13
                ->build()
215 13
        );
216
    }
217
218
    /**
219
     * {@inheritDoc}
220
     *
221
     * @return static
222
     */
223 6
    public function lessOrEqual($number): NumericRuleInterface
224
    {
225 6
        return $this->check(
226 6
            CheckBuilder::create(CheckName::LESS_OR_EQUEAL)
227 6
                ->withPredicate(fn ($value, $number) => $value <= $number)
228 6
                ->withParams([Param::EXPECTED => $number])
229 6
                ->build()
230 6
        );
231
    }
232
233
    /**
234
     * {@inheritDoc}
235
     *
236
     * @return static
237
     */
238 23
    public function between($start, $end): self
239
    {
240 23
        return $this->check(
241 23
            CheckBuilder::create(CheckName::BETWEEN)
242 23
                ->withPredicate(fn ($value, $start, $end) => $value >= $start && $value <= $end)
243 23
                ->withParams(['start' => $start, 'end' => $end])
244 23
                ->build()
245 23
        );
246
    }
247
248
    /**
249
     * {@inheritDoc}
250
     */
251 22
    public function inInterval($start, $end): self
252
    {
253 22
        return $this->check(
254 22
            CheckBuilder::create(CheckName::IN_INTERVAL)
255 22
                ->withPredicate(fn ($value, $start, $end) => $value > $start && $value < $end)
256 22
                ->withParams(['start' => $start, 'end' => $end])
257 22
                ->build()
258 22
        );
259
    }
260
261
    /**
262
     * {@inheritDoc}
263
     *
264
     * @return static
265
     */
266 4
    public function fractional(): self
267
    {
268 4
        return $this->check(
269 4
            CheckBuilder::create(CheckName::FRACTIONAL)
270 4
                ->withPredicate(fn ($value) => \abs($value - \round(\floatval($value))) >= \PHP_FLOAT_EPSILON)
271 4
                ->build()
272 4
        );
273
    }
274
275
    /**
276
     * {@inheritDoc}
277
     *
278
     * @return static
279
     */
280 7
    public function nonFractional(): self
281
    {
282 7
        return $this->check(
283 7
            CheckBuilder::create(CheckName::NON_FRACTIONAL)
284 7
                ->withPredicate(fn ($value) => \abs($value - \round(\floatval($value))) < \PHP_FLOAT_EPSILON)
285 7
                ->build()
286 7
        );
287
    }
288
289
    /**
290
     * {@inheritDoc}
291
     *
292
     * @return static
293
     */
294 4
    public function finite(): self
295
    {
296 4
        return $this->check(
297 4
            CheckBuilder::create(CheckName::FINITE)
298 4
                ->withPredicate(fn ($value) => $value > -INF && $value < INF)
299 4
                ->build()
300 4
        );
301
    }
302
303
    /**
304
     * {@inheritDoc}
305
     *
306
     * @return static
307
     */
308 4
    public function infinite(): self
309
    {
310 4
        return $this->check(
311 4
            CheckBuilder::create(CheckName::INFINITE)
312 4
                ->withPredicate(fn ($value) => $value === -INF || $value === INF)
313 4
                ->build()
314 4
        );
315
    }
316
317
    /**
318
     * {@inheritDoc}
319
     *
320
     * @return static
321
     */
322 20
    public function even(): self
323
    {
324 20
        return $this->check(
325 20
            CheckBuilder::create(CheckName::EVEN)
326 20
                ->withPredicate(fn ($value) => $value % 2 === 0)
327 20
                ->build()
328 20
        );
329
    }
330
331
    /**
332
     * {@inheritDoc}
333
     *
334
     * @return static
335
     */
336 22
    public function odd(): self
337
    {
338 22
        return $this->check(
339 22
            CheckBuilder::create(CheckName::ODD)
340 22
                ->withPredicate(fn ($value) => $value % 2 !== 0)
341 22
                ->build()
342 22
        );
343
    }
344
}
345