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