Test Failed
Push — master ( 30bcee...994f97 )
by Smoren
02:25
created

NumericRule::integer()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
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 51
    public function __construct(string $name)
18
    {
19 51
        parent::__construct($name);
20
21 51
        $this->check(
22 51
            CheckBuilder::create(CheckName::NUMERIC)
23 51
                ->withPredicate(fn ($value) => \is_numeric($value))
24 51
                ->build(),
25 51
            true
26 51
        );
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 6
    public function integer(bool $stopOnViolation = true): self
65
    {
66 6
        return $this->check(
67 6
            CheckBuilder::create(CheckName::INTEGER)
68 6
                ->withPredicate(fn ($value) => \is_int($value))
69 6
                ->build(),
70 6
            $stopOnViolation
71
        );
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     *
77
     * @return static
78 6
     */
79
    public function float(bool $stopOnViolation = true): self
80 6
    {
81 6
        return $this->check(
82 6
            CheckBuilder::create(CheckName::FLOAT)
83 6
                ->withPredicate(fn ($value) => \is_float($value))
84 6
                ->build(),
85
            $stopOnViolation
86
        );
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     *
92 37
     * @return static
93
     */
94 37
    public function truthy(): self
95 37
    {
96 37
        return $this->check(
97 37
            CheckBuilder::create(CheckName::TRUTHY)
98 37
                ->withPredicate(fn ($value) => \boolval(floatval($value)))
99
                ->build()
100
        );
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     *
106 6
     * @return static
107
     */
108 6
    public function falsy(): self
109 6
    {
110 6
        return $this->check(
111 6
            CheckBuilder::create(CheckName::FALSY)
112 6
                ->withPredicate(fn ($value) => !\boolval(floatval($value)))
113
                ->build()
114
        );
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     *
120 11
     * @return static
121
     */
122 11
    public function positive(): self
123 11
    {
124 11
        return $this->check(
125 11
            CheckBuilder::create(CheckName::POSITIVE)
126 11
                ->withPredicate(fn ($value) => $value > 0)
127
                ->build()
128
        );
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     *
134 6
     * @return static
135
     */
136 6
    public function nonPositive(): self
137 6
    {
138 6
        return $this->check(
139 6
            CheckBuilder::create(CheckName::NON_POSITIVE)
140 6
                ->withPredicate(fn ($value) => $value <= 0)
141
                ->build()
142
        );
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     *
148 11
     * @return static
149
     */
150 11
    public function nonNegative(): self
151 11
    {
152 11
        return $this->check(
153 11
            CheckBuilder::create(CheckName::NON_NEGATIVE)
154 11
                ->withPredicate(fn ($value) => $value >= 0)
155 11
                ->build()
156
        );
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     *
162
     * @return static
163 6
     */
164
    public function negative(): self
165 6
    {
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 13
    public function greaterTran($number): NumericRuleInterface
179
    {
180 13
        return $this->check(
181 13
            CheckBuilder::create(CheckName::GREATER)
182 13
                ->withPredicate(fn ($value, $number) => $value > $number)
183 13
                ->withParams([Param::EXPECTED => $number])
184 13
                ->build()
185 13
        );
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 15
    public function lessTran($number): NumericRuleInterface
209
    {
210 15
        return $this->check(
211 15
            CheckBuilder::create(CheckName::LESS)
212 15
                ->withPredicate(fn ($value, $number) => $value < $number)
213 15
                ->withParams([Param::EXPECTED => $number])
214 15
                ->build()
215 15
        );
216
    }
217
218
    /**
219
     * {@inheritDoc}
220
     *
221 11
     * @return static
222
     */
223 11
    public function lessOrEqual($number): NumericRuleInterface
224 11
    {
225 11
        return $this->check(
226 11
            CheckBuilder::create(CheckName::LESS_OR_EQUEAL)
227 11
                ->withPredicate(fn ($value, $number) => $value <= $number)
228 11
                ->withParams([Param::EXPECTED => $number])
229
                ->build()
230
        );
231
    }
232
233
    /**
234
     * {@inheritDoc}
235
     *
236
     * @return static
237
     */
238
    public function between($start, $end): self
239
    {
240
        return $this->check(
241
            CheckBuilder::create(CheckName::BETWEEN)
242
                ->withPredicate(fn ($value, $start, $end) => $value >= $start && $value <= $end)
243
                ->withParams(['start' => $start, 'end' => $end])
244
                ->build()
245
        );
246
    }
247
248
    /**
249
     * {@inheritDoc}
250
     */
251
    public function inInterval($start, $end): self
252
    {
253
        return $this->check(
254
            CheckBuilder::create(CheckName::IN_INTERVAL)
255
                ->withPredicate(fn ($value, $start, $end) => $value > $start && $value < $end)
256
                ->withParams(['start' => $start, 'end' => $end])
257
                ->build()
258
        );
259
    }
260
261
    /**
262
     * {@inheritDoc}
263
     *
264
     * @return static
265
     */
266
    public function fractional(): self
267
    {
268
        return $this->check(
269
            CheckBuilder::create(CheckName::FRACTIONAL)
270
                ->withPredicate(fn ($value) => \abs($value - \round($value)) >= \PHP_FLOAT_EPSILON)
271
                ->build()
272
        );
273
    }
274
275
    /**
276
     * {@inheritDoc}
277
     *
278
     * @return static
279
     */
280
    public function nonFractional(): self
281
    {
282
        return $this->check(
283
            CheckBuilder::create(CheckName::NON_FRACTIONAL)
284
                ->withPredicate(fn ($value) => \abs($value - \round($value)) < \PHP_FLOAT_EPSILON)
285
                ->build()
286
        );
287
    }
288
289
    /**
290
     * {@inheritDoc}
291
     *
292
     * @return static
293
     */
294
    public function finite(): self
295
    {
296
        return $this->check(
297
            CheckBuilder::create(CheckName::FINITE)
298
                ->withPredicate(fn ($value) => $value > -INF && $value < INF)
299
                ->build()
300
        );
301
    }
302
303
    /**
304
     * {@inheritDoc}
305
     *
306
     * @return static
307
     */
308
    public function infinite(): self
309
    {
310
        return $this->check(
311
            CheckBuilder::create(CheckName::INFINITE)
312
                ->withPredicate(fn ($value) => $value === -INF || $value === INF)
313
                ->build()
314
        );
315
    }
316
317
    /**
318
     * {@inheritDoc}
319
     *
320
     * @return static
321
     */
322
    public function even(): self
323
    {
324
        return $this->check(
325
            CheckBuilder::create(CheckName::EVEN)
326
                ->withPredicate(fn ($value) => $value % 2 === 0)
327
                ->build()
328
        );
329
    }
330
331
    /**
332
     * {@inheritDoc}
333
     *
334
     * @return static
335
     */
336
    public function odd(): self
337
    {
338
        return $this->check(
339
            CheckBuilder::create(CheckName::ODD)
340
                ->withPredicate(fn ($value) => $value % 2 !== 0)
341
                ->build()
342
        );
343
    }
344
}
345