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