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