Completed
Push — master ( 5ad706...418f81 )
by Sebastian
03:53
created

NumberInterval::concreteValidate()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 4
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 5
rs 9.5222
c 0
b 0
f 0
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
        'class' => 'NumberInterval',
26
        'full_class' => __CLASS__,
27
        'alias' => ['numberinterval', 'numint', 'ni'],
28
        'args_count' => 3,
29
        'args_type' => ['string', 'number', 'number'],
30
        'has_validate' => true,
31
        //'has_sanitize' => true
32
    ];
33
34
    /**
35
     * @var string Error message
36
     */
37
    private $message = '';
38
39
    /**
40
     * Validate.
41
     *
42
     * @return bool
43
     */
44 39
    public function validate(): bool
45
    {
46 39
        $args = func_get_args();
47
48 39
        return $this->concreteValidate($args[0], $args[1], $args[2], $args[3]);
49
    }
50
51
    /**
52
     * Concrete validate.
53
     *
54
     * @param int|float $received
55
     * @param string    $operator
56
     * @param int|float $min
57
     * @param int|float $max
58
     *
59
     * @return bool
60
     */
61 39
    private function concreteValidate($received, string $operator, $min, $max): bool
62
    {
63 39
        if (!is_numeric($received)) {
0 ignored issues
show
introduced by
The condition is_numeric($received) is always true.
Loading history...
64 1
            return true;
65
        }
66
67 38
        if (!is_numeric($min)) {
0 ignored issues
show
introduced by
The condition is_numeric($min) is always true.
Loading history...
68 1
            return true;
69
        }
70
71 37
        if (!is_numeric($max)) {
0 ignored issues
show
introduced by
The condition is_numeric($max) is always true.
Loading history...
72 1
            return true;
73
        }
74
75 36
        settype($received, 'float');
76 36
        settype($min, 'float');
77 36
        settype($max, 'float');
78
79 36
        if ($this->switchOperator($operator, $received, $min, $max)) {
80 17
            return false;
81
        }
82
83 18
        $this->message = "Received number is not {$min} {$operator} {$max}";
84
85 18
        return true;
86
    }
87
88
    /**
89
     * Perform correct operation from passed operator.
90
     *
91
     * @param string    $operator
92
     * @param int|float $numberReceived
93
     * @param int|float $min
94
     * @param int|float $max
95
     *
96
     * @return bool
97
     *
98
     * @throws UnexpectedValueException if unknown operator is provided.
99
     */
100 36
    private function switchOperator(string $operator, &$numberReceived, &$min, &$max): bool
101
    {
102
        switch ($operator) {
103 36
            case '><': //inside interval exclusive
104 10
                return $numberReceived > $min && $numberReceived < $max;
105 26
            case '>=<': //inside interval inclusive
106 15
                return $numberReceived >= $min && $numberReceived <= $max;
107 11
            case '<>': //outside interval exclusive
108 5
                return $numberReceived < $min || $numberReceived > $max;
109 6
            case '<=>': //outside interval inclusive
110 5
                return $numberReceived <= $min || $numberReceived >= $max;;
111
            default:
112 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted ><, <>, >=<, <=>");
113
        }
114
    }
115
116
    /**
117
     * Return error message.
118
     *
119
     * @return string Error message
120
     */
121 8
    public function getMessage(): string
122
    {
123 8
        return $this->message;
124
    }
125
}
126