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, $value2, $message = '') |
55
|
|
|
* @method static void nullOrNotEq($value,$value2, $message = '') |
56
|
|
|
* @method static void nullOrSame($value, $value2, $message = '') |
57
|
|
|
* @method static void nullOrNotSame($value, $value2, $message = '') |
58
|
|
|
* @method static void nullOrGreaterThan($value, $value2, $message = '') |
59
|
|
|
* @method static void nullOrGreaterThanEq($value, $value2, $message = '') |
60
|
|
|
* @method static void nullOrLessThan($value, $value2, $message = '') |
61
|
|
|
* @method static void nullOrLessThanEq($value, $value2, $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, $value2, $message = '') |
135
|
|
|
* @method static void allNotEq($values,$value2, $message = '') |
136
|
|
|
* @method static void allSame($values, $value2, $message = '') |
137
|
|
|
* @method static void allNotSame($values, $value2, $message = '') |
138
|
|
|
* @method static void allGreaterThan($values, $value2, $message = '') |
139
|
|
|
* @method static void allGreaterThanEq($values, $value2, $message = '') |
140
|
|
|
* @method static void allLessThan($values, $value2, $message = '') |
141
|
|
|
* @method static void allLessThanEq($values, $value2, $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
|
20 |
|
public static function isTraversable($value, $message = '') |
326
|
|
|
{ |
327
|
20 |
|
@trigger_error( |
|
|
|
|
328
|
20 |
|
sprintf( |
329
|
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.', |
330
|
20 |
|
__METHOD__ |
331
|
|
|
), |
332
|
20 |
|
E_USER_DEPRECATED |
333
|
|
|
); |
334
|
|
|
|
335
|
20 |
|
if (!is_array($value) && !($value instanceof Traversable)) { |
336
|
8 |
|
static::reportInvalidArgument(sprintf( |
337
|
8 |
|
$message ?: 'Expected a traversable. Got: %s', |
338
|
8 |
|
static::typeToString($value) |
339
|
|
|
)); |
340
|
|
|
} |
341
|
12 |
|
} |
342
|
|
|
|
343
|
20 |
View Code Duplication |
public static function isArrayAccessible($value, $message = '') |
344
|
|
|
{ |
345
|
20 |
|
if (!is_array($value) && !($value instanceof ArrayAccess)) { |
346
|
8 |
|
static::reportInvalidArgument(sprintf( |
347
|
8 |
|
$message ?: 'Expected an array accessible. Got: %s', |
348
|
8 |
|
static::typeToString($value) |
349
|
|
|
)); |
350
|
|
|
} |
351
|
12 |
|
} |
352
|
|
|
|
353
|
24 |
View Code Duplication |
public static function isCountable($value, $message = '') |
354
|
|
|
{ |
355
|
24 |
|
if (!is_array($value) && !($value instanceof Countable)) { |
356
|
12 |
|
static::reportInvalidArgument(sprintf( |
357
|
12 |
|
$message ?: 'Expected a countable. Got: %s', |
358
|
12 |
|
static::typeToString($value) |
359
|
|
|
)); |
360
|
|
|
} |
361
|
12 |
|
} |
362
|
|
|
|
363
|
712 |
View Code Duplication |
public static function isIterable($value, $message = '') |
364
|
|
|
{ |
365
|
712 |
|
if (!is_array($value) && !($value instanceof Traversable)) { |
366
|
8 |
|
static::reportInvalidArgument(sprintf( |
367
|
8 |
|
$message ?: 'Expected an iterable. Got: %s', |
368
|
8 |
|
static::typeToString($value) |
369
|
|
|
)); |
370
|
|
|
} |
371
|
708 |
|
} |
372
|
|
|
|
373
|
16 |
|
public static function isInstanceOf($value, $class, $message = '') |
374
|
|
|
{ |
375
|
16 |
|
if (!($value instanceof $class)) { |
376
|
12 |
|
static::reportInvalidArgument(sprintf( |
377
|
12 |
|
$message ?: 'Expected an instance of %2$s. Got: %s', |
378
|
12 |
|
static::typeToString($value), |
379
|
12 |
|
$class |
380
|
|
|
)); |
381
|
|
|
} |
382
|
4 |
|
} |
383
|
|
|
|
384
|
16 |
|
public static function notInstanceOf($value, $class, $message = '') |
385
|
|
|
{ |
386
|
16 |
|
if ($value instanceof $class) { |
387
|
4 |
|
static::reportInvalidArgument(sprintf( |
388
|
4 |
|
$message ?: 'Expected an instance other than %2$s. Got: %s', |
389
|
4 |
|
static::typeToString($value), |
390
|
4 |
|
$class |
391
|
|
|
)); |
392
|
|
|
} |
393
|
12 |
|
} |
394
|
|
|
|
395
|
20 |
|
public static function isInstanceOfAny($value, array $classes, $message = '') |
396
|
|
|
{ |
397
|
20 |
|
foreach ($classes as $class) { |
398
|
20 |
|
if ($value instanceof $class) { |
399
|
20 |
|
return; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
12 |
|
static::reportInvalidArgument(sprintf( |
404
|
12 |
|
$message ?: 'Expected an instance of any of %2$s. Got: %s', |
405
|
12 |
|
static::typeToString($value), |
406
|
12 |
|
implode(', ', array_map(array('static', 'valueToString'), $classes)) |
407
|
|
|
)); |
408
|
|
|
} |
409
|
|
|
|
410
|
23 |
|
public static function isEmpty($value, $message = '') |
411
|
|
|
{ |
412
|
23 |
|
if (!empty($value)) { |
413
|
8 |
|
static::reportInvalidArgument(sprintf( |
414
|
8 |
|
$message ?: 'Expected an empty value. Got: %s', |
415
|
8 |
|
static::valueToString($value) |
416
|
|
|
)); |
417
|
|
|
} |
418
|
15 |
|
} |
419
|
|
|
|
420
|
23 |
|
public static function notEmpty($value, $message = '') |
421
|
|
|
{ |
422
|
23 |
|
if (empty($value)) { |
423
|
15 |
|
static::reportInvalidArgument(sprintf( |
424
|
15 |
|
$message ?: 'Expected a non-empty value. Got: %s', |
425
|
15 |
|
static::valueToString($value) |
426
|
|
|
)); |
427
|
|
|
} |
428
|
8 |
|
} |
429
|
|
|
|
430
|
11 |
|
public static function null($value, $message = '') |
431
|
|
|
{ |
432
|
11 |
|
if (null !== $value) { |
433
|
8 |
|
static::reportInvalidArgument(sprintf( |
434
|
8 |
|
$message ?: 'Expected null. Got: %s', |
435
|
8 |
|
static::valueToString($value) |
436
|
|
|
)); |
437
|
|
|
} |
438
|
3 |
|
} |
439
|
|
|
|
440
|
11 |
|
public static function notNull($value, $message = '') |
441
|
|
|
{ |
442
|
11 |
|
if (null === $value) { |
443
|
3 |
|
static::reportInvalidArgument( |
444
|
3 |
|
$message ?: 'Expected a value other than null.' |
445
|
|
|
); |
446
|
|
|
} |
447
|
8 |
|
} |
448
|
|
|
|
449
|
15 |
|
public static function true($value, $message = '') |
450
|
|
|
{ |
451
|
15 |
|
if (true !== $value) { |
452
|
11 |
|
static::reportInvalidArgument(sprintf( |
453
|
11 |
|
$message ?: 'Expected a value to be true. Got: %s', |
454
|
11 |
|
static::valueToString($value) |
455
|
|
|
)); |
456
|
|
|
} |
457
|
4 |
|
} |
458
|
|
|
|
459
|
19 |
|
public static function false($value, $message = '') |
460
|
|
|
{ |
461
|
19 |
|
if (false !== $value) { |
462
|
15 |
|
static::reportInvalidArgument(sprintf( |
463
|
15 |
|
$message ?: 'Expected a value to be false. Got: %s', |
464
|
15 |
|
static::valueToString($value) |
465
|
|
|
)); |
466
|
|
|
} |
467
|
4 |
|
} |
468
|
|
|
|
469
|
47 |
View Code Duplication |
public static function ip($value, $message = '') |
470
|
|
|
{ |
471
|
47 |
|
if (false === filter_var($value, FILTER_VALIDATE_IP)) { |
472
|
19 |
|
static::reportInvalidArgument(sprintf( |
473
|
19 |
|
$message ?: 'Expected a value to be an IP. Got: %s', |
474
|
19 |
|
static::valueToString($value) |
475
|
|
|
)); |
476
|
|
|
} |
477
|
28 |
|
} |
478
|
|
|
|
479
|
47 |
View Code Duplication |
public static function ipv4($value, $message = '') |
480
|
|
|
{ |
481
|
47 |
|
if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
482
|
35 |
|
static::reportInvalidArgument(sprintf( |
483
|
35 |
|
$message ?: 'Expected a value to be an IPv4. Got: %s', |
484
|
35 |
|
static::valueToString($value) |
485
|
|
|
)); |
486
|
|
|
} |
487
|
12 |
|
} |
488
|
|
|
|
489
|
47 |
View Code Duplication |
public static function ipv6($value, $message = '') |
490
|
|
|
{ |
491
|
47 |
|
if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
492
|
31 |
|
static::reportInvalidArgument(sprintf( |
493
|
31 |
|
$message ?: 'Expected a value to be an IPv6. Got %s', |
494
|
31 |
|
static::valueToString($value) |
495
|
|
|
)); |
496
|
|
|
} |
497
|
16 |
|
} |
498
|
|
|
|
499
|
8 |
|
public static function uniqueValues(array $values, $message = '') |
500
|
|
|
{ |
501
|
8 |
|
$countValues = array_count_values($values); |
502
|
|
|
|
503
|
8 |
|
$nonUniqueValues = array(); |
504
|
|
|
|
505
|
8 |
|
foreach ($countValues as $value => $count) { |
506
|
8 |
|
if ($count > 1) { |
507
|
8 |
|
array_push($nonUniqueValues, $nonUniqueValues); |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
8 |
|
if (!empty($nonUniqueValues)) { |
512
|
4 |
|
$countNonUniqueValues = count($nonUniqueValues); |
513
|
|
|
|
514
|
4 |
|
static::reportInvalidArgument(sprintf( |
515
|
4 |
|
$message ?: 'Expected an array of unique values, but %s %s duplicated: %s', |
516
|
4 |
|
$countNonUniqueValues, |
517
|
4 |
|
(1 === $countNonUniqueValues ? 'is' : 'are'), |
518
|
4 |
|
implode(', ', $nonUniqueValues) |
519
|
|
|
)); |
520
|
|
|
} |
521
|
4 |
|
} |
522
|
|
|
|
523
|
33 |
|
public static function eq($value, $value2, $message = '') |
524
|
|
|
{ |
525
|
33 |
|
if ($value2 != $value) { |
526
|
17 |
|
static::reportInvalidArgument(sprintf( |
527
|
17 |
|
$message ?: 'Expected a value equal to %2$s. Got: %s', |
528
|
17 |
|
static::valueToString($value), |
529
|
17 |
|
static::valueToString($value2) |
530
|
|
|
)); |
531
|
|
|
} |
532
|
16 |
|
} |
533
|
|
|
|
534
|
28 |
|
public static function notEq($value, $value2, $message = '') |
535
|
|
|
{ |
536
|
28 |
|
if ($value2 == $value) { |
537
|
16 |
|
static::reportInvalidArgument(sprintf( |
538
|
16 |
|
$message ?: 'Expected a different value than %s.', |
539
|
16 |
|
static::valueToString($value2) |
540
|
|
|
)); |
541
|
|
|
} |
542
|
12 |
|
} |
543
|
|
|
|
544
|
16 |
|
public static function same($value, $value2, $message = '') |
545
|
|
|
{ |
546
|
16 |
|
if ($value2 !== $value) { |
547
|
12 |
|
static::reportInvalidArgument(sprintf( |
548
|
12 |
|
$message ?: 'Expected a value identical to %2$s. Got: %s', |
549
|
12 |
|
static::valueToString($value), |
550
|
12 |
|
static::valueToString($value2) |
551
|
|
|
)); |
552
|
|
|
} |
553
|
4 |
|
} |
554
|
|
|
|
555
|
16 |
|
public static function notSame($value, $value2, $message = '') |
556
|
|
|
{ |
557
|
16 |
|
if ($value2 === $value) { |
558
|
4 |
|
static::reportInvalidArgument(sprintf( |
559
|
4 |
|
$message ?: 'Expected a value not identical to %s.', |
560
|
4 |
|
static::valueToString($value2) |
561
|
|
|
)); |
562
|
|
|
} |
563
|
12 |
|
} |
564
|
|
|
|
565
|
8 |
|
public static function greaterThan($value, $limit, $message = '') |
566
|
|
|
{ |
567
|
8 |
|
if ($value <= $limit) { |
568
|
4 |
|
static::reportInvalidArgument(sprintf( |
569
|
4 |
|
$message ?: 'Expected a value greater than %2$s. Got: %s', |
570
|
4 |
|
static::valueToString($value), |
571
|
4 |
|
static::valueToString($limit) |
572
|
|
|
)); |
573
|
|
|
} |
574
|
4 |
|
} |
575
|
|
|
|
576
|
12 |
|
public static function greaterThanEq($value, $limit, $message = '') |
577
|
|
|
{ |
578
|
12 |
|
if ($value < $limit) { |
579
|
4 |
|
static::reportInvalidArgument(sprintf( |
580
|
4 |
|
$message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
581
|
4 |
|
static::valueToString($value), |
582
|
4 |
|
static::valueToString($limit) |
583
|
|
|
)); |
584
|
|
|
} |
585
|
8 |
|
} |
586
|
|
|
|
587
|
8 |
|
public static function lessThan($value, $limit, $message = '') |
588
|
|
|
{ |
589
|
8 |
|
if ($value >= $limit) { |
590
|
4 |
|
static::reportInvalidArgument(sprintf( |
591
|
4 |
|
$message ?: 'Expected a value less than %2$s. Got: %s', |
592
|
4 |
|
static::valueToString($value), |
593
|
4 |
|
static::valueToString($limit) |
594
|
|
|
)); |
595
|
|
|
} |
596
|
4 |
|
} |
597
|
|
|
|
598
|
12 |
|
public static function lessThanEq($value, $limit, $message = '') |
599
|
|
|
{ |
600
|
12 |
|
if ($value > $limit) { |
601
|
4 |
|
static::reportInvalidArgument(sprintf( |
602
|
4 |
|
$message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
603
|
4 |
|
static::valueToString($value), |
604
|
4 |
|
static::valueToString($limit) |
605
|
|
|
)); |
606
|
|
|
} |
607
|
8 |
|
} |
608
|
|
|
|
609
|
16 |
View Code Duplication |
public static function range($value, $min, $max, $message = '') |
610
|
|
|
{ |
611
|
16 |
|
if ($value < $min || $value > $max) { |
612
|
8 |
|
static::reportInvalidArgument(sprintf( |
613
|
8 |
|
$message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
614
|
8 |
|
static::valueToString($value), |
615
|
8 |
|
static::valueToString($min), |
616
|
8 |
|
static::valueToString($max) |
617
|
|
|
)); |
618
|
|
|
} |
619
|
8 |
|
} |
620
|
|
|
|
621
|
8 |
|
public static function oneOf($value, array $values, $message = '') |
622
|
|
|
{ |
623
|
8 |
|
if (!in_array($value, $values, true)) { |
624
|
4 |
|
static::reportInvalidArgument(sprintf( |
625
|
4 |
|
$message ?: 'Expected one of: %2$s. Got: %s', |
626
|
4 |
|
static::valueToString($value), |
627
|
4 |
|
implode(', ', array_map(array('static', 'valueToString'), $values)) |
628
|
|
|
)); |
629
|
|
|
} |
630
|
4 |
|
} |
631
|
|
|
|
632
|
20 |
View Code Duplication |
public static function contains($value, $subString, $message = '') |
633
|
|
|
{ |
634
|
20 |
|
if (false === strpos($value, $subString)) { |
635
|
8 |
|
static::reportInvalidArgument(sprintf( |
636
|
8 |
|
$message ?: 'Expected a value to contain %2$s. Got: %s', |
637
|
8 |
|
static::valueToString($value), |
638
|
8 |
|
static::valueToString($subString) |
639
|
|
|
)); |
640
|
|
|
} |
641
|
12 |
|
} |
642
|
|
|
|
643
|
20 |
View Code Duplication |
public static function notContains($value, $subString, $message = '') |
644
|
|
|
{ |
645
|
20 |
|
if (false !== strpos($value, $subString)) { |
646
|
12 |
|
static::reportInvalidArgument(sprintf( |
647
|
12 |
|
$message ?: '%2$s was not expected to be contained in a value. Got: %s', |
648
|
12 |
|
static::valueToString($value), |
649
|
12 |
|
static::valueToString($subString) |
650
|
|
|
)); |
651
|
|
|
} |
652
|
8 |
|
} |
653
|
|
|
|
654
|
40 |
|
public static function notWhitespaceOnly($value, $message = '') |
655
|
|
|
{ |
656
|
40 |
|
if (preg_match('/^\s*$/', $value)) { |
657
|
24 |
|
static::reportInvalidArgument(sprintf( |
658
|
24 |
|
$message ?: 'Expected a non-whitespace string. Got: %s', |
659
|
24 |
|
static::valueToString($value) |
660
|
|
|
)); |
661
|
|
|
} |
662
|
16 |
|
} |
663
|
|
|
|
664
|
12 |
View Code Duplication |
public static function startsWith($value, $prefix, $message = '') |
665
|
|
|
{ |
666
|
12 |
|
if (0 !== strpos($value, $prefix)) { |
667
|
8 |
|
static::reportInvalidArgument(sprintf( |
668
|
8 |
|
$message ?: 'Expected a value to start with %2$s. Got: %s', |
669
|
8 |
|
static::valueToString($value), |
670
|
8 |
|
static::valueToString($prefix) |
671
|
|
|
)); |
672
|
|
|
} |
673
|
4 |
|
} |
674
|
|
|
|
675
|
12 |
|
public static function startsWithLetter($value, $message = '') |
676
|
|
|
{ |
677
|
12 |
|
$valid = isset($value[0]); |
678
|
|
|
|
679
|
12 |
|
if ($valid) { |
680
|
8 |
|
$locale = setlocale(LC_CTYPE, 0); |
681
|
8 |
|
setlocale(LC_CTYPE, 'C'); |
682
|
8 |
|
$valid = ctype_alpha($value[0]); |
683
|
8 |
|
setlocale(LC_CTYPE, $locale); |
684
|
|
|
} |
685
|
|
|
|
686
|
12 |
|
if (!$valid) { |
687
|
8 |
|
static::reportInvalidArgument(sprintf( |
688
|
8 |
|
$message ?: 'Expected a value to start with a letter. Got: %s', |
689
|
8 |
|
static::valueToString($value) |
690
|
|
|
)); |
691
|
|
|
} |
692
|
4 |
|
} |
693
|
|
|
|
694
|
12 |
View Code Duplication |
public static function endsWith($value, $suffix, $message = '') |
695
|
|
|
{ |
696
|
12 |
|
if ($suffix !== substr($value, -static::strlen($suffix))) { |
697
|
8 |
|
static::reportInvalidArgument(sprintf( |
698
|
8 |
|
$message ?: 'Expected a value to end with %2$s. Got: %s', |
699
|
8 |
|
static::valueToString($value), |
700
|
8 |
|
static::valueToString($suffix) |
701
|
|
|
)); |
702
|
|
|
} |
703
|
4 |
|
} |
704
|
|
|
|
705
|
12 |
|
public static function regex($value, $pattern, $message = '') |
706
|
|
|
{ |
707
|
12 |
|
if (!preg_match($pattern, $value)) { |
708
|
8 |
|
static::reportInvalidArgument(sprintf( |
709
|
8 |
|
$message ?: 'The value %s does not match the expected pattern.', |
710
|
8 |
|
static::valueToString($value) |
711
|
|
|
)); |
712
|
|
|
} |
713
|
4 |
|
} |
714
|
|
|
|
715
|
12 |
|
public static function notRegex($value, $pattern, $message = '') |
716
|
|
|
{ |
717
|
12 |
|
if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
718
|
4 |
|
static::reportInvalidArgument(sprintf( |
719
|
4 |
|
$message ?: 'The value %s matches the pattern %s (at offset %d).', |
720
|
4 |
|
static::valueToString($value), |
721
|
4 |
|
static::valueToString($pattern), |
722
|
4 |
|
$matches[0][1] |
723
|
|
|
)); |
724
|
|
|
} |
725
|
8 |
|
} |
726
|
|
|
|
727
|
12 |
View Code Duplication |
public static function alpha($value, $message = '') |
728
|
|
|
{ |
729
|
12 |
|
$locale = setlocale(LC_CTYPE, 0); |
730
|
12 |
|
setlocale(LC_CTYPE, 'C'); |
731
|
12 |
|
$valid = !ctype_alpha($value); |
732
|
12 |
|
setlocale(LC_CTYPE, $locale); |
733
|
|
|
|
734
|
12 |
|
if ($valid) { |
735
|
8 |
|
static::reportInvalidArgument(sprintf( |
736
|
8 |
|
$message ?: 'Expected a value to contain only letters. Got: %s', |
737
|
8 |
|
static::valueToString($value) |
738
|
|
|
)); |
739
|
|
|
} |
740
|
4 |
|
} |
741
|
|
|
|
742
|
12 |
View Code Duplication |
public static function digits($value, $message = '') |
743
|
|
|
{ |
744
|
12 |
|
$locale = setlocale(LC_CTYPE, 0); |
745
|
12 |
|
setlocale(LC_CTYPE, 'C'); |
746
|
12 |
|
$valid = !ctype_digit($value); |
747
|
12 |
|
setlocale(LC_CTYPE, $locale); |
748
|
|
|
|
749
|
12 |
|
if ($valid) { |
750
|
8 |
|
static::reportInvalidArgument(sprintf( |
751
|
8 |
|
$message ?: 'Expected a value to contain digits only. Got: %s', |
752
|
8 |
|
static::valueToString($value) |
753
|
|
|
)); |
754
|
|
|
} |
755
|
4 |
|
} |
756
|
|
|
|
757
|
12 |
View Code Duplication |
public static function alnum($value, $message = '') |
758
|
|
|
{ |
759
|
12 |
|
$locale = setlocale(LC_CTYPE, 0); |
760
|
12 |
|
setlocale(LC_CTYPE, 'C'); |
761
|
12 |
|
$valid = !ctype_alnum($value); |
762
|
12 |
|
setlocale(LC_CTYPE, $locale); |
763
|
|
|
|
764
|
12 |
|
if ($valid) { |
765
|
8 |
|
static::reportInvalidArgument(sprintf( |
766
|
8 |
|
$message ?: 'Expected a value to contain letters and digits only. Got: %s', |
767
|
8 |
|
static::valueToString($value) |
768
|
|
|
)); |
769
|
|
|
} |
770
|
4 |
|
} |
771
|
|
|
|
772
|
16 |
View Code Duplication |
public static function lower($value, $message = '') |
773
|
|
|
{ |
774
|
16 |
|
$locale = setlocale(LC_CTYPE, 0); |
775
|
16 |
|
setlocale(LC_CTYPE, 'C'); |
776
|
16 |
|
$valid = !ctype_lower($value); |
777
|
16 |
|
setlocale(LC_CTYPE, $locale); |
778
|
|
|
|
779
|
16 |
|
if ($valid) { |
780
|
12 |
|
static::reportInvalidArgument(sprintf( |
781
|
12 |
|
$message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
782
|
12 |
|
static::valueToString($value) |
783
|
|
|
)); |
784
|
|
|
} |
785
|
4 |
|
} |
786
|
|
|
|
787
|
16 |
View Code Duplication |
public static function upper($value, $message = '') |
788
|
|
|
{ |
789
|
16 |
|
$locale = setlocale(LC_CTYPE, 0); |
790
|
16 |
|
setlocale(LC_CTYPE, 'C'); |
791
|
16 |
|
$valid = !ctype_upper($value); |
792
|
16 |
|
setlocale(LC_CTYPE, $locale); |
793
|
|
|
|
794
|
16 |
|
if ($valid) { |
795
|
12 |
|
static::reportInvalidArgument(sprintf( |
796
|
12 |
|
$message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
797
|
12 |
|
static::valueToString($value) |
798
|
|
|
)); |
799
|
|
|
} |
800
|
4 |
|
} |
801
|
|
|
|
802
|
24 |
|
public static function length($value, $length, $message = '') |
803
|
|
|
{ |
804
|
24 |
|
if ($length !== static::strlen($value)) { |
805
|
16 |
|
static::reportInvalidArgument(sprintf( |
806
|
16 |
|
$message ?: 'Expected a value to contain %2$s characters. Got: %s', |
807
|
16 |
|
static::valueToString($value), |
808
|
16 |
|
$length |
809
|
|
|
)); |
810
|
|
|
} |
811
|
8 |
|
} |
812
|
|
|
|
813
|
24 |
|
public static function minLength($value, $min, $message = '') |
814
|
|
|
{ |
815
|
24 |
|
if (static::strlen($value) < $min) { |
816
|
8 |
|
static::reportInvalidArgument(sprintf( |
817
|
8 |
|
$message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
818
|
8 |
|
static::valueToString($value), |
819
|
8 |
|
$min |
820
|
|
|
)); |
821
|
|
|
} |
822
|
16 |
|
} |
823
|
|
|
|
824
|
24 |
|
public static function maxLength($value, $max, $message = '') |
825
|
|
|
{ |
826
|
24 |
|
if (static::strlen($value) > $max) { |
827
|
8 |
|
static::reportInvalidArgument(sprintf( |
828
|
8 |
|
$message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
829
|
8 |
|
static::valueToString($value), |
830
|
8 |
|
$max |
831
|
|
|
)); |
832
|
|
|
} |
833
|
16 |
|
} |
834
|
|
|
|
835
|
40 |
View Code Duplication |
public static function lengthBetween($value, $min, $max, $message = '') |
836
|
|
|
{ |
837
|
40 |
|
$length = static::strlen($value); |
838
|
|
|
|
839
|
40 |
|
if ($length < $min || $length > $max) { |
840
|
16 |
|
static::reportInvalidArgument(sprintf( |
841
|
16 |
|
$message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
842
|
16 |
|
static::valueToString($value), |
843
|
16 |
|
$min, |
844
|
16 |
|
$max |
845
|
|
|
)); |
846
|
|
|
} |
847
|
24 |
|
} |
848
|
|
|
|
849
|
36 |
|
public static function fileExists($value, $message = '') |
850
|
|
|
{ |
851
|
36 |
|
static::string($value); |
852
|
|
|
|
853
|
36 |
|
if (!file_exists($value)) { |
854
|
12 |
|
static::reportInvalidArgument(sprintf( |
855
|
12 |
|
$message ?: 'The file %s does not exist.', |
856
|
12 |
|
static::valueToString($value) |
857
|
|
|
)); |
858
|
|
|
} |
859
|
24 |
|
} |
860
|
|
|
|
861
|
12 |
View Code Duplication |
public static function file($value, $message = '') |
862
|
|
|
{ |
863
|
12 |
|
static::fileExists($value, $message); |
864
|
|
|
|
865
|
8 |
|
if (!is_file($value)) { |
866
|
4 |
|
static::reportInvalidArgument(sprintf( |
867
|
4 |
|
$message ?: 'The path %s is not a file.', |
868
|
4 |
|
static::valueToString($value) |
869
|
|
|
)); |
870
|
|
|
} |
871
|
4 |
|
} |
872
|
|
|
|
873
|
12 |
View Code Duplication |
public static function directory($value, $message = '') |
874
|
|
|
{ |
875
|
12 |
|
static::fileExists($value, $message); |
876
|
|
|
|
877
|
8 |
|
if (!is_dir($value)) { |
878
|
4 |
|
static::reportInvalidArgument(sprintf( |
879
|
4 |
|
$message ?: 'The path %s is no directory.', |
880
|
4 |
|
static::valueToString($value) |
881
|
|
|
)); |
882
|
|
|
} |
883
|
4 |
|
} |
884
|
|
|
|
885
|
|
|
public static function readable($value, $message = '') |
886
|
|
|
{ |
887
|
|
|
if (!is_readable($value)) { |
888
|
|
|
static::reportInvalidArgument(sprintf( |
889
|
|
|
$message ?: 'The path %s is not readable.', |
890
|
|
|
static::valueToString($value) |
891
|
|
|
)); |
892
|
|
|
} |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
public static function writable($value, $message = '') |
896
|
|
|
{ |
897
|
|
|
if (!is_writable($value)) { |
898
|
|
|
static::reportInvalidArgument(sprintf( |
899
|
|
|
$message ?: 'The path %s is not writable.', |
900
|
|
|
static::valueToString($value) |
901
|
|
|
)); |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
|
905
|
8 |
|
public static function classExists($value, $message = '') |
906
|
|
|
{ |
907
|
8 |
|
if (!class_exists($value)) { |
908
|
4 |
|
static::reportInvalidArgument(sprintf( |
909
|
4 |
|
$message ?: 'Expected an existing class name. Got: %s', |
910
|
4 |
|
static::valueToString($value) |
911
|
|
|
)); |
912
|
|
|
} |
913
|
4 |
|
} |
914
|
|
|
|
915
|
8 |
|
public static function subclassOf($value, $class, $message = '') |
916
|
|
|
{ |
917
|
8 |
|
if (!is_subclass_of($value, $class)) { |
918
|
4 |
|
static::reportInvalidArgument(sprintf( |
919
|
4 |
|
$message ?: 'Expected a sub-class of %2$s. Got: %s', |
920
|
4 |
|
static::valueToString($value), |
921
|
4 |
|
static::valueToString($class) |
922
|
|
|
)); |
923
|
|
|
} |
924
|
4 |
|
} |
925
|
|
|
|
926
|
8 |
|
public static function interfaceExists($value, $message = '') |
927
|
|
|
{ |
928
|
8 |
|
if (!interface_exists($value)) { |
929
|
4 |
|
static::reportInvalidArgument(sprintf( |
930
|
4 |
|
$message ?: 'Expected an existing interface name. got %s', |
931
|
4 |
|
static::valueToString($value) |
932
|
|
|
)); |
933
|
|
|
} |
934
|
4 |
|
} |
935
|
|
|
|
936
|
8 |
View Code Duplication |
public static function implementsInterface($value, $interface, $message = '') |
937
|
|
|
{ |
938
|
8 |
|
if (!in_array($interface, class_implements($value))) { |
939
|
4 |
|
static::reportInvalidArgument(sprintf( |
940
|
4 |
|
$message ?: 'Expected an implementation of %2$s. Got: %s', |
941
|
4 |
|
static::valueToString($value), |
942
|
4 |
|
static::valueToString($interface) |
943
|
|
|
)); |
944
|
|
|
} |
945
|
4 |
|
} |
946
|
|
|
|
947
|
12 |
View Code Duplication |
public static function propertyExists($classOrObject, $property, $message = '') |
948
|
|
|
{ |
949
|
12 |
|
if (!property_exists($classOrObject, $property)) { |
950
|
4 |
|
static::reportInvalidArgument(sprintf( |
951
|
4 |
|
$message ?: 'Expected the property %s to exist.', |
952
|
4 |
|
static::valueToString($property) |
953
|
|
|
)); |
954
|
|
|
} |
955
|
8 |
|
} |
956
|
|
|
|
957
|
12 |
View Code Duplication |
public static function propertyNotExists($classOrObject, $property, $message = '') |
958
|
|
|
{ |
959
|
12 |
|
if (property_exists($classOrObject, $property)) { |
960
|
8 |
|
static::reportInvalidArgument(sprintf( |
961
|
8 |
|
$message ?: 'Expected the property %s to not exist.', |
962
|
8 |
|
static::valueToString($property) |
963
|
|
|
)); |
964
|
|
|
} |
965
|
4 |
|
} |
966
|
|
|
|
967
|
27 |
View Code Duplication |
public static function methodExists($classOrObject, $method, $message = '') |
968
|
|
|
{ |
969
|
27 |
|
if (!method_exists($classOrObject, $method)) { |
970
|
19 |
|
static::reportInvalidArgument(sprintf( |
971
|
19 |
|
$message ?: 'Expected the method %s to exist.', |
972
|
19 |
|
static::valueToString($method) |
973
|
|
|
)); |
974
|
|
|
} |
975
|
8 |
|
} |
976
|
|
|
|
977
|
27 |
View Code Duplication |
public static function methodNotExists($classOrObject, $method, $message = '') |
978
|
|
|
{ |
979
|
27 |
|
if (method_exists($classOrObject, $method)) { |
980
|
8 |
|
static::reportInvalidArgument(sprintf( |
981
|
8 |
|
$message ?: 'Expected the method %s to not exist.', |
982
|
8 |
|
static::valueToString($method) |
983
|
|
|
)); |
984
|
|
|
} |
985
|
19 |
|
} |
986
|
|
|
|
987
|
12 |
View Code Duplication |
public static function keyExists($array, $key, $message = '') |
988
|
|
|
{ |
989
|
12 |
|
if (!(isset($array[$key]) || array_key_exists($key, $array))) { |
990
|
4 |
|
static::reportInvalidArgument(sprintf( |
991
|
4 |
|
$message ?: 'Expected the key %s to exist.', |
992
|
4 |
|
static::valueToString($key) |
993
|
|
|
)); |
994
|
|
|
} |
995
|
8 |
|
} |
996
|
|
|
|
997
|
12 |
View Code Duplication |
public static function keyNotExists($array, $key, $message = '') |
998
|
|
|
{ |
999
|
12 |
|
if (isset($array[$key]) || array_key_exists($key, $array)) { |
1000
|
8 |
|
static::reportInvalidArgument(sprintf( |
1001
|
8 |
|
$message ?: 'Expected the key %s to not exist.', |
1002
|
8 |
|
static::valueToString($key) |
1003
|
|
|
)); |
1004
|
|
|
} |
1005
|
4 |
|
} |
1006
|
|
|
|
1007
|
8 |
|
public static function count($array, $number, $message = '') |
1008
|
|
|
{ |
1009
|
8 |
|
static::eq( |
1010
|
8 |
|
count($array), |
1011
|
8 |
|
$number, |
1012
|
8 |
|
$message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
1013
|
|
|
); |
1014
|
4 |
|
} |
1015
|
|
|
|
1016
|
12 |
View Code Duplication |
public static function minCount($array, $min, $message = '') |
1017
|
|
|
{ |
1018
|
12 |
|
if (count($array) < $min) { |
1019
|
4 |
|
static::reportInvalidArgument(sprintf( |
1020
|
4 |
|
$message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
1021
|
4 |
|
count($array), |
1022
|
4 |
|
$min |
1023
|
|
|
)); |
1024
|
|
|
} |
1025
|
8 |
|
} |
1026
|
|
|
|
1027
|
12 |
View Code Duplication |
public static function maxCount($array, $max, $message = '') |
1028
|
|
|
{ |
1029
|
12 |
|
if (count($array) > $max) { |
1030
|
4 |
|
static::reportInvalidArgument(sprintf( |
1031
|
4 |
|
$message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
1032
|
4 |
|
count($array), |
1033
|
4 |
|
$max |
1034
|
|
|
)); |
1035
|
|
|
} |
1036
|
8 |
|
} |
1037
|
|
|
|
1038
|
12 |
|
public static function countBetween($array, $min, $max, $message = '') |
1039
|
|
|
{ |
1040
|
12 |
|
$count = count($array); |
1041
|
|
|
|
1042
|
12 |
|
if ($count < $min || $count > $max) { |
1043
|
8 |
|
static::reportInvalidArgument(sprintf( |
1044
|
8 |
|
$message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', |
1045
|
8 |
|
$count, |
1046
|
8 |
|
$min, |
1047
|
8 |
|
$max |
1048
|
|
|
)); |
1049
|
|
|
} |
1050
|
4 |
|
} |
1051
|
|
|
|
1052
|
16 |
|
public static function isList($array, $message = '') |
1053
|
|
|
{ |
1054
|
16 |
|
if (!is_array($array) || !$array || array_keys($array) !== range(0, count($array) - 1)) { |
|
|
|
|
1055
|
12 |
|
static::reportInvalidArgument( |
1056
|
12 |
|
$message ?: 'Expected list - non-associative array.' |
1057
|
|
|
); |
1058
|
|
|
} |
1059
|
4 |
|
} |
1060
|
|
|
|
1061
|
16 |
|
public static function isMap($array, $message = '') |
1062
|
|
|
{ |
1063
|
|
|
if ( |
1064
|
16 |
|
!is_array($array) || |
1065
|
16 |
|
!$array || |
|
|
|
|
1066
|
16 |
|
array_keys($array) !== array_filter(array_keys($array), function ($key) { |
1067
|
12 |
|
return is_string($key); |
1068
|
16 |
|
}) |
1069
|
|
|
) { |
1070
|
12 |
|
static::reportInvalidArgument( |
1071
|
12 |
|
$message ?: 'Expected map - associative array with string keys.' |
1072
|
|
|
); |
1073
|
|
|
} |
1074
|
4 |
|
} |
1075
|
|
|
|
1076
|
48 |
|
public static function uuid($value, $message = '') |
1077
|
|
|
{ |
1078
|
48 |
|
$value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
1079
|
|
|
|
1080
|
|
|
// The nil UUID is special form of UUID that is specified to have all |
1081
|
|
|
// 128 bits set to zero. |
1082
|
48 |
|
if ('00000000-0000-0000-0000-000000000000' === $value) { |
1083
|
4 |
|
return; |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
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)) { |
1087
|
20 |
|
static::reportInvalidArgument(sprintf( |
1088
|
20 |
|
$message ?: 'Value %s is not a valid UUID.', |
1089
|
20 |
|
static::valueToString($value) |
1090
|
|
|
)); |
1091
|
|
|
} |
1092
|
24 |
|
} |
1093
|
|
|
|
1094
|
24 |
|
public static function throws(Closure $expression, $class = 'Exception', $message = '') |
1095
|
|
|
{ |
1096
|
24 |
|
static::string($class); |
1097
|
|
|
|
1098
|
24 |
|
$actual = 'none'; |
1099
|
|
|
|
1100
|
|
|
try { |
1101
|
24 |
|
$expression(); |
1102
|
24 |
|
} catch (Exception $e) { |
1103
|
20 |
|
$actual = get_class($e); |
1104
|
20 |
|
if ($e instanceof $class) { |
1105
|
20 |
|
return; |
1106
|
|
|
} |
1107
|
4 |
|
} catch (Throwable $e) { |
|
|
|
|
1108
|
4 |
|
$actual = get_class($e); |
1109
|
4 |
|
if ($e instanceof $class) { |
1110
|
4 |
|
return; |
1111
|
|
|
} |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
8 |
|
static::reportInvalidArgument($message ?: sprintf( |
1115
|
8 |
|
'Expected to throw "%s", got "%s"', |
1116
|
8 |
|
$class, |
1117
|
8 |
|
$actual |
1118
|
|
|
)); |
1119
|
|
|
} |
1120
|
|
|
|
1121
|
1131 |
|
public static function __callStatic($name, $arguments) |
1122
|
|
|
{ |
1123
|
1131 |
|
if ('nullOr' === substr($name, 0, 6)) { |
1124
|
429 |
|
if (null !== $arguments[0]) { |
1125
|
338 |
|
$method = lcfirst(substr($name, 6)); |
1126
|
338 |
|
call_user_func_array(array('static', $method), $arguments); |
1127
|
|
|
} |
1128
|
|
|
|
1129
|
255 |
|
return; |
1130
|
|
|
} |
1131
|
|
|
|
1132
|
702 |
|
if ('all' === substr($name, 0, 3)) { |
1133
|
702 |
|
static::isIterable($arguments[0]); |
1134
|
|
|
|
1135
|
702 |
|
$method = lcfirst(substr($name, 3)); |
1136
|
702 |
|
$args = $arguments; |
1137
|
|
|
|
1138
|
702 |
|
foreach ($arguments[0] as $entry) { |
1139
|
702 |
|
$args[0] = $entry; |
1140
|
|
|
|
1141
|
702 |
|
call_user_func_array(array('static', $method), $args); |
1142
|
|
|
} |
1143
|
|
|
|
1144
|
334 |
|
return; |
1145
|
|
|
} |
1146
|
|
|
|
1147
|
|
|
throw new BadMethodCallException('No such method: '.$name); |
1148
|
|
|
} |
1149
|
|
|
|
1150
|
530 |
|
protected static function valueToString($value) |
1151
|
|
|
{ |
1152
|
530 |
|
if (null === $value) { |
1153
|
20 |
|
return 'null'; |
1154
|
|
|
} |
1155
|
|
|
|
1156
|
512 |
|
if (true === $value) { |
1157
|
15 |
|
return 'true'; |
1158
|
|
|
} |
1159
|
|
|
|
1160
|
502 |
|
if (false === $value) { |
1161
|
25 |
|
return 'false'; |
1162
|
|
|
} |
1163
|
|
|
|
1164
|
477 |
|
if (is_array($value)) { |
1165
|
13 |
|
return 'array'; |
1166
|
|
|
} |
1167
|
|
|
|
1168
|
464 |
|
if (is_object($value)) { |
1169
|
2 |
|
if (method_exists($value, '__toString')) { |
1170
|
1 |
|
return get_class($value).': '.self::valueToString($value->__toString()); |
1171
|
|
|
} |
1172
|
|
|
|
1173
|
1 |
|
return get_class($value); |
1174
|
|
|
} |
1175
|
|
|
|
1176
|
463 |
|
if (is_resource($value)) { |
1177
|
1 |
|
return 'resource'; |
1178
|
|
|
} |
1179
|
|
|
|
1180
|
463 |
|
if (is_string($value)) { |
1181
|
365 |
|
return '"'.$value.'"'; |
1182
|
|
|
} |
1183
|
|
|
|
1184
|
106 |
|
return (string) $value; |
1185
|
|
|
} |
1186
|
|
|
|
1187
|
169 |
|
protected static function typeToString($value) |
1188
|
|
|
{ |
1189
|
169 |
|
return is_object($value) ? get_class($value) : gettype($value); |
1190
|
|
|
} |
1191
|
|
|
|
1192
|
124 |
|
protected static function strlen($value) |
1193
|
|
|
{ |
1194
|
124 |
|
if (!function_exists('mb_detect_encoding')) { |
1195
|
|
|
return strlen($value); |
1196
|
|
|
} |
1197
|
|
|
|
1198
|
124 |
|
if (false === $encoding = mb_detect_encoding($value)) { |
1199
|
|
|
return strlen($value); |
1200
|
|
|
} |
1201
|
|
|
|
1202
|
124 |
|
return mb_strwidth($value, $encoding); |
1203
|
|
|
} |
1204
|
|
|
|
1205
|
738 |
|
protected static function reportInvalidArgument($message) |
1206
|
|
|
{ |
1207
|
738 |
|
throw new InvalidArgumentException($message); |
1208
|
|
|
} |
1209
|
|
|
|
1210
|
|
|
private function __construct() |
1211
|
|
|
{ |
1212
|
|
|
} |
1213
|
|
|
} |
1214
|
|
|
|