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