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