Passed
Push — master ( cdfa93...96b6af )
by Smoren
02:36
created

FloatRule::nonFractional()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Smoren\Validator\Rules;
4
5
use Smoren\Validator\Interfaces\FloatRuleInterface;
6
use Smoren\Validator\Structs\Check;
7
8
class FloatRule extends NumberRule implements FloatRuleInterface
9
{
10
    public const ERROR_NOT_FLOAT = 'not_float';
11
    public const ERROR_FRACTIONAL = 'fractional';
12
    public const ERROR_NOT_FRACTIONAL = 'not_fractional';
13
    public const ERROR_NOT_INFINITE = 'not_infinite';
14
    public const ERROR_NOT_FINITE = 'not_finite';
15
16
    public function __construct()
17
    {
18
        $this->add(new Check(
19
            self::ERROR_NOT_FLOAT,
20
            fn ($value) => is_int($value),
21
            [],
22
            true
23
        ));
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     *
29
     * @return static
30
     */
31
    public function fractional(): self
32
    {
33
        return $this->add(new Check(
34
            self::ERROR_NOT_FRACTIONAL,
35
            fn ($value) => \abs($value - \round($value)) >= PHP_FLOAT_EPSILON
36
        ));
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     *
42
     * @return static
43
     */
44
    public function nonFractional(): self
45
    {
46
        return $this->add(new Check(
47
            self::ERROR_FRACTIONAL,
48
            fn ($value) => \abs($value - \round($value)) < PHP_FLOAT_EPSILON
49
        ));
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     *
55
     * @return static
56
     */
57
    public function finite(): self
58
    {
59
        return $this->add(new Check(
60
            self::ERROR_NOT_FINITE,
61
            fn ($value) => $value > -INF && $value < INF,
62
            [],
63
        ));
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     *
69
     * @return static
70
     */
71
    public function infinite(): self
72
    {
73
        return$this->add(new Check(
74
            self::ERROR_NOT_INFINITE,
75
            fn ($value) => $value === -INF || $value === INF,
76
            [],
77
        ));
78
    }
79
}
80