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

NumberCompare::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
 * Compare two numbers using >, <, >=, <=, = operators.
18
 */
19
class NumberCompare extends AbstractNumber implements RuleSanitizeInterface
20
{
21
    /**
22
     * @var array Arguments expected.
23
     */
24
    private $arguments = ['string', '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 $compare
37
     *
38
     * @return bool
39
     */
40 62
    public function validate($received, string $operator, $compare): bool
41
    {
42 62
        if (!is_numeric($received)) {
0 ignored issues
show
introduced by
The condition is_numeric($received) is always true.
Loading history...
43 1
            return true;
44
        }
45
46 61
        if (!is_numeric($compare)) {
0 ignored issues
show
introduced by
The condition is_numeric($compare) is always true.
Loading history...
47 1
            return true;
48
        }
49
50 60
        settype($received, 'float');
51 60
        settype($compare, 'float');
52
53 60
        if ($this->switchOperator($operator, $received, $compare)) {
54 24
            return false;
55
        }
56
57 35
        $this->message = "Received number is not {$operator} {$compare}";
58
59 35
        return true;
60
    }
61
62
    /**
63
     * Perform correct operation from passed operator.
64
     *
65
     * @param string    $operator
66
     * @param int|float $numberReceived
67
     * @param int|float $numberCompare
68
     *
69
     * @return bool
70
     *
71
     * @throws UnexpectedValueException if unknown operator is provided.
72
     */
73 60
    private function switchOperator(string $operator, &$numberReceived, &$numberCompare): bool
74
    {
75
        switch ($operator) {
76 60
            case '>': //greater than
77 20
                return $numberReceived > $numberCompare;
78 42
            case '<': //less than
79 17
                return $numberReceived < $numberCompare;
80 25
            case '>=': //greater than or equal
81 6
                return $numberReceived >= $numberCompare;
82 19
            case '<=': //less than or equal
83 3
                return $numberReceived <= $numberCompare;
84 16
            case '=': //equal
85 15
                return !($numberReceived - $numberCompare);
86
            default:
87 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =");
88
        }
89
    }
90
91
    /**
92
     * Return error message.
93
     *
94
     * @return string Error message
95
     */
96 12
    public function getMessage(): string
97
    {
98 12
        return $this->message;
99
    }
100
}
101