|
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
|
16 |
|
public function __construct(int $min = null, int $max = null, $inclusive = true) |
|
64
|
|
|
{ |
|
65
|
16 |
|
$this->minValue = $min; |
|
66
|
16 |
|
$this->maxValue = $max; |
|
67
|
16 |
|
$this->inclusive = $inclusive; |
|
68
|
|
|
|
|
69
|
16 |
|
if (null !== $max && $min > $max) { |
|
70
|
1 |
|
throw new ComponentException(sprintf('%d cannot be less than %d for validation', $min, $max)); |
|
71
|
|
|
} |
|
72
|
15 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
38 |
|
public function validate($input): bool |
|
78
|
|
|
{ |
|
79
|
38 |
|
$length = $this->extractLength($input); |
|
80
|
38 |
|
if (null === $length) { |
|
81
|
1 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
37 |
|
return $this->validateMin($length) && $this->validateMax($length); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
38 |
|
private function extractLength($input): ?int |
|
88
|
|
|
{ |
|
89
|
38 |
|
if (is_string($input)) { |
|
90
|
28 |
|
return mb_strlen($input, mb_detect_encoding($input)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
14 |
|
if (is_array($input) || $input instanceof CountableInterface) { |
|
94
|
9 |
|
return count($input); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
8 |
|
if (is_object($input)) { |
|
98
|
3 |
|
return $this->extractLength(get_object_vars($input)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
5 |
|
if (is_int($input)) { |
|
102
|
4 |
|
return $this->extractLength((string) $input); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
37 |
|
private function validateMin(int $length): bool |
|
109
|
|
|
{ |
|
110
|
37 |
|
if (null === $this->minValue) { |
|
111
|
3 |
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
34 |
|
if ($this->inclusive) { |
|
115
|
26 |
|
return $length >= $this->minValue; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return $length > $this->minValue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
29 |
|
private function validateMax(int $length): bool |
|
122
|
|
|
{ |
|
123
|
29 |
|
if (null === $this->maxValue) { |
|
124
|
4 |
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
26 |
|
if ($this->inclusive) { |
|
128
|
19 |
|
return $length <= $this->maxValue; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
7 |
|
return $length < $this->maxValue; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|