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 the length of provided string using >, <, >=, <=, = operators. |
18
|
|
|
*/ |
19
|
|
|
class StringLenCompare extends AbstractString implements RuleSanitizeInterface, RuleValidateInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array Rule properties |
23
|
|
|
*/ |
24
|
|
|
public static $config = [ |
25
|
|
|
'full_class' => __CLASS__, |
26
|
|
|
'alias' => ['stringlencompare', 'strlencmp', 'strlen', 'slc'], |
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
|
39 |
|
public function validate(): bool |
42
|
|
|
{ |
43
|
39 |
|
$args = \func_get_args(); |
44
|
|
|
|
45
|
39 |
|
return $this->concreteValidate($args[0], $args[1], $args[2]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Concrete validate. |
50
|
|
|
* |
51
|
|
|
* @param string $received |
52
|
|
|
* @param string $operator |
53
|
|
|
* @param int $compare |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
39 |
|
private function concreteValidate(string $received, string $operator, int $compare): bool |
58
|
|
|
{ |
59
|
39 |
|
if ($this->switchOperator($operator, $received, $compare)) { |
60
|
20 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
18 |
|
$this->message = "Received string length is not {$operator} of {$compare}"; |
64
|
|
|
|
65
|
18 |
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Perform correct operation from passed operator. |
70
|
|
|
* |
71
|
|
|
* @param string $operator |
72
|
|
|
* @param string $strReceived |
73
|
|
|
* @param int $strCompare |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
* |
77
|
|
|
* @throws UnexpectedValueException if unknown operator is provided. |
78
|
|
|
*/ |
79
|
39 |
|
private function switchOperator(string $operator, string &$strReceived, int &$strCompare): bool |
80
|
|
|
{ |
81
|
39 |
|
switch ($operator) { |
82
|
39 |
|
case '>': //greater than |
83
|
7 |
|
return \strlen($strReceived) > $strCompare; |
84
|
32 |
|
case '<': //less than |
85
|
6 |
|
return \strlen($strReceived) < $strCompare; |
86
|
26 |
|
case '>=': //greater than or equal |
87
|
9 |
|
return \strlen($strReceived) >= $strCompare; |
88
|
17 |
|
case '<=': //less than or equal |
89
|
9 |
|
return \strlen($strReceived) <= $strCompare; |
90
|
8 |
|
case '=': //equal |
91
|
4 |
|
return \strlen($strReceived) === $strCompare; |
92
|
4 |
|
case '!=': //equal |
93
|
3 |
|
return \strlen($strReceived) !== $strCompare; |
94
|
|
|
default: |
95
|
1 |
|
throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =, !="); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return error message. |
101
|
|
|
* |
102
|
|
|
* @return string Error message |
103
|
|
|
*/ |
104
|
9 |
|
public function getMessage(): string |
105
|
|
|
{ |
106
|
9 |
|
return $this->message; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|