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