|
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
|
|
|
'full_class' => __CLASS__, |
|
26
|
|
|
'alias' => ['numbercompare', 'numcmp', 'nc'], |
|
27
|
|
|
'args_count' => 2, |
|
28
|
|
|
'args_type' => ['string', 'number'] |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string Error message |
|
33
|
|
|
*/ |
|
34
|
|
|
private $message = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Validate. |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
63 |
|
public function validate(): bool |
|
42
|
|
|
{ |
|
43
|
63 |
|
$args = \func_get_args(); |
|
44
|
|
|
|
|
45
|
63 |
|
return $this->concreteValidate($args[0], $args[1], $args[2]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Concrete validate. |
|
50
|
|
|
* |
|
51
|
|
|
* @param int|float $received |
|
52
|
|
|
* @param string $operator |
|
53
|
|
|
* @param int|float $compare |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
63 |
|
private function concreteValidate($received, string $operator, $compare): bool |
|
58
|
|
|
{ |
|
59
|
63 |
|
if (!\is_numeric($received)) { |
|
|
|
|
|
|
60
|
1 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
62 |
|
if (!\is_numeric($compare)) { |
|
|
|
|
|
|
64
|
1 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
61 |
|
$received = (float) $received; |
|
68
|
61 |
|
$compare = (float) $compare; |
|
69
|
|
|
|
|
70
|
61 |
|
if ($this->switchOperator($operator, $received, $compare)) { |
|
71
|
24 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
36 |
|
$this->message = "Received number is not {$operator} {$compare}"; |
|
75
|
|
|
|
|
76
|
36 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Perform correct operation from passed operator. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $operator |
|
83
|
|
|
* @param int|float $numberReceived |
|
84
|
|
|
* @param int|float $numberCompare |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
* |
|
88
|
|
|
* @throws UnexpectedValueException if unknown operator is provided. |
|
89
|
|
|
*/ |
|
90
|
61 |
|
private function switchOperator(string $operator, &$numberReceived, &$numberCompare): bool |
|
91
|
|
|
{ |
|
92
|
61 |
|
switch ($operator) { |
|
93
|
61 |
|
case '>': //greater than |
|
94
|
20 |
|
return $numberReceived > $numberCompare; |
|
95
|
43 |
|
case '<': //less than |
|
96
|
17 |
|
return $numberReceived < $numberCompare; |
|
97
|
26 |
|
case '>=': //greater than or equal |
|
98
|
6 |
|
return $numberReceived >= $numberCompare; |
|
99
|
20 |
|
case '<=': //less than or equal |
|
100
|
3 |
|
return $numberReceived <= $numberCompare; |
|
101
|
17 |
|
case '=': //equal |
|
102
|
16 |
|
return !($numberReceived - $numberCompare); |
|
103
|
|
|
default: |
|
104
|
1 |
|
throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, ="); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return error message. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string Error message |
|
112
|
|
|
*/ |
|
113
|
13 |
|
public function getMessage(): string |
|
114
|
|
|
{ |
|
115
|
13 |
|
return $this->message; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|