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

FloatRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 1
b 0
f 0
dl 0
loc 69
ccs 0
cts 28
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A nonFractional() 0 5 1
A fractional() 0 5 1
A __construct() 0 7 1
A finite() 0 6 2
A infinite() 0 6 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