1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/assert package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\Assert; |
13
|
|
|
|
14
|
|
|
use BadMethodCallException; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Traversable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Efficient assertions to validate the input/output of your methods. |
20
|
|
|
* |
21
|
|
|
* @method static void nullOrString($value, $message = '') |
22
|
|
|
* @method static void nullOrStringNotEmpty($value, $message = '') |
23
|
|
|
* @method static void nullOrInteger($value, $message = '') |
24
|
|
|
* @method static void nullOrIntegerish($value, $message = '') |
25
|
|
|
* @method static void nullOrFloat($value, $message = '') |
26
|
|
|
* @method static void nullOrNumeric($value, $message = '') |
27
|
|
|
* @method static void nullOrBoolean($value, $message = '') |
28
|
|
|
* @method static void nullOrScalar($value, $message = '') |
29
|
|
|
* @method static void nullOrObject($value, $message = '') |
30
|
|
|
* @method static void nullOrResource($value, $type = null, $message = '') |
31
|
|
|
* @method static void nullOrIsCallable($value, $message = '') |
32
|
|
|
* @method static void nullOrIsArray($value, $message = '') |
33
|
|
|
* @method static void nullOrIsTraversable($value, $message = '') |
34
|
|
|
* @method static void nullOrIsInstanceOf($value, $class, $message = '') |
35
|
|
|
* @method static void nullOrNotInstanceOf($value, $class, $message = '') |
36
|
|
|
* @method static void nullOrIsEmpty($value, $message = '') |
37
|
|
|
* @method static void nullOrNotEmpty($value, $message = '') |
38
|
|
|
* @method static void nullOrTrue($value, $message = '') |
39
|
|
|
* @method static void nullOrFalse($value, $message = '') |
40
|
|
|
* @method static void nullOrEq($value, $value2, $message = '') |
41
|
|
|
* @method static void nullOrNotEq($value,$value2, $message = '') |
42
|
|
|
* @method static void nullOrSame($value, $value2, $message = '') |
43
|
|
|
* @method static void nullOrNotSame($value, $value2, $message = '') |
44
|
|
|
* @method static void nullOrGreaterThan($value, $value2, $message = '') |
45
|
|
|
* @method static void nullOrGreaterThanEq($value, $value2, $message = '') |
46
|
|
|
* @method static void nullOrLessThan($value, $value2, $message = '') |
47
|
|
|
* @method static void nullOrLessThanEq($value, $value2, $message = '') |
48
|
|
|
* @method static void nullOrRange($value, $min, $max, $message = '') |
49
|
|
|
* @method static void nullOrOneOf($value, $values, $message = '') |
50
|
|
|
* @method static void nullOrContains($value, $subString, $message = '') |
51
|
|
|
* @method static void nullOrStartsWith($value, $prefix, $message = '') |
52
|
|
|
* @method static void nullOrStartsWithLetter($value, $message = '') |
53
|
|
|
* @method static void nullOrEndsWith($value, $suffix, $message = '') |
54
|
|
|
* @method static void nullOrRegex($value, $pattern, $message = '') |
55
|
|
|
* @method static void nullOrAlpha($value, $message = '') |
56
|
|
|
* @method static void nullOrDigits($value, $message = '') |
57
|
|
|
* @method static void nullOrAlnum($value, $message = '') |
58
|
|
|
* @method static void nullOrLower($value, $message = '') |
59
|
|
|
* @method static void nullOrUpper($value, $message = '') |
60
|
|
|
* @method static void nullOrLength($value, $length, $message = '') |
61
|
|
|
* @method static void nullOrMinLength($value, $min, $message = '') |
62
|
|
|
* @method static void nullOrMaxLength($value, $max, $message = '') |
63
|
|
|
* @method static void nullOrLengthBetween($value, $min, $max, $message = '') |
64
|
|
|
* @method static void nullOrFileExists($value, $message = '') |
65
|
|
|
* @method static void nullOrFile($value, $message = '') |
66
|
|
|
* @method static void nullOrDirectory($value, $message = '') |
67
|
|
|
* @method static void nullOrReadable($value, $message = '') |
68
|
|
|
* @method static void nullOrWritable($value, $message = '') |
69
|
|
|
* @method static void nullOrClassExists($value, $message = '') |
70
|
|
|
* @method static void nullOrSubclassOf($value, $class, $message = '') |
71
|
|
|
* @method static void nullOrImplementsInterface($value, $interface, $message = '') |
72
|
|
|
* @method static void nullOrPropertyExists($value, $property, $message = '') |
73
|
|
|
* @method static void nullOrPropertyNotExists($value, $property, $message = '') |
74
|
|
|
* @method static void nullOrMethodExists($value, $method, $message = '') |
75
|
|
|
* @method static void nullOrMethodNotExists($value, $method, $message = '') |
76
|
|
|
* @method static void nullOrKeyExists($value, $key, $message = '') |
77
|
|
|
* @method static void nullOrKeyNotExists($value, $key, $message = '') |
78
|
|
|
* @method static void nullOrUuid($values, $message = '') |
79
|
|
|
* @method static void allString($values, $message = '') |
80
|
|
|
* @method static void allStringNotEmpty($values, $message = '') |
81
|
|
|
* @method static void allInteger($values, $message = '') |
82
|
|
|
* @method static void allIntegerish($values, $message = '') |
83
|
|
|
* @method static void allFloat($values, $message = '') |
84
|
|
|
* @method static void allNumeric($values, $message = '') |
85
|
|
|
* @method static void allBoolean($values, $message = '') |
86
|
|
|
* @method static void allScalar($values, $message = '') |
87
|
|
|
* @method static void allObject($values, $message = '') |
88
|
|
|
* @method static void allResource($values, $type = null, $message = '') |
89
|
|
|
* @method static void allIsCallable($values, $message = '') |
90
|
|
|
* @method static void allIsArray($values, $message = '') |
91
|
|
|
* @method static void allIsTraversable($values, $message = '') |
92
|
|
|
* @method static void allIsInstanceOf($values, $class, $message = '') |
93
|
|
|
* @method static void allNotInstanceOf($values, $class, $message = '') |
94
|
|
|
* @method static void allNull($values, $message = '') |
95
|
|
|
* @method static void allNotNull($values, $message = '') |
96
|
|
|
* @method static void allIsEmpty($values, $message = '') |
97
|
|
|
* @method static void allNotEmpty($values, $message = '') |
98
|
|
|
* @method static void allTrue($values, $message = '') |
99
|
|
|
* @method static void allFalse($values, $message = '') |
100
|
|
|
* @method static void allEq($values, $value2, $message = '') |
101
|
|
|
* @method static void allNotEq($values,$value2, $message = '') |
102
|
|
|
* @method static void allSame($values, $value2, $message = '') |
103
|
|
|
* @method static void allNotSame($values, $value2, $message = '') |
104
|
|
|
* @method static void allGreaterThan($values, $value2, $message = '') |
105
|
|
|
* @method static void allGreaterThanEq($values, $value2, $message = '') |
106
|
|
|
* @method static void allLessThan($values, $value2, $message = '') |
107
|
|
|
* @method static void allLessThanEq($values, $value2, $message = '') |
108
|
|
|
* @method static void allRange($values, $min, $max, $message = '') |
109
|
|
|
* @method static void allOneOf($values, $values, $message = '') |
110
|
|
|
* @method static void allContains($values, $subString, $message = '') |
111
|
|
|
* @method static void allStartsWith($values, $prefix, $message = '') |
112
|
|
|
* @method static void allStartsWithLetter($values, $message = '') |
113
|
|
|
* @method static void allEndsWith($values, $suffix, $message = '') |
114
|
|
|
* @method static void allRegex($values, $pattern, $message = '') |
115
|
|
|
* @method static void allAlpha($values, $message = '') |
116
|
|
|
* @method static void allDigits($values, $message = '') |
117
|
|
|
* @method static void allAlnum($values, $message = '') |
118
|
|
|
* @method static void allLower($values, $message = '') |
119
|
|
|
* @method static void allUpper($values, $message = '') |
120
|
|
|
* @method static void allLength($values, $length, $message = '') |
121
|
|
|
* @method static void allMinLength($values, $min, $message = '') |
122
|
|
|
* @method static void allMaxLength($values, $max, $message = '') |
123
|
|
|
* @method static void allLengthBetween($values, $min, $max, $message = '') |
124
|
|
|
* @method static void allFileExists($values, $message = '') |
125
|
|
|
* @method static void allFile($values, $message = '') |
126
|
|
|
* @method static void allDirectory($values, $message = '') |
127
|
|
|
* @method static void allReadable($values, $message = '') |
128
|
|
|
* @method static void allWritable($values, $message = '') |
129
|
|
|
* @method static void allClassExists($values, $message = '') |
130
|
|
|
* @method static void allSubclassOf($values, $class, $message = '') |
131
|
|
|
* @method static void allImplementsInterface($values, $interface, $message = '') |
132
|
|
|
* @method static void allPropertyExists($values, $property, $message = '') |
133
|
|
|
* @method static void allPropertyNotExists($values, $property, $message = '') |
134
|
|
|
* @method static void allMethodExists($values, $method, $message = '') |
135
|
|
|
* @method static void allMethodNotExists($values, $method, $message = '') |
136
|
|
|
* @method static void allKeyExists($values, $key, $message = '') |
137
|
|
|
* @method static void allKeyNotExists($values, $key, $message = '') |
138
|
|
|
* @method static void allUuid($values, $message = '') |
139
|
|
|
* |
140
|
|
|
* @since 1.0 |
141
|
|
|
* |
142
|
|
|
* @author Bernhard Schussek <[email protected]> |
143
|
|
|
*/ |
144
|
|
|
class Assert |
|
|
|
|
145
|
|
|
{ |
146
|
66 |
|
public static function string($value, $message = '') |
147
|
|
|
{ |
148
|
66 |
|
if (!is_string($value)) { |
149
|
14 |
|
throw new InvalidArgumentException(sprintf( |
150
|
14 |
|
$message ?: 'Expected a string. Got: %s', |
151
|
14 |
|
self::typeToString($value) |
152
|
|
|
)); |
153
|
|
|
} |
154
|
52 |
|
} |
155
|
|
|
|
156
|
12 |
|
public static function stringNotEmpty($value, $message = '') |
157
|
|
|
{ |
158
|
12 |
|
self::string($value, $message); |
159
|
8 |
|
self::notEmpty($value, $message); |
160
|
4 |
|
} |
161
|
|
|
|
162
|
17 |
|
public static function integer($value, $message = '') |
163
|
|
|
{ |
164
|
17 |
|
if (!is_int($value)) { |
165
|
13 |
|
throw new InvalidArgumentException(sprintf( |
166
|
13 |
|
$message ?: 'Expected an integer. Got: %s', |
167
|
13 |
|
self::typeToString($value) |
168
|
|
|
)); |
169
|
|
|
} |
170
|
4 |
|
} |
171
|
|
|
|
172
|
16 |
View Code Duplication |
public static function integerish($value, $message = '') |
173
|
|
|
{ |
174
|
16 |
|
if (!is_numeric($value) || $value != (int) $value) { |
175
|
4 |
|
throw new InvalidArgumentException(sprintf( |
176
|
4 |
|
$message ?: 'Expected an integerish value. Got: %s', |
177
|
4 |
|
self::typeToString($value) |
178
|
|
|
)); |
179
|
|
|
} |
180
|
12 |
|
} |
181
|
|
|
|
182
|
16 |
|
public static function float($value, $message = '') |
183
|
|
|
{ |
184
|
16 |
|
if (!is_float($value)) { |
185
|
8 |
|
throw new InvalidArgumentException(sprintf( |
186
|
8 |
|
$message ?: 'Expected a float. Got: %s', |
187
|
8 |
|
self::typeToString($value) |
188
|
|
|
)); |
189
|
|
|
} |
190
|
8 |
|
} |
191
|
|
|
|
192
|
20 |
|
public static function numeric($value, $message = '') |
193
|
|
|
{ |
194
|
20 |
|
if (!is_numeric($value)) { |
195
|
4 |
|
throw new InvalidArgumentException(sprintf( |
196
|
4 |
|
$message ?: 'Expected a numeric. Got: %s', |
197
|
4 |
|
self::typeToString($value) |
198
|
|
|
)); |
199
|
|
|
} |
200
|
16 |
|
} |
201
|
|
|
|
202
|
16 |
|
public static function boolean($value, $message = '') |
203
|
|
|
{ |
204
|
16 |
|
if (!is_bool($value)) { |
205
|
8 |
|
throw new InvalidArgumentException(sprintf( |
206
|
8 |
|
$message ?: 'Expected a boolean. Got: %s', |
207
|
8 |
|
self::typeToString($value) |
208
|
|
|
)); |
209
|
|
|
} |
210
|
8 |
|
} |
211
|
|
|
|
212
|
23 |
|
public static function scalar($value, $message = '') |
213
|
|
|
{ |
214
|
23 |
|
if (!is_scalar($value)) { |
215
|
11 |
|
throw new InvalidArgumentException(sprintf( |
216
|
11 |
|
$message ?: 'Expected a scalar. Got: %s', |
217
|
11 |
|
self::typeToString($value) |
218
|
|
|
)); |
219
|
|
|
} |
220
|
12 |
|
} |
221
|
|
|
|
222
|
23 |
|
public static function object($value, $message = '') |
223
|
|
|
{ |
224
|
23 |
|
if (!is_object($value)) { |
225
|
15 |
|
throw new InvalidArgumentException(sprintf( |
226
|
15 |
|
$message ?: 'Expected an object. Got: %s', |
227
|
15 |
|
self::typeToString($value) |
228
|
|
|
)); |
229
|
|
|
} |
230
|
8 |
|
} |
231
|
|
|
|
232
|
16 |
|
public static function resource($value, $type = null, $message = '') |
233
|
|
|
{ |
234
|
16 |
|
if (!is_resource($value)) { |
235
|
4 |
|
throw new InvalidArgumentException(sprintf( |
236
|
4 |
|
$message ?: 'Expected a resource. Got: %s', |
237
|
4 |
|
self::typeToString($value) |
238
|
|
|
)); |
239
|
|
|
} |
240
|
|
|
|
241
|
12 |
|
if ($type && $type !== get_resource_type($value)) { |
242
|
4 |
|
throw new InvalidArgumentException(sprintf( |
243
|
4 |
|
$message ?: 'Expected a resource of type %2$s. Got: %s', |
244
|
4 |
|
self::typeToString($value), |
245
|
|
|
$type |
246
|
|
|
)); |
247
|
|
|
} |
248
|
8 |
|
} |
249
|
|
|
|
250
|
20 |
|
public static function isCallable($value, $message = '') |
251
|
|
|
{ |
252
|
20 |
|
if (!is_callable($value)) { |
253
|
8 |
|
throw new InvalidArgumentException(sprintf( |
254
|
8 |
|
$message ?: 'Expected a callable. Got: %s', |
255
|
8 |
|
self::typeToString($value) |
256
|
|
|
)); |
257
|
|
|
} |
258
|
12 |
|
} |
259
|
|
|
|
260
|
20 |
View Code Duplication |
public static function isArray($value, $message = '') |
261
|
|
|
{ |
262
|
20 |
|
if (!is_array($value)) { |
263
|
12 |
|
throw new InvalidArgumentException(sprintf( |
264
|
12 |
|
$message ?: 'Expected an array. Got: %s', |
265
|
12 |
|
self::typeToString($value) |
266
|
|
|
)); |
267
|
|
|
} |
268
|
8 |
|
} |
269
|
|
|
|
270
|
490 |
View Code Duplication |
public static function isTraversable($value, $message = '') |
271
|
|
|
{ |
272
|
490 |
|
if (!is_array($value) && !($value instanceof Traversable)) { |
273
|
8 |
|
throw new InvalidArgumentException(sprintf( |
274
|
8 |
|
$message ?: 'Expected a traversable. Got: %s', |
275
|
8 |
|
self::typeToString($value) |
276
|
|
|
)); |
277
|
|
|
} |
278
|
486 |
|
} |
279
|
|
|
|
280
|
16 |
|
public static function isInstanceOf($value, $class, $message = '') |
281
|
|
|
{ |
282
|
16 |
|
if (!($value instanceof $class)) { |
283
|
12 |
|
throw new InvalidArgumentException(sprintf( |
284
|
12 |
|
$message ?: 'Expected an instance of %2$s. Got: %s', |
285
|
12 |
|
self::typeToString($value), |
286
|
|
|
$class |
287
|
|
|
)); |
288
|
|
|
} |
289
|
4 |
|
} |
290
|
|
|
|
291
|
16 |
|
public static function notInstanceOf($value, $class, $message = '') |
292
|
|
|
{ |
293
|
16 |
|
if ($value instanceof $class) { |
294
|
4 |
|
throw new InvalidArgumentException(sprintf( |
295
|
4 |
|
$message ?: 'Expected an instance other than %2$s. Got: %s', |
296
|
4 |
|
self::typeToString($value), |
297
|
|
|
$class |
298
|
|
|
)); |
299
|
|
|
} |
300
|
12 |
|
} |
301
|
|
|
|
302
|
23 |
|
public static function isEmpty($value, $message = '') |
303
|
|
|
{ |
304
|
23 |
|
if (!empty($value)) { |
305
|
8 |
|
throw new InvalidArgumentException(sprintf( |
306
|
8 |
|
$message ?: 'Expected an empty value. Got: %s', |
307
|
8 |
|
self::valueToString($value) |
308
|
|
|
)); |
309
|
|
|
} |
310
|
15 |
|
} |
311
|
|
|
|
312
|
31 |
|
public static function notEmpty($value, $message = '') |
313
|
|
|
{ |
314
|
31 |
|
if (empty($value)) { |
315
|
19 |
|
throw new InvalidArgumentException(sprintf( |
316
|
19 |
|
$message ?: 'Expected a non-empty value. Got: %s', |
317
|
19 |
|
self::valueToString($value) |
318
|
|
|
)); |
319
|
|
|
} |
320
|
12 |
|
} |
321
|
|
|
|
322
|
11 |
View Code Duplication |
public static function null($value, $message = '') |
323
|
|
|
{ |
324
|
11 |
|
if (null !== $value) { |
325
|
8 |
|
throw new InvalidArgumentException(sprintf( |
326
|
8 |
|
$message ?: 'Expected null. Got: %s', |
327
|
8 |
|
self::valueToString($value) |
328
|
|
|
)); |
329
|
|
|
} |
330
|
3 |
|
} |
331
|
|
|
|
332
|
11 |
|
public static function notNull($value, $message = '') |
333
|
|
|
{ |
334
|
11 |
|
if (null === $value) { |
335
|
3 |
|
throw new InvalidArgumentException( |
336
|
3 |
|
$message ?: 'Expected a value other than null.' |
337
|
|
|
); |
338
|
|
|
} |
339
|
8 |
|
} |
340
|
|
|
|
341
|
15 |
View Code Duplication |
public static function true($value, $message = '') |
342
|
|
|
{ |
343
|
15 |
|
if (true !== $value) { |
344
|
11 |
|
throw new InvalidArgumentException(sprintf( |
345
|
11 |
|
$message ?: 'Expected a value to be true. Got: %s', |
346
|
11 |
|
self::valueToString($value) |
347
|
|
|
)); |
348
|
|
|
} |
349
|
4 |
|
} |
350
|
|
|
|
351
|
19 |
View Code Duplication |
public static function false($value, $message = '') |
352
|
|
|
{ |
353
|
19 |
|
if (false !== $value) { |
354
|
15 |
|
throw new InvalidArgumentException(sprintf( |
355
|
15 |
|
$message ?: 'Expected a value to be false. Got: %s', |
356
|
15 |
|
self::valueToString($value) |
357
|
|
|
)); |
358
|
|
|
} |
359
|
4 |
|
} |
360
|
|
|
|
361
|
24 |
|
public static function eq($value, $value2, $message = '') |
362
|
|
|
{ |
363
|
24 |
|
if ($value2 != $value) { |
364
|
12 |
|
throw new InvalidArgumentException(sprintf( |
365
|
12 |
|
$message ?: 'Expected a value equal to %2$s. Got: %s', |
366
|
12 |
|
self::valueToString($value), |
367
|
12 |
|
self::valueToString($value2) |
368
|
|
|
)); |
369
|
|
|
} |
370
|
12 |
|
} |
371
|
|
|
|
372
|
16 |
|
public static function notEq($value, $value2, $message = '') |
373
|
|
|
{ |
374
|
16 |
|
if ($value2 == $value) { |
375
|
12 |
|
throw new InvalidArgumentException(sprintf( |
376
|
12 |
|
$message ?: 'Expected a different value than %s.', |
377
|
12 |
|
self::valueToString($value2) |
378
|
|
|
)); |
379
|
|
|
} |
380
|
4 |
|
} |
381
|
|
|
|
382
|
16 |
|
public static function same($value, $value2, $message = '') |
383
|
|
|
{ |
384
|
16 |
|
if ($value2 !== $value) { |
385
|
12 |
|
throw new InvalidArgumentException(sprintf( |
386
|
12 |
|
$message ?: 'Expected a value identical to %2$s. Got: %s', |
387
|
12 |
|
self::valueToString($value), |
388
|
12 |
|
self::valueToString($value2) |
389
|
|
|
)); |
390
|
|
|
} |
391
|
4 |
|
} |
392
|
|
|
|
393
|
16 |
|
public static function notSame($value, $value2, $message = '') |
394
|
|
|
{ |
395
|
16 |
|
if ($value2 === $value) { |
396
|
4 |
|
throw new InvalidArgumentException(sprintf( |
397
|
4 |
|
$message ?: 'Expected a value not identical to %s.', |
398
|
4 |
|
self::valueToString($value2) |
399
|
|
|
)); |
400
|
|
|
} |
401
|
12 |
|
} |
402
|
|
|
|
403
|
8 |
|
public static function greaterThan($value, $limit, $message = '') |
404
|
|
|
{ |
405
|
8 |
|
if ($value <= $limit) { |
406
|
4 |
|
throw new InvalidArgumentException(sprintf( |
407
|
4 |
|
$message ?: 'Expected a value greater than %2$s. Got: %s', |
408
|
4 |
|
self::valueToString($value), |
409
|
4 |
|
self::valueToString($limit) |
410
|
|
|
)); |
411
|
|
|
} |
412
|
4 |
|
} |
413
|
|
|
|
414
|
12 |
|
public static function greaterThanEq($value, $limit, $message = '') |
415
|
|
|
{ |
416
|
12 |
|
if ($value < $limit) { |
417
|
4 |
|
throw new InvalidArgumentException(sprintf( |
418
|
4 |
|
$message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
419
|
4 |
|
self::valueToString($value), |
420
|
4 |
|
self::valueToString($limit) |
421
|
|
|
)); |
422
|
|
|
} |
423
|
8 |
|
} |
424
|
|
|
|
425
|
8 |
|
public static function lessThan($value, $limit, $message = '') |
426
|
|
|
{ |
427
|
8 |
|
if ($value >= $limit) { |
428
|
4 |
|
throw new InvalidArgumentException(sprintf( |
429
|
4 |
|
$message ?: 'Expected a value less than %2$s. Got: %s', |
430
|
4 |
|
self::valueToString($value), |
431
|
4 |
|
self::valueToString($limit) |
432
|
|
|
)); |
433
|
|
|
} |
434
|
4 |
|
} |
435
|
|
|
|
436
|
12 |
|
public static function lessThanEq($value, $limit, $message = '') |
437
|
|
|
{ |
438
|
12 |
|
if ($value > $limit) { |
439
|
4 |
|
throw new InvalidArgumentException(sprintf( |
440
|
4 |
|
$message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
441
|
4 |
|
self::valueToString($value), |
442
|
4 |
|
self::valueToString($limit) |
443
|
|
|
)); |
444
|
|
|
} |
445
|
8 |
|
} |
446
|
|
|
|
447
|
16 |
View Code Duplication |
public static function range($value, $min, $max, $message = '') |
448
|
|
|
{ |
449
|
16 |
|
if ($value < $min || $value > $max) { |
450
|
8 |
|
throw new InvalidArgumentException(sprintf( |
451
|
8 |
|
$message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
452
|
8 |
|
self::valueToString($value), |
453
|
8 |
|
self::valueToString($min), |
454
|
8 |
|
self::valueToString($max) |
455
|
|
|
)); |
456
|
|
|
} |
457
|
8 |
|
} |
458
|
|
|
|
459
|
8 |
|
public static function oneOf($value, array $values, $message = '') |
460
|
|
|
{ |
461
|
8 |
|
if (!in_array($value, $values, true)) { |
462
|
4 |
|
throw new InvalidArgumentException(sprintf( |
463
|
4 |
|
$message ?: 'Expected one of: %2$s. Got: %s', |
464
|
4 |
|
self::valueToString($value), |
465
|
4 |
|
implode(', ', array_map(array(__CLASS__, 'valueToString'), $values)) |
466
|
|
|
)); |
467
|
|
|
} |
468
|
4 |
|
} |
469
|
|
|
|
470
|
20 |
View Code Duplication |
public static function contains($value, $subString, $message = '') |
471
|
|
|
{ |
472
|
20 |
|
if (false === strpos($value, $subString)) { |
473
|
8 |
|
throw new InvalidArgumentException(sprintf( |
474
|
8 |
|
$message ?: 'Expected a value to contain %2$s. Got: %s', |
475
|
8 |
|
self::valueToString($value), |
476
|
8 |
|
self::valueToString($subString) |
477
|
|
|
)); |
478
|
|
|
} |
479
|
12 |
|
} |
480
|
|
|
|
481
|
12 |
View Code Duplication |
public static function startsWith($value, $prefix, $message = '') |
482
|
|
|
{ |
483
|
12 |
|
if (0 !== strpos($value, $prefix)) { |
484
|
8 |
|
throw new InvalidArgumentException(sprintf( |
485
|
8 |
|
$message ?: 'Expected a value to start with %2$s. Got: %s', |
486
|
8 |
|
self::valueToString($value), |
487
|
8 |
|
self::valueToString($prefix) |
488
|
|
|
)); |
489
|
|
|
} |
490
|
4 |
|
} |
491
|
|
|
|
492
|
12 |
|
public static function startsWithLetter($value, $message = '') |
493
|
|
|
{ |
494
|
12 |
|
$valid = isset($value[0]); |
495
|
|
|
|
496
|
12 |
|
if ($valid) { |
497
|
8 |
|
$locale = setlocale(LC_CTYPE, 0); |
498
|
8 |
|
setlocale(LC_CTYPE, 'C'); |
499
|
8 |
|
$valid = ctype_alpha($value[0]); |
500
|
8 |
|
setlocale(LC_CTYPE, $locale); |
501
|
|
|
} |
502
|
|
|
|
503
|
12 |
|
if (!$valid) { |
504
|
8 |
|
throw new InvalidArgumentException(sprintf( |
505
|
8 |
|
$message ?: 'Expected a value to start with a letter. Got: %s', |
506
|
8 |
|
self::valueToString($value) |
507
|
|
|
)); |
508
|
|
|
} |
509
|
4 |
|
} |
510
|
|
|
|
511
|
12 |
View Code Duplication |
public static function endsWith($value, $suffix, $message = '') |
512
|
|
|
{ |
513
|
12 |
|
if ($suffix !== substr($value, -self::strlen($suffix))) { |
514
|
8 |
|
throw new InvalidArgumentException(sprintf( |
515
|
8 |
|
$message ?: 'Expected a value to end with %2$s. Got: %s', |
516
|
8 |
|
self::valueToString($value), |
517
|
8 |
|
self::valueToString($suffix) |
518
|
|
|
)); |
519
|
|
|
} |
520
|
4 |
|
} |
521
|
|
|
|
522
|
12 |
|
public static function regex($value, $pattern, $message = '') |
523
|
|
|
{ |
524
|
12 |
|
if (!preg_match($pattern, $value)) { |
525
|
8 |
|
throw new InvalidArgumentException(sprintf( |
526
|
8 |
|
$message ?: 'The value %s does not match the expected pattern.', |
527
|
8 |
|
self::valueToString($value) |
528
|
|
|
)); |
529
|
|
|
} |
530
|
4 |
|
} |
531
|
|
|
|
532
|
12 |
View Code Duplication |
public static function alpha($value, $message = '') |
533
|
|
|
{ |
534
|
12 |
|
$locale = setlocale(LC_CTYPE, 0); |
535
|
12 |
|
setlocale(LC_CTYPE, 'C'); |
536
|
12 |
|
$valid = !ctype_alpha($value); |
537
|
12 |
|
setlocale(LC_CTYPE, $locale); |
538
|
|
|
|
539
|
12 |
|
if ($valid) { |
540
|
8 |
|
throw new InvalidArgumentException(sprintf( |
541
|
8 |
|
$message ?: 'Expected a value to contain only letters. Got: %s', |
542
|
8 |
|
self::valueToString($value) |
543
|
|
|
)); |
544
|
|
|
} |
545
|
4 |
|
} |
546
|
|
|
|
547
|
12 |
View Code Duplication |
public static function digits($value, $message = '') |
548
|
|
|
{ |
549
|
12 |
|
$locale = setlocale(LC_CTYPE, 0); |
550
|
12 |
|
setlocale(LC_CTYPE, 'C'); |
551
|
12 |
|
$valid = !ctype_digit($value); |
552
|
12 |
|
setlocale(LC_CTYPE, $locale); |
553
|
|
|
|
554
|
12 |
|
if ($valid) { |
555
|
8 |
|
throw new InvalidArgumentException(sprintf( |
556
|
8 |
|
$message ?: 'Expected a value to contain digits only. Got: %s', |
557
|
8 |
|
self::valueToString($value) |
558
|
|
|
)); |
559
|
|
|
} |
560
|
4 |
|
} |
561
|
|
|
|
562
|
12 |
View Code Duplication |
public static function alnum($value, $message = '') |
563
|
|
|
{ |
564
|
12 |
|
$locale = setlocale(LC_CTYPE, 0); |
565
|
12 |
|
setlocale(LC_CTYPE, 'C'); |
566
|
12 |
|
$valid = !ctype_alnum($value); |
567
|
12 |
|
setlocale(LC_CTYPE, $locale); |
568
|
|
|
|
569
|
12 |
|
if ($valid) { |
570
|
8 |
|
throw new InvalidArgumentException(sprintf( |
571
|
8 |
|
$message ?: 'Expected a value to contain letters and digits only. Got: %s', |
572
|
8 |
|
self::valueToString($value) |
573
|
|
|
)); |
574
|
|
|
} |
575
|
4 |
|
} |
576
|
|
|
|
577
|
16 |
View Code Duplication |
public static function lower($value, $message = '') |
578
|
|
|
{ |
579
|
16 |
|
$locale = setlocale(LC_CTYPE, 0); |
580
|
16 |
|
setlocale(LC_CTYPE, 'C'); |
581
|
16 |
|
$valid = !ctype_lower($value); |
582
|
16 |
|
setlocale(LC_CTYPE, $locale); |
583
|
|
|
|
584
|
16 |
|
if ($valid) { |
585
|
12 |
|
throw new InvalidArgumentException(sprintf( |
586
|
12 |
|
$message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
587
|
12 |
|
self::valueToString($value) |
588
|
|
|
)); |
589
|
|
|
} |
590
|
4 |
|
} |
591
|
|
|
|
592
|
16 |
View Code Duplication |
public static function upper($value, $message = '') |
593
|
|
|
{ |
594
|
16 |
|
$locale = setlocale(LC_CTYPE, 0); |
595
|
16 |
|
setlocale(LC_CTYPE, 'C'); |
596
|
16 |
|
$valid = !ctype_upper($value); |
597
|
16 |
|
setlocale(LC_CTYPE, $locale); |
598
|
|
|
|
599
|
16 |
|
if ($valid) { |
600
|
12 |
|
throw new InvalidArgumentException(sprintf( |
601
|
12 |
|
$message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
602
|
12 |
|
self::valueToString($value) |
603
|
|
|
)); |
604
|
|
|
} |
605
|
4 |
|
} |
606
|
|
|
|
607
|
24 |
|
public static function length($value, $length, $message = '') |
608
|
|
|
{ |
609
|
24 |
|
if ($length !== self::strlen($value)) { |
610
|
16 |
|
throw new InvalidArgumentException(sprintf( |
611
|
16 |
|
$message ?: 'Expected a value to contain %2$s characters. Got: %s', |
612
|
16 |
|
self::valueToString($value), |
613
|
|
|
$length |
614
|
|
|
)); |
615
|
|
|
} |
616
|
8 |
|
} |
617
|
|
|
|
618
|
24 |
|
public static function minLength($value, $min, $message = '') |
619
|
|
|
{ |
620
|
24 |
|
if (self::strlen($value) < $min) { |
621
|
8 |
|
throw new InvalidArgumentException(sprintf( |
622
|
8 |
|
$message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
623
|
8 |
|
self::valueToString($value), |
624
|
|
|
$min |
625
|
|
|
)); |
626
|
|
|
} |
627
|
16 |
|
} |
628
|
|
|
|
629
|
24 |
|
public static function maxLength($value, $max, $message = '') |
630
|
|
|
{ |
631
|
24 |
|
if (self::strlen($value) > $max) { |
632
|
8 |
|
throw new InvalidArgumentException(sprintf( |
633
|
8 |
|
$message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
634
|
8 |
|
self::valueToString($value), |
635
|
|
|
$max |
636
|
|
|
)); |
637
|
|
|
} |
638
|
16 |
|
} |
639
|
|
|
|
640
|
40 |
View Code Duplication |
public static function lengthBetween($value, $min, $max, $message = '') |
641
|
|
|
{ |
642
|
40 |
|
$length = self::strlen($value); |
643
|
|
|
|
644
|
40 |
|
if ($length < $min || $length > $max) { |
645
|
16 |
|
throw new InvalidArgumentException(sprintf( |
646
|
16 |
|
$message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
647
|
16 |
|
self::valueToString($value), |
648
|
|
|
$min, |
649
|
|
|
$max |
650
|
|
|
)); |
651
|
|
|
} |
652
|
24 |
|
} |
653
|
|
|
|
654
|
36 |
View Code Duplication |
public static function fileExists($value, $message = '') |
655
|
|
|
{ |
656
|
36 |
|
self::string($value); |
657
|
|
|
|
658
|
36 |
|
if (!file_exists($value)) { |
659
|
12 |
|
throw new InvalidArgumentException(sprintf( |
660
|
12 |
|
$message ?: 'The file %s does not exist.', |
661
|
12 |
|
self::valueToString($value) |
662
|
|
|
)); |
663
|
|
|
} |
664
|
24 |
|
} |
665
|
|
|
|
666
|
12 |
View Code Duplication |
public static function file($value, $message = '') |
667
|
|
|
{ |
668
|
12 |
|
self::fileExists($value, $message); |
669
|
|
|
|
670
|
8 |
|
if (!is_file($value)) { |
671
|
4 |
|
throw new InvalidArgumentException(sprintf( |
672
|
4 |
|
$message ?: 'The path %s is not a file.', |
673
|
4 |
|
self::valueToString($value) |
674
|
|
|
)); |
675
|
|
|
} |
676
|
4 |
|
} |
677
|
|
|
|
678
|
12 |
View Code Duplication |
public static function directory($value, $message = '') |
679
|
|
|
{ |
680
|
12 |
|
self::fileExists($value, $message); |
681
|
|
|
|
682
|
8 |
|
if (!is_dir($value)) { |
683
|
4 |
|
throw new InvalidArgumentException(sprintf( |
684
|
4 |
|
$message ?: 'The path %s is no directory.', |
685
|
4 |
|
self::valueToString($value) |
686
|
|
|
)); |
687
|
|
|
} |
688
|
4 |
|
} |
689
|
|
|
|
690
|
|
|
public static function readable($value, $message = '') |
691
|
|
|
{ |
692
|
|
|
if (!is_readable($value)) { |
693
|
|
|
throw new InvalidArgumentException(sprintf( |
694
|
|
|
$message ?: 'The path %s is not readable.', |
695
|
|
|
self::valueToString($value) |
696
|
|
|
)); |
697
|
|
|
} |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
public static function writable($value, $message = '') |
701
|
|
|
{ |
702
|
|
|
if (!is_writable($value)) { |
703
|
|
|
throw new InvalidArgumentException(sprintf( |
704
|
|
|
$message ?: 'The path %s is not writable.', |
705
|
|
|
self::valueToString($value) |
706
|
|
|
)); |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|
710
|
8 |
|
public static function classExists($value, $message = '') |
711
|
|
|
{ |
712
|
8 |
|
if (!class_exists($value)) { |
713
|
4 |
|
throw new InvalidArgumentException(sprintf( |
714
|
4 |
|
$message ?: 'Expected an existing class name. Got: %s', |
715
|
4 |
|
self::valueToString($value) |
716
|
|
|
)); |
717
|
|
|
} |
718
|
4 |
|
} |
719
|
|
|
|
720
|
8 |
View Code Duplication |
public static function subclassOf($value, $class, $message = '') |
721
|
|
|
{ |
722
|
8 |
|
if (!is_subclass_of($value, $class)) { |
723
|
4 |
|
throw new InvalidArgumentException(sprintf( |
724
|
4 |
|
$message ?: 'Expected a sub-class of %2$s. Got: %s', |
725
|
4 |
|
self::valueToString($value), |
726
|
4 |
|
self::valueToString($class) |
727
|
|
|
)); |
728
|
|
|
} |
729
|
4 |
|
} |
730
|
|
|
|
731
|
8 |
View Code Duplication |
public static function implementsInterface($value, $interface, $message = '') |
732
|
|
|
{ |
733
|
8 |
|
if (!in_array($interface, class_implements($value))) { |
734
|
4 |
|
throw new InvalidArgumentException(sprintf( |
735
|
4 |
|
$message ?: 'Expected an implementation of %2$s. Got: %s', |
736
|
4 |
|
self::valueToString($value), |
737
|
4 |
|
self::valueToString($interface) |
738
|
|
|
)); |
739
|
|
|
} |
740
|
4 |
|
} |
741
|
|
|
|
742
|
12 |
View Code Duplication |
public static function propertyExists($classOrObject, $property, $message = '') |
743
|
|
|
{ |
744
|
12 |
|
if (!property_exists($classOrObject, $property)) { |
745
|
4 |
|
throw new InvalidArgumentException(sprintf( |
746
|
4 |
|
$message ?: 'Expected the property %s to exist.', |
747
|
4 |
|
self::valueToString($property) |
748
|
|
|
)); |
749
|
|
|
} |
750
|
8 |
|
} |
751
|
|
|
|
752
|
12 |
View Code Duplication |
public static function propertyNotExists($classOrObject, $property, $message = '') |
753
|
|
|
{ |
754
|
12 |
|
if (property_exists($classOrObject, $property)) { |
755
|
8 |
|
throw new InvalidArgumentException(sprintf( |
756
|
8 |
|
$message ?: 'Expected the property %s to not exist.', |
757
|
8 |
|
self::valueToString($property) |
758
|
|
|
)); |
759
|
|
|
} |
760
|
4 |
|
} |
761
|
|
|
|
762
|
27 |
View Code Duplication |
public static function methodExists($classOrObject, $method, $message = '') |
763
|
|
|
{ |
764
|
27 |
|
if (!method_exists($classOrObject, $method)) { |
765
|
19 |
|
throw new InvalidArgumentException(sprintf( |
766
|
19 |
|
$message ?: 'Expected the method %s to exist.', |
767
|
19 |
|
self::valueToString($method) |
768
|
|
|
)); |
769
|
|
|
} |
770
|
8 |
|
} |
771
|
|
|
|
772
|
27 |
View Code Duplication |
public static function methodNotExists($classOrObject, $method, $message = '') |
773
|
|
|
{ |
774
|
27 |
|
if (method_exists($classOrObject, $method)) { |
775
|
8 |
|
throw new InvalidArgumentException(sprintf( |
776
|
8 |
|
$message ?: 'Expected the method %s to not exist.', |
777
|
8 |
|
self::valueToString($method) |
778
|
|
|
)); |
779
|
|
|
} |
780
|
19 |
|
} |
781
|
|
|
|
782
|
12 |
View Code Duplication |
public static function keyExists($array, $key, $message = '') |
783
|
|
|
{ |
784
|
12 |
|
if (!array_key_exists($key, $array)) { |
785
|
4 |
|
throw new InvalidArgumentException(sprintf( |
786
|
4 |
|
$message ?: 'Expected the key %s to exist.', |
787
|
4 |
|
self::valueToString($key) |
788
|
|
|
)); |
789
|
|
|
} |
790
|
8 |
|
} |
791
|
|
|
|
792
|
12 |
View Code Duplication |
public static function keyNotExists($array, $key, $message = '') |
793
|
|
|
{ |
794
|
12 |
|
if (array_key_exists($key, $array)) { |
795
|
8 |
|
throw new InvalidArgumentException(sprintf( |
796
|
8 |
|
$message ?: 'Expected the key %s to not exist.', |
797
|
8 |
|
self::valueToString($key) |
798
|
|
|
)); |
799
|
|
|
} |
800
|
4 |
|
} |
801
|
|
|
|
802
|
48 |
|
public static function uuid($value, $message = '') |
803
|
|
|
{ |
804
|
48 |
|
$value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
805
|
|
|
|
806
|
|
|
// The nil UUID is special form of UUID that is specified to have all |
807
|
|
|
// 128 bits set to zero. |
808
|
48 |
|
if ('00000000-0000-0000-0000-000000000000' === $value) { |
809
|
4 |
|
return; |
810
|
|
|
} |
811
|
|
|
|
812
|
44 |
|
if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { |
813
|
20 |
|
throw new InvalidArgumentException(sprintf( |
814
|
20 |
|
$message ?: 'Value "%s" is not a valid UUID.', |
815
|
20 |
|
self::valueToString($value) |
816
|
|
|
)); |
817
|
|
|
} |
818
|
24 |
|
} |
819
|
|
|
|
820
|
778 |
|
public static function __callStatic($name, $arguments) |
821
|
|
|
{ |
822
|
778 |
|
if ('nullOr' === substr($name, 0, 6)) { |
823
|
298 |
|
if (null !== $arguments[0]) { |
824
|
230 |
|
$method = lcfirst(substr($name, 6)); |
825
|
230 |
|
call_user_func_array(array('static', $method), $arguments); |
826
|
|
|
} |
827
|
|
|
|
828
|
182 |
|
return; |
829
|
|
|
} |
830
|
|
|
|
831
|
480 |
|
if ('all' === substr($name, 0, 3)) { |
832
|
480 |
|
self::isTraversable($arguments[0]); |
833
|
|
|
|
834
|
480 |
|
$method = lcfirst(substr($name, 3)); |
835
|
480 |
|
$args = $arguments; |
836
|
|
|
|
837
|
480 |
|
foreach ($arguments[0] as $entry) { |
838
|
480 |
|
$args[0] = $entry; |
839
|
|
|
|
840
|
480 |
|
call_user_func_array(array('static', $method), $args); |
841
|
|
|
} |
842
|
|
|
|
843
|
234 |
|
return; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
throw new BadMethodCallException('No such method: '.$name); |
847
|
|
|
} |
848
|
|
|
|
849
|
368 |
|
protected static function valueToString($value) |
850
|
|
|
{ |
851
|
368 |
|
if (null === $value) { |
852
|
11 |
|
return 'null'; |
853
|
|
|
} |
854
|
|
|
|
855
|
359 |
|
if (true === $value) { |
856
|
15 |
|
return 'true'; |
857
|
|
|
} |
858
|
|
|
|
859
|
349 |
|
if (false === $value) { |
860
|
13 |
|
return 'false'; |
861
|
|
|
} |
862
|
|
|
|
863
|
336 |
|
if (is_array($value)) { |
864
|
1 |
|
return 'array'; |
865
|
|
|
} |
866
|
|
|
|
867
|
335 |
|
if (is_object($value)) { |
868
|
1 |
|
return get_class($value); |
869
|
|
|
} |
870
|
|
|
|
871
|
334 |
|
if (is_resource($value)) { |
872
|
1 |
|
return 'resource'; |
873
|
|
|
} |
874
|
|
|
|
875
|
334 |
|
if (is_string($value)) { |
876
|
264 |
|
return '"'.$value.'"'; |
877
|
|
|
} |
878
|
|
|
|
879
|
78 |
|
return (string) $value; |
880
|
|
|
} |
881
|
|
|
|
882
|
129 |
|
protected static function typeToString($value) |
883
|
|
|
{ |
884
|
129 |
|
return is_object($value) ? get_class($value) : gettype($value); |
885
|
|
|
} |
886
|
|
|
|
887
|
124 |
|
protected static function strlen($value) |
888
|
|
|
{ |
889
|
124 |
|
if (!function_exists('mb_detect_encoding')) { |
890
|
|
|
return strlen($value); |
891
|
|
|
} |
892
|
|
|
|
893
|
124 |
|
if (false === $encoding = mb_detect_encoding($value)) { |
894
|
|
|
return strlen($value); |
895
|
|
|
} |
896
|
|
|
|
897
|
124 |
|
return mb_strwidth($value, $encoding); |
898
|
|
|
} |
899
|
|
|
|
900
|
|
|
private function __construct() |
901
|
|
|
{ |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
|