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