|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
|
15
|
|
|
|
|
16
|
|
|
use Countable as CountableInterface; |
|
17
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
18
|
|
|
use function count; |
|
19
|
|
|
use function is_array; |
|
20
|
|
|
use function is_int; |
|
21
|
|
|
use function is_object; |
|
22
|
|
|
use function is_string; |
|
23
|
|
|
use function mb_detect_encoding; |
|
24
|
|
|
use function mb_strlen; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Validates the length of the given input. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
|
30
|
|
|
* @author Blake Hair <[email protected]> |
|
31
|
|
|
* @author Danilo Correa <[email protected]> |
|
32
|
|
|
* @author Henrique Moody <[email protected]> |
|
33
|
|
|
* @author Hugo Hamon <[email protected]> |
|
34
|
|
|
* @author João Torquato <[email protected]> |
|
35
|
|
|
* @author Marcelo Araujo <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
final class Length extends AbstractRule |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
private $minValue; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
private $maxValue; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
private $inclusive; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Creates the rule with a minimum and maximum value. |
|
56
|
|
|
* |
|
57
|
|
|
* @param int|null $min |
|
58
|
|
|
* @param int|null $max |
|
59
|
|
|
* @param bool $inclusive TRUE by default |
|
60
|
|
|
* |
|
61
|
|
|
* @throws ComponentException |
|
62
|
|
|
*/ |
|
63
|
15 |
|
public function __construct(int $min = null, int $max = null, $inclusive = true) |
|
64
|
|
|
{ |
|
65
|
15 |
|
$this->minValue = $min; |
|
66
|
15 |
|
$this->maxValue = $max; |
|
67
|
15 |
|
$this->inclusive = $inclusive; |
|
68
|
|
|
|
|
69
|
15 |
|
if (null !== $min && null !== $max && $min > $max) { |
|
70
|
|
|
throw new ComponentException( |
|
71
|
|
|
sprintf('%s cannot be less than %s for validation', $min, $max) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
15 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
35 |
|
public function validate($input): bool |
|
80
|
|
|
{ |
|
81
|
35 |
|
$length = $this->extractLength($input); |
|
82
|
35 |
|
if (null === $length) { |
|
83
|
1 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
34 |
|
return $this->validateMin($length) && $this->validateMax($length); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
35 |
|
private function extractLength($input): ?int |
|
90
|
|
|
{ |
|
91
|
35 |
|
if (is_string($input)) { |
|
92
|
26 |
|
return mb_strlen($input, mb_detect_encoding($input)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
11 |
|
if (is_array($input) || $input instanceof CountableInterface) { |
|
96
|
8 |
|
return count($input); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
if (is_object($input)) { |
|
100
|
3 |
|
return $this->extractLength(get_object_vars($input)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
if (is_int($input)) { |
|
104
|
2 |
|
return $this->extractLength((string) $input); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
34 |
|
private function validateMin(int $length): bool |
|
111
|
|
|
{ |
|
112
|
34 |
|
if (null === $this->minValue) { |
|
113
|
3 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
31 |
|
if ($this->inclusive) { |
|
117
|
23 |
|
return $length >= $this->minValue; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
8 |
|
return $length > $this->minValue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
26 |
|
private function validateMax(int $length): bool |
|
124
|
|
|
{ |
|
125
|
26 |
|
if (null === $this->maxValue) { |
|
126
|
4 |
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
23 |
|
if ($this->inclusive) { |
|
130
|
16 |
|
return $length <= $this->maxValue; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
7 |
|
return $length < $this->maxValue; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|