1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bluz Framework Component |
4
|
|
|
* |
5
|
|
|
* @copyright Bluz PHP Team |
6
|
|
|
* @link https://github.com/bluzphp/framework |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Bluz\Validator\Rule; |
12
|
|
|
|
13
|
|
|
use Bluz\Validator\Exception\ComponentException; |
14
|
|
|
use Countable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Check for length in range between minimum and maximum values |
18
|
|
|
* |
19
|
|
|
* @package Bluz\Validator\Rule |
20
|
|
|
*/ |
21
|
|
|
class LengthRule extends AbstractCompareRule |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var integer minimum value |
25
|
|
|
*/ |
26
|
|
|
protected $minValue; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var integer maximum value |
30
|
|
|
*/ |
31
|
|
|
protected $maxValue; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $inclusive = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Setup validation rule |
40
|
|
|
* |
41
|
|
|
* @param integer|null $min |
42
|
|
|
* @param integer|null $max |
43
|
|
|
* |
44
|
|
|
* @throws \Bluz\Validator\Exception\ComponentException |
45
|
|
|
*/ |
46
|
18 |
|
public function __construct($min = null, $max = null) |
47
|
|
|
{ |
48
|
18 |
|
$this->minValue = $min; |
49
|
18 |
|
$this->maxValue = $max; |
50
|
|
|
|
51
|
18 |
|
if (!is_numeric($min) && !is_null($min)) { |
52
|
1 |
|
throw new ComponentException( |
53
|
1 |
|
__('"%s" is not a valid numeric length', $min) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
17 |
|
if (!is_numeric($max) && !is_null($max)) { |
58
|
1 |
|
throw new ComponentException( |
59
|
1 |
|
__('"%s" is not a valid numeric length', $max) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
16 |
|
if (!is_null($min) && !is_null($max) && $min > $max) { |
64
|
1 |
|
throw new ComponentException( |
65
|
1 |
|
__('"%s" cannot be less than "%s" for validation', $min, $max) |
66
|
|
|
); |
67
|
|
|
} |
68
|
15 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check input data |
72
|
|
|
* |
73
|
|
|
* @param string $input |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
14 |
|
public function validate($input) : bool |
78
|
|
|
{ |
79
|
14 |
|
if (!$length = $this->extractLength($input)) { |
80
|
1 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
13 |
|
return (is_null($this->minValue) || $this->less($this->minValue, $length)) |
84
|
13 |
|
&& (is_null($this->maxValue) || $this->less($length, $this->maxValue)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Extract length |
89
|
|
|
* |
90
|
|
|
* @param string|object $input |
91
|
|
|
* |
92
|
|
|
* @return integer|false |
93
|
|
|
*/ |
94
|
14 |
|
protected function extractLength($input) |
95
|
|
|
{ |
96
|
14 |
|
if (is_string($input)) { |
97
|
9 |
|
return mb_strlen($input, mb_detect_encoding($input)); |
98
|
5 |
|
} elseif (is_array($input) || $input instanceof Countable) { |
99
|
2 |
|
return count($input); |
100
|
3 |
|
} elseif (is_object($input)) { |
101
|
2 |
|
return count(get_object_vars($input)); |
102
|
|
|
} |
103
|
1 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get error template |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
13 |
|
public function getDescription() : string |
112
|
|
|
{ |
113
|
13 |
|
if (!$this->minValue) { |
114
|
1 |
|
return __('must have a length lower than "%d"', $this->maxValue); |
115
|
|
|
} |
116
|
12 |
|
if (!$this->maxValue) { |
117
|
1 |
|
return __('must have a length greater than "%d"', $this->minValue); |
118
|
|
|
} |
119
|
11 |
|
if ($this->minValue === $this->maxValue) { |
120
|
|
|
return __('must have a length "%d"', $this->minValue); |
121
|
|
|
} |
122
|
11 |
|
return __('must have a length between "%d" and "%d"', $this->minValue, $this->maxValue); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|