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