1
|
|
|
<?php |
2
|
|
|
namespace AlgoWeb\xsdTypes\Facets; |
3
|
|
|
|
4
|
|
|
trait LengthTrait |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @Exclude |
8
|
|
|
* @var int Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
9
|
|
|
*/ |
10
|
|
|
private $maxLength = null; |
11
|
|
|
/** |
12
|
|
|
* @Exclude |
13
|
|
|
* @var int Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
14
|
|
|
*/ |
15
|
|
|
private $minLength = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param int $value Specifies the exact number of characters or list items allowed. Must be equal to or |
19
|
|
|
* greater than zero |
20
|
|
|
*/ |
21
|
|
|
protected function setLengthFacet($value) |
22
|
|
|
{ |
23
|
|
|
$this->setMinLengthFacet($value); |
24
|
|
|
$this->setMaxLengthFacet($value); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param int $value Specifies the minimum number of characters or list items allowed. Must be equal to or |
29
|
|
|
* greater than zero |
30
|
|
|
*/ |
31
|
|
|
protected function setMinLengthFacet($value) |
32
|
|
|
{ |
33
|
|
|
$this->checkValidMinMaxLength($value); |
34
|
|
|
$this->minLength = $value; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $value |
39
|
|
|
* @param int $min |
40
|
|
|
*/ |
41
|
|
|
private function checkValidMinMaxLength($value, $min = 0) |
42
|
|
|
{ |
43
|
|
|
if (((int)$value) != $value) { |
44
|
|
|
throw new \InvalidArgumentException('Length values MUST be castable to int ' . get_class($this)); |
45
|
|
|
} |
46
|
|
|
if ($min >= $value) { |
47
|
|
|
throw new \InvalidArgumentException('Length values MUST be greater than 0 ' . get_class($this)); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param int $value Specifies the maximum number of characters or list items allowed. Must be equal to or |
53
|
|
|
* greater than zero |
54
|
|
|
*/ |
55
|
|
|
protected function setMaxLengthFacet($value) |
56
|
|
|
{ |
57
|
|
|
$this->checkValidMinMaxLength($value); |
58
|
|
|
$this->maxLength = $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function checkLength($v) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (null != $this->minLength) { |
64
|
|
|
$this->checkMinLength($v); |
65
|
|
|
} |
66
|
|
|
if (null != $this->maxLength) { |
67
|
|
|
$this->checkMaxLength($v); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
private function checkMinLength($v) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$len = $this->getLengthForValue($v); |
74
|
|
|
if ($len > $this->minLength) { |
75
|
|
|
throw new \InvalidArgumentException( |
76
|
|
|
'The provided value for ' . get_class($this) . ' is too long - MinLength: ' |
77
|
|
|
. $this->minLength |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getLengthForValue($v) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if (is_array($v)) { |
85
|
|
|
return count($v); |
86
|
|
|
} |
87
|
|
|
return strlen($v); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
private function checkMaxLength($v) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$len = $this->getLengthForValue($v); |
93
|
|
|
if ($len < $this->maxLength) { |
94
|
|
|
throw new \InvalidArgumentException( |
95
|
|
|
'The provided value for ' . get_class($this) . ' is too short - MaxLength: ' |
96
|
|
|
. $this->maxLength |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.