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

FloatRule::nonFractional()   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 1
Bugs 0 Features 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 1
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\FloatRuleInterface;
9
use Smoren\Validator\Structs\CheckErrorName;
10
use Smoren\Validator\Structs\CheckName;
11
12
class FloatRule extends NumericRule implements FloatRuleInterface
13
{
14
    protected const DEFAULT_NAME = 'float';
15
16
    /**
17
     * @param string $name
18
     */
19 8
    public function __construct(string $name)
20
    {
21 8
        Rule::__construct($name);
22 8
        $this->check(new Check(
23 8
            CheckName::FLOAT,
24 8
            CheckErrorName::NOT_FLOAT,
25 8
            fn ($value) => is_float($value),
26 8
            []
27 8
        ), true);
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     *
33
     * @return static
34
     */
35
    public function fractional(): self
36
    {
37
        return $this->check(new Check(
38
            CheckName::FRACTIONAL,
39
            CheckErrorName::NOT_FRACTIONAL,
40
            fn ($value) => \abs($value - \round($value)) >= PHP_FLOAT_EPSILON
41
        ));
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     *
47
     * @return static
48
     */
49 3
    public function nonFractional(): self
50
    {
51 3
        return $this->check(new Check(
52 3
            CheckName::NOT_FRACTIONAL,
53 3
            CheckErrorName::FRACTIONAL,
54 3
            fn ($value) => \abs($value - \round($value)) < PHP_FLOAT_EPSILON
55 3
        ));
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     *
61
     * @return static
62
     */
63
    public function finite(): self
64
    {
65
        return $this->check(new Check(
66
            CheckName::FINITE,
67
            CheckErrorName::NOT_FINITE,
68
            fn ($value) => $value > -INF && $value < INF,
69
        ));
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     *
75
     * @return static
76
     */
77
    public function infinite(): self
78
    {
79
        return$this->check(new Check(
80
            CheckName::INFINITE,
81
            CheckErrorName::NOT_INFINITE,
82
            fn ($value) => $value === -INF || $value === INF,
83
        ));
84
    }
85
}
86