Completed
Push — master ( eaf770...e2d746 )
by Sebastian
04:09
created

NumberInterval::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
20
{
21
    /**
22
     * @var array Arguments expected.
23
     */
24
    private $arguments = ['string', 'number', 'number'];
0 ignored issues
show
introduced by
The private property $arguments is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var string Error message
28
     */
29
    private $message = '';
30
31
    /**
32
     * Validate.
33
     *
34
     * @param int|float $received
35
     * @param string    $operator
36
     * @param int|float $min
37
     * @param int|float $max
38
     *
39
     * @return bool
40
     */
41 39
    public function validate($received, string $operator, $min, $max): bool
42
    {
43 39
        if (!is_numeric($received)) {
0 ignored issues
show
introduced by
The condition is_numeric($received) is always true.
Loading history...
44 1
            return true;
45
        }
46
47 38
        if (!is_numeric($min)) {
0 ignored issues
show
introduced by
The condition is_numeric($min) is always true.
Loading history...
48 1
            return true;
49
        }
50
51 37
        if (!is_numeric($max)) {
0 ignored issues
show
introduced by
The condition is_numeric($max) is always true.
Loading history...
52 1
            return true;
53
        }
54
55 36
        settype($received, 'float');
56 36
        settype($min, 'float');
57 36
        settype($max, 'float');
58
59 36
        if ($this->switchOperator($operator, $received, $min, $max)) {
60 17
            return false;
61
        }
62
63 18
        $this->message = "Received number is not {$min} {$operator} {$max}";
64
65 18
        return true;
66
    }
67
68
    /**
69
     * Perform correct operation from passed operator.
70
     *
71
     * @param string    $operator
72
     * @param int|float $numberReceived
73
     * @param int|float $min
74
     * @param int|float $max
75
     *
76
     * @return bool
77
     *
78
     * @throws UnexpectedValueException if unknown operator is provided.
79
     */
80 36
    private function switchOperator(string $operator, &$numberReceived, &$min, &$max): bool
81
    {
82
        switch ($operator) {
83 36
            case '><': //inside interval exclusive
84 10
                return $numberReceived > $min && $numberReceived < $max;
85 26
            case '>=<': //inside interval inclusive
86 15
                return $numberReceived >= $min && $numberReceived <= $max;
87 11
            case '<>': //outside interval exclusive
88 5
                return $numberReceived < $min || $numberReceived > $max;
89 6
            case '<=>': //outside interval inclusive
90 5
                return $numberReceived <= $min || $numberReceived >= $max;;
91
            default:
92 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted ><, <>, >=<, <=>");
93
        }
94
    }
95
96
    /**
97
     * Return error message.
98
     *
99
     * @return string Error message
100
     */
101 8
    public function getMessage(): string
102
    {
103 8
        return $this->message;
104
    }
105
}
106