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)) { |
|
|
|
|
63
|
1 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
61 |
|
if (!is_numeric($compare)) { |
|
|
|
|
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
|
|
|
|