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, RuleValidateInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array Rule properties |
23
|
|
|
*/ |
24
|
|
|
public static $config = [ |
25
|
|
|
'class' => 'NumberInterval', |
26
|
|
|
'full_class' => __CLASS__, |
27
|
|
|
'alias' => ['numberinterval', 'numint', 'ni'], |
28
|
|
|
'args_count' => 3, |
29
|
|
|
'args_type' => ['string', 'number', '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
|
39 |
|
public function validate(): bool |
45
|
|
|
{ |
46
|
39 |
|
$args = func_get_args(); |
47
|
|
|
|
48
|
39 |
|
return $this->concreteValidate($args[0], $args[1], $args[2], $args[3]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Concrete validate. |
53
|
|
|
* |
54
|
|
|
* @param int|float $received |
55
|
|
|
* @param string $operator |
56
|
|
|
* @param int|float $min |
57
|
|
|
* @param int|float $max |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
39 |
|
private function concreteValidate($received, string $operator, $min, $max): bool |
62
|
|
|
{ |
63
|
39 |
|
if (!is_numeric($received)) { |
|
|
|
|
64
|
1 |
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
38 |
|
if (!is_numeric($min)) { |
|
|
|
|
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
37 |
|
if (!is_numeric($max)) { |
|
|
|
|
72
|
1 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
36 |
|
settype($received, 'float'); |
76
|
36 |
|
settype($min, 'float'); |
77
|
36 |
|
settype($max, 'float'); |
78
|
|
|
|
79
|
36 |
|
if ($this->switchOperator($operator, $received, $min, $max)) { |
80
|
17 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
18 |
|
$this->message = "Received number is not {$min} {$operator} {$max}"; |
84
|
|
|
|
85
|
18 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Perform correct operation from passed operator. |
90
|
|
|
* |
91
|
|
|
* @param string $operator |
92
|
|
|
* @param int|float $numberReceived |
93
|
|
|
* @param int|float $min |
94
|
|
|
* @param int|float $max |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
* |
98
|
|
|
* @throws UnexpectedValueException if unknown operator is provided. |
99
|
|
|
*/ |
100
|
36 |
|
private function switchOperator(string $operator, &$numberReceived, &$min, &$max): bool |
101
|
|
|
{ |
102
|
|
|
switch ($operator) { |
103
|
36 |
|
case '><': //inside interval exclusive |
104
|
10 |
|
return $numberReceived > $min && $numberReceived < $max; |
105
|
26 |
|
case '>=<': //inside interval inclusive |
106
|
15 |
|
return $numberReceived >= $min && $numberReceived <= $max; |
107
|
11 |
|
case '<>': //outside interval exclusive |
108
|
5 |
|
return $numberReceived < $min || $numberReceived > $max; |
109
|
6 |
|
case '<=>': //outside interval inclusive |
110
|
5 |
|
return $numberReceived <= $min || $numberReceived >= $max;; |
111
|
|
|
default: |
112
|
1 |
|
throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted ><, <>, >=<, <=>"); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return error message. |
118
|
|
|
* |
119
|
|
|
* @return string Error message |
120
|
|
|
*/ |
121
|
8 |
|
public function getMessage(): string |
122
|
|
|
{ |
123
|
8 |
|
return $this->message; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|