1
|
|
|
<?php |
2
|
|
|
namespace AlgoWeb\xsdTypes\Facets; |
3
|
|
|
|
4
|
|
|
trait LengthTrait |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* @Exclude |
8
|
|
|
* @var integer 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 integer 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 integer $value Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero |
19
|
|
|
*/ |
20
|
|
|
protected function setLengthFacet($value) |
21
|
|
|
{ |
22
|
|
|
$this->setMinLengthFacet($value); |
23
|
|
|
$this->setMaxLengthFacet($value); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param integer $value Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
28
|
|
|
*/ |
29
|
|
|
protected function setMinLengthFacet($value) |
30
|
|
|
{ |
31
|
|
|
$this->checkValidMinMaxLength($value); |
32
|
|
|
$this->minLength = $value; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param integer $value |
37
|
|
|
*/ |
38
|
|
|
private function checkValidMinMaxLength($value, $min = 0) |
39
|
|
|
{ |
40
|
|
|
if (((int)$value) != $value) { |
41
|
|
|
throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__); |
42
|
|
|
} |
43
|
|
|
if ($min >= $value) { |
44
|
|
|
throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param integer $value Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
50
|
|
|
*/ |
51
|
|
|
protected function setMaxLengthFacet($value) |
52
|
|
|
{ |
53
|
|
|
$this->checkValidMinMaxLength($value); |
54
|
|
|
$this->maxLength = $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function checkMaxLength($v) |
58
|
|
|
{ |
59
|
|
|
if ($this->maxLength != null) { |
60
|
|
|
if ($this->minLength != null) { |
61
|
|
|
if (is_array($v)) { |
62
|
|
|
$this->checkMaxLengthArray($v); |
63
|
|
|
} else { |
64
|
|
|
$this->checkMaxLengthString($v); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
private function checkMaxLengthArray($v) |
71
|
|
|
{ |
72
|
|
|
assert(is_array($v)); |
73
|
|
|
$arrayLen = count($v); |
74
|
|
|
if ($arrayLen < $this->maxLength) { |
75
|
|
|
throw new \InvalidArgumentException( |
76
|
|
|
"the provided value for " . __CLASS__ . " is to short MaxLength: " |
77
|
|
|
. $this->maxLength |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
private function checkMaxLengthString($v) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$stringLen = strlen($v); |
85
|
|
|
if ($stringLen < $this->maxLength) { |
86
|
|
|
throw new \InvalidArgumentException( |
87
|
|
|
"the provided value for " . __CLASS__ . " is to short MaxLength: " |
88
|
|
|
. $this->maxLength |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function checkMinLength($v) |
94
|
|
|
{ |
95
|
|
|
if ($this->minLength != null) { |
96
|
|
|
if (is_array($v)) { |
97
|
|
|
$this->checkMinLengthArray($v); |
98
|
|
|
} else { |
99
|
|
|
$this->checkMinLengthString($v); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
private function checkMinLengthArray($v) |
105
|
|
|
{ |
106
|
|
|
assert(is_array($v)); |
107
|
|
|
$arrayLen = count($v); |
108
|
|
|
if ($arrayLen > $this->minLength) { |
109
|
|
|
throw new \InvalidArgumentException( |
110
|
|
|
"the provided value for " . __CLASS__ . " is to long MinLength: " |
111
|
|
|
. $this->minLength |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
private function checkMinLengthString($v) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$stringLen = strlen($v); |
119
|
|
|
if ($stringLen > $this->minLength) { |
120
|
|
|
throw new \InvalidArgumentException( |
121
|
|
|
"the provided value for " . __CLASS__ . " is to long MinLength: " |
122
|
|
|
. $this->minLength |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function getLengthForValue($v) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
if (is_array($v)) { |
130
|
|
|
return count($v); |
131
|
|
|
} |
132
|
|
|
return strlen($v); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.