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 Respect\Validation\Exceptions\ComponentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
20
|
|
|
* @author Blake Hair <[email protected]> |
21
|
|
|
* @author Danilo Correa <[email protected]> |
22
|
|
|
* @author Henrique Moody <[email protected]> |
23
|
|
|
* @author Hugo Hamon <[email protected]> |
24
|
|
|
* @author João Torquato <[email protected]> |
25
|
|
|
* @author Marcelo Araujo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class Length extends AbstractRule |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $minValue; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $maxValue; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $inclusive; |
43
|
|
|
|
44
|
15 |
|
public function __construct(int $min = null, int $max = null, $inclusive = true) |
45
|
|
|
{ |
46
|
15 |
|
$this->minValue = $min; |
47
|
15 |
|
$this->maxValue = $max; |
48
|
15 |
|
$this->inclusive = $inclusive; |
49
|
15 |
|
$paramValidator = new AnyOf(new NumericVal(), new NullType()); |
50
|
15 |
|
if (!$paramValidator->validate($min)) { |
51
|
|
|
throw new ComponentException( |
52
|
|
|
sprintf('%s is not a valid numeric length', $min) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
15 |
|
if (!$paramValidator->validate($max)) { |
57
|
|
|
throw new ComponentException( |
58
|
|
|
sprintf('%s is not a valid numeric length', $max) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
15 |
|
if (!is_null($min) && !is_null($max) && $min > $max) { |
63
|
|
|
throw new ComponentException( |
64
|
|
|
sprintf('%s cannot be less than %s for validation', $min, $max) |
65
|
|
|
); |
66
|
|
|
} |
67
|
15 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
33 |
|
public function validate($input): bool |
73
|
|
|
{ |
74
|
33 |
|
$length = $this->extractLength($input); |
75
|
|
|
|
76
|
33 |
|
return $this->validateMin($length) && $this->validateMax($length); |
77
|
|
|
} |
78
|
|
|
|
79
|
33 |
|
protected function extractLength($input) |
80
|
|
|
{ |
81
|
33 |
|
if (is_string($input)) { |
82
|
24 |
|
return mb_strlen($input, mb_detect_encoding($input)); |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
if (is_array($input) || $input instanceof \Countable) { |
86
|
4 |
|
return count($input); |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
if (is_object($input)) { |
90
|
3 |
|
return count(get_object_vars($input)); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
if (is_int($input)) { |
94
|
2 |
|
return mb_strlen((string) $input); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
33 |
|
protected function validateMin($length) |
101
|
|
|
{ |
102
|
33 |
|
if (is_null($this->minValue)) { |
|
|
|
|
103
|
3 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
30 |
|
if ($this->inclusive) { |
107
|
22 |
|
return $length >= $this->minValue; |
108
|
|
|
} |
109
|
|
|
|
110
|
8 |
|
return $length > $this->minValue; |
111
|
|
|
} |
112
|
|
|
|
113
|
25 |
|
protected function validateMax($length) |
114
|
|
|
{ |
115
|
25 |
|
if (is_null($this->maxValue)) { |
|
|
|
|
116
|
3 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
22 |
|
if ($this->inclusive) { |
120
|
15 |
|
return $length <= $this->maxValue; |
121
|
|
|
} |
122
|
|
|
|
123
|
7 |
|
return $length < $this->maxValue; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|