NumberInterval   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 102
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A concreteValidate() 0 25 5
A getMessage() 0 3 1
A validate() 0 5 1
B switchOperator() 0 13 9
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Filter\Rules;
13
14
use UnexpectedValueException;
15
16
/**
17
 * Check if a number is included or not on interval using ><, <>, >=<, <=> operators.
18
 */
19
class NumberInterval extends AbstractNumber implements RuleSanitizeInterface, RuleValidateInterface
20
{
21
    /**
22
     * @var array Rule properties
23
     */
24
    public static $config = [
25
        'full_class' => __CLASS__,
26
        'alias' => ['numberinterval', 'numint', 'ni'],
27
        'args_count' => 3,
28
        'args_type' => ['string', 'number', 'number']
29
    ];
30
31
    /**
32
     * @var string Error message
33
     */
34
    private $message = '';
35
36
    /**
37
     * Validate.
38
     *
39
     * @return bool
40
     */
41 40
    public function validate(): bool
42
    {
43 40
        $args = \func_get_args();
44
45 40
        return $this->concreteValidate($args[0], $args[1], $args[2], $args[3]);
46
    }
47
48
    /**
49
     * Concrete validate.
50
     *
51
     * @param int|float $received
52
     * @param string    $operator
53
     * @param int|float $min
54
     * @param int|float $max
55
     *
56
     * @return bool
57
     */
58 40
    private function concreteValidate($received, string $operator, $min, $max): bool
59
    {
60 40
        if (!\is_numeric($received)) {
0 ignored issues
show
introduced by
The condition is_numeric($received) is always true.
Loading history...
61 1
            return true;
62
        }
63
64 39
        if (!\is_numeric($min)) {
0 ignored issues
show
introduced by
The condition is_numeric($min) is always true.
Loading history...
65 1
            return true;
66
        }
67
68 38
        if (!\is_numeric($max)) {
0 ignored issues
show
introduced by
The condition is_numeric($max) is always true.
Loading history...
69 1
            return true;
70
        }
71
72 37
        $received = (float) $received;
73 37
        $min = (float) $min;
74 37
        $max = (float) $max;
75
76 37
        if ($this->switchOperator($operator, $received, $min, $max)) {
77 17
            return false;
78
        }
79
80 19
        $this->message = "Received number is not {$min} {$operator} {$max}";
81
82 19
        return true;
83
    }
84
85
    /**
86
     * Perform correct operation from passed operator.
87
     *
88
     * @param string    $operator
89
     * @param int|float $numberReceived
90
     * @param int|float $min
91
     * @param int|float $max
92
     *
93
     * @return bool
94
     *
95
     * @throws UnexpectedValueException if unknown operator is provided.
96
     */
97 37
    private function switchOperator(string $operator, &$numberReceived, &$min, &$max): bool
98
    {
99 37
        switch ($operator) {
100 37
            case '><': //inside interval exclusive
101 11
                return $numberReceived > $min && $numberReceived < $max;
102 26
            case '>=<': //inside interval inclusive
103 15
                return $numberReceived >= $min && $numberReceived <= $max;
104 11
            case '<>': //outside interval exclusive
105 5
                return $numberReceived < $min || $numberReceived > $max;
106 6
            case '<=>': //outside interval inclusive
107 5
                return $numberReceived <= $min || $numberReceived >= $max; ;
108
            default:
109 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted ><, <>, >=<, <=>");
110
        }
111
    }
112
113
    /**
114
     * Return error message.
115
     *
116
     * @return string Error message
117
     */
118 9
    public function getMessage(): string
119
    {
120 9
        return $this->message;
121
    }
122
}
123