1
|
|
|
<?php |
2
|
|
|
namespace GoetasWebservices\XML\XSDReader\Utils; |
3
|
|
|
|
4
|
|
|
use GoetasWebservices\XML\XSDReader\Exception\RestrictionException; |
5
|
|
|
|
6
|
|
|
class RestrictionUtils |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Defines a list of acceptable values. |
11
|
|
|
* |
12
|
|
|
* @param mixed $value |
13
|
|
|
* @param array $enumeration |
14
|
|
|
* @throws RestrictionException |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
2 |
|
public static function checkEnumeration($value, $enumeration) |
18
|
|
|
{ |
19
|
2 |
|
if (!in_array($value, $enumeration)) { |
20
|
1 |
|
$values = implode(', ', $enumeration); |
21
|
1 |
|
throw new RestrictionException( |
22
|
1 |
|
"The restriction enumeration with '$values' is not true", |
23
|
1 |
|
RestrictionException::ERROR_CODE_ENUMARATION, |
24
|
1 |
|
$value, |
25
|
1 |
|
$enumeration); |
|
|
|
|
26
|
|
|
} |
27
|
1 |
|
return $value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Defines the exact sequence of characters that are acceptable. |
32
|
|
|
* |
33
|
|
|
* @param mixed $value |
34
|
|
|
* @param string $pattern |
35
|
|
|
* @throws RestrictionException |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
2 |
|
public static function checkPattern($value, $pattern) |
39
|
|
|
{ |
40
|
2 |
|
if (!preg_match("/^{$pattern}$/", $value)) { |
41
|
1 |
|
throw new RestrictionException( |
42
|
1 |
|
"The restriction pattern with '$pattern' is not true", |
43
|
1 |
|
RestrictionException::ERROR_CODE_PATTERN, |
44
|
1 |
|
$value, |
45
|
1 |
|
$pattern); |
46
|
|
|
} |
47
|
1 |
|
return $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check is numeric valid |
52
|
|
|
* |
53
|
|
|
* @param mixed $value |
54
|
|
|
* @return mixed |
55
|
|
|
* @throws RestrictionException |
56
|
|
|
*/ |
57
|
31 |
|
private static function getNumeric($value) |
58
|
|
|
{ |
59
|
31 |
|
if (!is_numeric($value)) { |
60
|
2 |
|
throw new RestrictionException( |
61
|
2 |
|
"The '$value' is not a valid numeric", |
62
|
2 |
|
RestrictionException::ERROR_CODE_VALUE, |
63
|
2 |
|
$value, |
64
|
2 |
|
'is_numeric'); |
65
|
|
|
} |
66
|
29 |
|
return $value + 0; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero. |
71
|
|
|
* |
72
|
|
|
* @param float $value |
73
|
|
|
* @param int $fractionDigits |
74
|
|
|
* @throws RestrictionException |
75
|
|
|
* @return float |
76
|
|
|
*/ |
77
|
3 |
|
public static function checkFractionDigits($value, $fractionDigits) |
78
|
|
|
{ |
79
|
3 |
|
$numeric = static::getNumeric($value); |
|
|
|
|
80
|
2 |
|
if ($numeric < 0) { |
81
|
|
|
throw new RestrictionException( |
82
|
|
|
"The '$numeric' must be equal to or greater than zero", |
83
|
|
|
RestrictionException::ERROR_CODE_GTE, |
84
|
|
|
$numeric); |
85
|
|
|
} |
86
|
2 |
|
$count = 0; |
87
|
2 |
|
if ((int)$numeric != $numeric){ |
88
|
2 |
|
$count = strlen($numeric) - strrpos($numeric, '.') - 1; |
89
|
2 |
|
} |
90
|
2 |
|
if ($count > $fractionDigits) { |
91
|
1 |
|
throw new RestrictionException( |
92
|
1 |
|
"The restriction fraction digits with '$fractionDigits' is not true", |
93
|
1 |
|
RestrictionException::ERROR_CODE_FRACTION_DIGITS, |
94
|
1 |
|
$value, |
95
|
1 |
|
$fractionDigits); |
96
|
|
|
} |
97
|
1 |
|
return $numeric; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Specifies the exact number of digits allowed. Must be greater than zero. |
102
|
|
|
* |
103
|
|
|
* @param float $value |
104
|
|
|
* @param int $totalDigits |
105
|
|
|
* @throws RestrictionException |
106
|
|
|
* @return float |
107
|
|
|
*/ |
108
|
4 |
|
public static function checkTotalDigits($value, $totalDigits) |
109
|
|
|
{ |
110
|
4 |
|
$numeric = static::getNumeric($value); |
|
|
|
|
111
|
3 |
|
if ($numeric < 0) { |
112
|
|
|
throw new RestrictionException( |
113
|
|
|
"The '$numeric' must be equal to or greater than zero", |
114
|
|
|
RestrictionException::ERROR_CODE_GTE, |
115
|
|
|
$numeric); |
116
|
|
|
} |
117
|
3 |
|
$count = 0; |
|
|
|
|
118
|
3 |
|
if ($numeric === 0) { |
119
|
|
|
$count = 1; |
120
|
|
|
} else { |
121
|
3 |
|
$count = floor(log10(floor($numeric))) + 1; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
if ($count > $totalDigits) { |
125
|
2 |
|
throw new RestrictionException( |
126
|
2 |
|
"The restriction total digits with '$totalDigits' is not true", |
127
|
2 |
|
RestrictionException::ERROR_CODE_TOTAL_DIGITS, |
128
|
2 |
|
$value, |
129
|
2 |
|
$totalDigits); |
130
|
|
|
} |
131
|
1 |
|
return $numeric; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Specifies the upper bounds for numeric values (the value must be less than this value) |
136
|
|
|
* |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @param int $maxExclusive |
139
|
|
|
* @throws RestrictionException |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
3 |
View Code Duplication |
public static function checkMaxExclusive($value, $maxExclusive) |
|
|
|
|
143
|
|
|
{ |
144
|
3 |
|
$numeric = static::getNumeric($value); |
|
|
|
|
145
|
3 |
|
if ($numeric >= $maxExclusive) { |
146
|
2 |
|
throw new RestrictionException( |
147
|
2 |
|
"The restriction max exclusive with '$maxExclusive' is not true", |
148
|
2 |
|
RestrictionException::ERROR_CODE_MAX_EXCLUSIVE, |
149
|
2 |
|
$value, |
150
|
2 |
|
$maxExclusive); |
151
|
|
|
} |
152
|
1 |
|
return $numeric; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
157
|
|
|
* |
158
|
|
|
* @param mixed $value |
159
|
|
|
* @param int $maxInclusive |
160
|
|
|
* @throws RestrictionException |
161
|
|
|
* @return mixed |
162
|
|
|
*/ |
163
|
3 |
View Code Duplication |
public static function checkMaxInclusive($value, $maxInclusive) |
|
|
|
|
164
|
|
|
{ |
165
|
3 |
|
$numeric = static::getNumeric($value); |
|
|
|
|
166
|
3 |
|
if ($numeric > $maxInclusive) { |
167
|
2 |
|
throw new RestrictionException( |
168
|
2 |
|
"The restriction max inclusive with '$maxInclusive' is not true", |
169
|
2 |
|
RestrictionException::ERROR_CODE_MAX_INCLUSIVE, |
170
|
2 |
|
$value, |
171
|
2 |
|
$maxInclusive); |
172
|
|
|
} |
173
|
1 |
|
return $numeric; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Retrive the number of characters or list items allowed. |
178
|
|
|
* |
179
|
|
|
* @param mixed $value |
180
|
|
|
* @param string $nativeType |
181
|
|
|
* @throws RestrictionException |
182
|
|
|
* @return int |
183
|
|
|
*/ |
184
|
21 |
|
private static function getLength($value, $nativeType=null) |
185
|
|
|
{ |
186
|
21 |
|
$valueLength = 0; |
|
|
|
|
187
|
21 |
|
if (in_array($nativeType, ['int','float','integer']) && is_numeric($value)) { |
188
|
12 |
|
$valueLength = static::getNumeric($value); |
|
|
|
|
189
|
12 |
|
} else |
190
|
12 |
|
if (is_array($value)) { |
191
|
6 |
|
$valueLength = count($value); |
192
|
6 |
|
} else { |
193
|
9 |
|
$valueLength = strlen($value); |
194
|
|
|
} |
195
|
21 |
|
if ($valueLength < 0) { |
196
|
|
|
throw new RestrictionException( |
197
|
|
|
"The '$value' must be equal to or greater than zero", |
198
|
|
|
RestrictionException::ERROR_CODE_GTE, |
199
|
|
|
$value); |
200
|
|
|
} |
201
|
21 |
|
return $valueLength; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero. |
206
|
|
|
* |
207
|
|
|
* @param mixed $value |
208
|
|
|
* @param int $length |
209
|
|
|
* @param string $nativeType |
210
|
|
|
* @throws RestrictionException |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
7 |
View Code Duplication |
public static function checkLength($value, $length, $nativeType=null) |
|
|
|
|
214
|
|
|
{ |
215
|
7 |
|
if (static::getLength($value, $nativeType) != $length) { |
|
|
|
|
216
|
6 |
|
throw new RestrictionException( |
217
|
6 |
|
"The restriction length with '$length' is not true", |
218
|
6 |
|
RestrictionException::ERROR_CODE_LENGTH, |
219
|
6 |
|
$value, |
220
|
6 |
|
$length); |
221
|
|
|
} |
222
|
1 |
|
return $value; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
227
|
|
|
* |
228
|
|
|
* @param mixed $value |
229
|
|
|
* @param int $maxLength |
230
|
|
|
* @param string $nativeType |
231
|
|
|
* @throws RestrictionException |
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
7 |
View Code Duplication |
public static function checkMaxLength($value, $maxLength, $nativeType=null) |
|
|
|
|
235
|
|
|
{ |
236
|
7 |
|
if (static::getLength($value, $nativeType) > $maxLength) { |
|
|
|
|
237
|
6 |
|
throw new RestrictionException( |
238
|
6 |
|
"The restriction max length with '$maxLength' is not true", |
239
|
6 |
|
RestrictionException::ERROR_CODE_MAX_LENGTH, |
240
|
6 |
|
$value, |
241
|
6 |
|
$maxLength); |
242
|
|
|
} |
243
|
1 |
|
return $value; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
248
|
|
|
* |
249
|
|
|
* @param mixed $value |
250
|
|
|
* @param int $minLength |
251
|
|
|
* @param string $nativeType |
252
|
|
|
* @throws RestrictionException |
253
|
|
|
* @return mixed |
254
|
|
|
*/ |
255
|
7 |
View Code Duplication |
public static function checkMinLength($value, $minLength, $nativeType=null) |
|
|
|
|
256
|
|
|
{ |
257
|
7 |
|
if (static::getLength($value, $nativeType) < $minLength) { |
|
|
|
|
258
|
6 |
|
throw new RestrictionException( |
259
|
6 |
|
"The restriction min length with '$minLength' is not true", |
260
|
6 |
|
RestrictionException::ERROR_CODE_MIN_LENGTH, |
261
|
6 |
|
$value, |
262
|
6 |
|
$minLength); |
263
|
|
|
} |
264
|
1 |
|
return $value; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Specifies the lower bounds for numeric values (the value must be greater than this value) |
269
|
|
|
* |
270
|
|
|
* @param mixed $value |
271
|
|
|
* @param int $minExclusive |
272
|
|
|
* @throws RestrictionException |
273
|
|
|
* @return mixed |
274
|
|
|
*/ |
275
|
3 |
View Code Duplication |
public static function checkMinExclusive($value, $minExclusive) |
|
|
|
|
276
|
|
|
{ |
277
|
3 |
|
$numeric = static::getNumeric($value); |
|
|
|
|
278
|
3 |
|
if ($numeric <= $minExclusive) { |
279
|
2 |
|
throw new RestrictionException( |
280
|
2 |
|
"The restriction min exclusive with '$minExclusive' is not true", |
281
|
2 |
|
RestrictionException::ERROR_CODE_MIN_EXCLUSIVE, |
282
|
2 |
|
$value, |
283
|
2 |
|
$minExclusive); |
284
|
|
|
} |
285
|
1 |
|
return $numeric; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
290
|
|
|
* |
291
|
|
|
* @param mixed $value |
292
|
|
|
* @param int $minInclusive |
293
|
|
|
* @throws RestrictionException |
294
|
|
|
* @return mixed |
295
|
|
|
*/ |
296
|
3 |
View Code Duplication |
public static function checkMinInclusive($value, $minInclusive) |
|
|
|
|
297
|
|
|
{ |
298
|
3 |
|
$numeric = static::getNumeric($value); |
|
|
|
|
299
|
3 |
|
if ($numeric < $minInclusive) { |
300
|
2 |
|
throw new RestrictionException( |
301
|
2 |
|
"The restriction min inclusive with '$minInclusive' is not true", |
302
|
2 |
|
RestrictionException::ERROR_CODE_MIN_INCLUSIVE, |
303
|
2 |
|
$value, |
304
|
2 |
|
$minInclusive); |
305
|
|
|
} |
306
|
1 |
|
return $numeric; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |
311
|
|
|
* |
312
|
|
|
* @param mixed $value |
313
|
|
|
* @param string $whiteSpace |
314
|
|
|
* @return mixed |
315
|
|
|
*/ |
316
|
1 |
|
public static function checkWhiteSpace($value, $whiteSpace) |
317
|
|
|
{ |
318
|
1 |
|
$output = $value; |
319
|
1 |
|
if ($whiteSpace == 'replace' || $whiteSpace == 'collapse') { |
320
|
1 |
|
$output = preg_replace('/\s/', ' ', $output); |
321
|
1 |
|
} |
322
|
1 |
|
if ($whiteSpace == 'collapse') { |
323
|
1 |
|
$output = preg_replace('/\s+/', ' ', $output); |
324
|
1 |
|
} |
325
|
1 |
|
return $output; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
} |
329
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: