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