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