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