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