Passed
Push — master ( 8039d2...4fbf5c )
by Smoren
02:19
created

FloatRule::fractional()   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
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
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\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
    /**
15
     * @param string $name
16
     */
17 42
    public function __construct(string $name)
18
    {
19 42
        Rule::__construct($name);
20 42
        $this->check(new Check(
21 42
            CheckName::FLOAT,
22 42
            CheckErrorName::NOT_FLOAT,
23 42
            fn ($value) => is_float($value),
24 42
            []
25 42
        ), true);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @return static
32
     */
33 2
    public function fractional(): self
34
    {
35 2
        return $this->check(new Check(
36 2
            CheckName::FRACTIONAL,
37 2
            CheckErrorName::NOT_FRACTIONAL,
38 2
            fn ($value) => \abs($value - \round($value)) >= PHP_FLOAT_EPSILON
39 2
        ));
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @return static
46
     */
47 5
    public function nonFractional(): self
48
    {
49 5
        return $this->check(new Check(
50 5
            CheckName::NOT_FRACTIONAL,
51 5
            CheckErrorName::FRACTIONAL,
52 5
            fn ($value) => \abs($value - \round($value)) < PHP_FLOAT_EPSILON
53 5
        ));
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     *
59
     * @return static
60
     */
61 2
    public function finite(): self
62
    {
63 2
        return $this->check(new Check(
64 2
            CheckName::FINITE,
65 2
            CheckErrorName::NOT_FINITE,
66 2
            fn ($value) => $value > -INF && $value < INF,
67 2
        ));
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     *
73
     * @return static
74
     */
75 2
    public function infinite(): self
76
    {
77 2
        return$this->check(new Check(
78 2
            CheckName::INFINITE,
79 2
            CheckErrorName::NOT_INFINITE,
80 2
            fn ($value) => $value === -INF || $value === INF,
81 2
        ));
82
    }
83
}
84