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