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
|
|
|
namespace Respect\Validation\Rules; |
13
|
|
|
|
14
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
15
|
|
|
|
16
|
|
|
class Length extends AbstractRule |
17
|
|
|
{ |
18
|
|
|
public $minValue; |
19
|
|
|
public $maxValue; |
20
|
|
|
public $inclusive; |
21
|
|
|
|
22
|
45 |
|
public function __construct($min = null, $max = null, $inclusive = true) |
23
|
|
|
{ |
24
|
45 |
|
$this->minValue = $min; |
25
|
45 |
|
$this->maxValue = $max; |
26
|
45 |
|
$this->inclusive = $inclusive; |
27
|
45 |
|
$paramValidator = new AnyOf(new NumericVal(), new NullType()); |
28
|
45 |
|
if (!$paramValidator->validate($min)) { |
29
|
1 |
|
throw new ComponentException( |
30
|
1 |
|
sprintf('%s is not a valid numeric length', $min) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
44 |
|
if (!$paramValidator->validate($max)) { |
35
|
1 |
|
throw new ComponentException( |
36
|
1 |
|
sprintf('%s is not a valid numeric length', $max) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
43 |
|
if (!is_null($min) && !is_null($max) && $min > $max) { |
41
|
1 |
|
throw new ComponentException( |
42
|
1 |
|
sprintf('%s cannot be less than %s for validation', $min, $max) |
43
|
|
|
); |
44
|
|
|
} |
45
|
42 |
|
} |
46
|
|
|
|
47
|
40 |
|
public function validate($input) |
48
|
|
|
{ |
49
|
40 |
|
$length = $this->extractLength($input); |
50
|
|
|
|
51
|
40 |
|
return $this->validateMin($length) && $this->validateMax($length); |
52
|
|
|
} |
53
|
|
|
|
54
|
40 |
|
protected function extractLength($input) |
55
|
|
|
{ |
56
|
40 |
|
if (is_string($input)) { |
57
|
31 |
|
return mb_strlen($input, mb_detect_encoding($input)); |
58
|
|
|
} |
59
|
|
|
|
60
|
11 |
|
if (is_array($input) || $input instanceof \Countable) { |
61
|
4 |
|
return count($input); |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
if (is_object($input)) { |
65
|
3 |
|
return count(get_object_vars($input)); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
if (is_int($input)) { |
69
|
2 |
|
return mb_strlen((string) $input); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
40 |
|
protected function validateMin($length) |
76
|
|
|
{ |
77
|
40 |
|
if (is_null($this->minValue)) { |
78
|
3 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
37 |
|
if ($this->inclusive) { |
82
|
29 |
|
return $length >= $this->minValue; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
return $length > $this->minValue; |
86
|
|
|
} |
87
|
|
|
|
88
|
30 |
|
protected function validateMax($length) |
89
|
|
|
{ |
90
|
30 |
|
if (is_null($this->maxValue)) { |
91
|
3 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
27 |
|
if ($this->inclusive) { |
95
|
20 |
|
return $length <= $this->maxValue; |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
return $length < $this->maxValue; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|