|
1
|
|
|
<?php |
|
2
|
|
|
namespace AlgoWeb\xsdTypes; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* The type xsd:anySimpleType is the base type from which all other built-in types are derived. Any value (including an empty value) is allowed for xsd:anySimpleType. |
|
6
|
|
|
* handles facets enumeration, length, maxLength, minLength, pattern |
|
7
|
|
|
* |
|
8
|
|
|
* @package AlgoWeb\xsdTypes |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class xsAnySimpleType |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @Exclude |
|
14
|
|
|
* @var array Defines a list of acceptable values |
|
15
|
|
|
*/ |
|
16
|
|
|
private $enumeration = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @Exclude |
|
20
|
|
|
* @var integer Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
|
21
|
|
|
*/ |
|
22
|
|
|
private $maxLength = null; |
|
23
|
|
|
/** |
|
24
|
|
|
* @Exclude |
|
25
|
|
|
* @var integer Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
|
26
|
|
|
*/ |
|
27
|
|
|
private $minLength = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @Exclude |
|
31
|
|
|
* @var string Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |
|
32
|
|
|
*/ |
|
33
|
|
|
private $whiteSpace = "preserve"; |
|
34
|
|
|
/** |
|
35
|
|
|
* @Exclude |
|
36
|
|
|
* @var string Defines the exact sequence of characters that are acceptable |
|
37
|
|
|
*/ |
|
38
|
|
|
private $pattern = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @property mixed $__value |
|
42
|
|
|
*/ |
|
43
|
|
|
private $__value = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Construct |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $value |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($value) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->__value = $this->fixValueInteral($value); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function fixValueInteral($value) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->fixValue($this->fixWhitespace($value)); |
|
58
|
|
|
} |
|
59
|
|
|
abstract protected function fixValue($value); |
|
60
|
|
|
protected function fixWhitespace($val) |
|
61
|
|
|
{ |
|
62
|
|
|
switch ($this->whiteSpace) { |
|
63
|
|
|
case "preserve": |
|
64
|
|
|
return $val; |
|
65
|
|
|
case "replace": |
|
66
|
|
|
return preg_replace('/\s/', ' ', $val); |
|
67
|
|
|
case "collapse": |
|
68
|
|
|
return preg_replace('/\s+/', ' ', $val); |
|
69
|
|
|
default: |
|
70
|
|
|
throw new \InvalidArgumentException(__CLASS__ . " Called Fix whitespace with invalid handle operation"); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
protected function isOKInternal($value) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->checkEnumeration($value); |
|
76
|
|
|
$this->checkMaxLength($value); |
|
77
|
|
|
$this->checkMinLength($value); |
|
78
|
|
|
$this->checkPattern($value); |
|
79
|
|
|
return $this->isOK($value); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function checkEnumeration($v) |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_array($this->enumeration) && 0 != count($this->enumeration) && !in_array($v, $this->enumeration)) { |
|
85
|
|
|
throw new \InvalidArgumentException( |
|
86
|
|
|
"the provided value for " . __CLASS__ . " is not " . |
|
87
|
|
|
implode(" || ", $this->enumeration) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
private function checkMaxLength($v) |
|
93
|
|
|
{ |
|
94
|
|
|
$stringLen = strlen($v); |
|
95
|
|
|
if ($this->maxLength != null) { |
|
96
|
|
|
if ($stringLen < $this->maxLength) { |
|
97
|
|
|
throw new \InvalidArgumentException( |
|
98
|
|
|
"the provided value for " . __CLASS__ . " is to short MaxLength: " |
|
99
|
|
|
. $this->maxLength |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
private function checkMinLength($v) |
|
106
|
|
|
{ |
|
107
|
|
|
$stringLen = strlen($v); |
|
108
|
|
|
if ($this->minLength != null) { |
|
109
|
|
|
if ($stringLen > $this->minLength) { |
|
110
|
|
|
throw new \InvalidArgumentException( |
|
111
|
|
|
"the provided value for " . __CLASS__ . " is to long minLength: " |
|
112
|
|
|
. $this->minLength |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function checkPattern($v) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($this->pattern != null) { |
|
121
|
|
|
if (!$this->matchesRegexPattern($this->pattern, $v)) { |
|
122
|
|
|
throw new \InvalidArgumentException("assigned value that dose not match pattern " . __CLASS__); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Checks a pattern against a string |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $pattern the regex pattern |
|
131
|
|
|
* @param string $string the string to check |
|
132
|
|
|
* @return bool true if string matches pattern |
|
133
|
|
|
*/ |
|
134
|
|
|
private function matchesRegexPattern($pattern, $string) |
|
135
|
|
|
{ |
|
136
|
|
|
$matches = null; |
|
137
|
|
|
return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
abstract protected function isOK($value); |
|
141
|
|
|
|
|
142
|
|
|
protected function setLengthFacet($value) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->setMinLengthFacet($value); |
|
145
|
|
|
$this->setMaxLengthFacet($value); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function setMinLengthFacet($value) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->checkValidMinMaxLength($value); |
|
151
|
|
|
$this->minLength = $value; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
private function checkValidMinMaxLength($value, $min = 0) |
|
155
|
|
|
{ |
|
156
|
|
|
if (((int)$value) != $value) { |
|
157
|
|
|
throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__); |
|
158
|
|
|
} |
|
159
|
|
|
if ($min >= $value) { |
|
160
|
|
|
throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function setMaxLengthFacet($value) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->checkValidMinMaxLength($value); |
|
167
|
|
|
$this->maxLength = $value; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function setWhiteSpaceFacet($value) |
|
171
|
|
|
{ |
|
172
|
|
|
if (!in_array($value, ["preserve", "replace", "collapse"])) { |
|
173
|
|
|
throw new \InvalidArgumentException("Invalid white space handleing method " . __CLASS__); |
|
174
|
|
|
} |
|
175
|
|
|
$this->whiteSpace = $value; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
protected function setPatternFacet($value) |
|
179
|
|
|
{ |
|
180
|
|
|
if (!$this->checkRegexValidPattern($value)) { |
|
181
|
|
|
$value = "/" . $value . "/"; |
|
182
|
|
|
if (!$this->checkRegexValidPattern($value)) { |
|
183
|
|
|
throw new \InvalidArgumentException("Invalid regex Pattern provided: " . __CLASS__); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
$this->pattern = $value; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
private function checkRegexValidPattern($pattern) |
|
190
|
|
|
{ |
|
191
|
|
|
return (@preg_match($pattern, null) === false); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|