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

NumberCompare::concreteValidate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 3
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
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, RuleValidateInterface
20
{
21
    /**
22
     * @var array Rule properties
23
     */
24
    public static $config = [
25
        'class' => 'NumberCompare',
26
        'full_class' => __CLASS__,
27
        'alias' => ['numbercompare', 'numcmp', 'nc'],
28
        'args_count' => 2,
29
        'args_type' => ['string', '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 62
    public function validate(): bool
45
    {
46 62
        $args = func_get_args();
47
48 62
        return $this->concreteValidate($args[0], $args[1], $args[2]);
49
    }
50
51
    /**
52
     * Concrete validate.
53
     *
54
     * @param int|float $received
55
     * @param string    $operator
56
     * @param int|float $compare
57
     *
58
     * @return bool
59
     */
60 62
    private function concreteValidate($received, string $operator, $compare): bool
61
    {
62 62
        if (!is_numeric($received)) {
0 ignored issues
show
introduced by
The condition is_numeric($received) is always true.
Loading history...
63 1
            return true;
64
        }
65
66 61
        if (!is_numeric($compare)) {
0 ignored issues
show
introduced by
The condition is_numeric($compare) is always true.
Loading history...
67 1
            return true;
68
        }
69
70 60
        settype($received, 'float');
71 60
        settype($compare, 'float');
72
73 60
        if ($this->switchOperator($operator, $received, $compare)) {
74 24
            return false;
75
        }
76
77 35
        $this->message = "Received number is not {$operator} {$compare}";
78
79 35
        return true;
80
    }
81
82
    /**
83
     * Perform correct operation from passed operator.
84
     *
85
     * @param string    $operator
86
     * @param int|float $numberReceived
87
     * @param int|float $numberCompare
88
     *
89
     * @return bool
90
     *
91
     * @throws UnexpectedValueException if unknown operator is provided.
92
     */
93 60
    private function switchOperator(string $operator, &$numberReceived, &$numberCompare): bool
94
    {
95
        switch ($operator) {
96 60
            case '>': //greater than
97 20
                return $numberReceived > $numberCompare;
98 42
            case '<': //less than
99 17
                return $numberReceived < $numberCompare;
100 25
            case '>=': //greater than or equal
101 6
                return $numberReceived >= $numberCompare;
102 19
            case '<=': //less than or equal
103 3
                return $numberReceived <= $numberCompare;
104 16
            case '=': //equal
105 15
                return !($numberReceived - $numberCompare);
106
            default:
107 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =");
108
        }
109
    }
110
111
    /**
112
     * Return error message.
113
     *
114
     * @return string Error message
115
     */
116 12
    public function getMessage(): string
117
    {
118 12
        return $this->message;
119
    }
120
}
121