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