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 DateTime; |
19
|
|
|
use DateTimeImmutable; |
20
|
|
|
use Exception; |
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
use ResourceBundle; |
23
|
|
|
use SimpleXMLElement; |
24
|
|
|
use Throwable; |
25
|
|
|
use Traversable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Efficient assertions to validate the input/output of your methods. |
29
|
|
|
* |
30
|
|
|
* @method static void nullOrString($value, $message = '') |
31
|
|
|
* @method static void nullOrStringNotEmpty($value, $message = '') |
32
|
|
|
* @method static void nullOrInteger($value, $message = '') |
33
|
|
|
* @method static void nullOrIntegerish($value, $message = '') |
34
|
|
|
* @method static void nullOrFloat($value, $message = '') |
35
|
|
|
* @method static void nullOrNumeric($value, $message = '') |
36
|
|
|
* @method static void nullOrNatural($value, $message = '') |
37
|
|
|
* @method static void nullOrBoolean($value, $message = '') |
38
|
|
|
* @method static void nullOrScalar($value, $message = '') |
39
|
|
|
* @method static void nullOrObject($value, $message = '') |
40
|
|
|
* @method static void nullOrResource($value, $type = null, $message = '') |
41
|
|
|
* @method static void nullOrIsCallable($value, $message = '') |
42
|
|
|
* @method static void nullOrIsArray($value, $message = '') |
43
|
|
|
* @method static void nullOrIsTraversable($value, $message = '') |
44
|
|
|
* @method static void nullOrIsArrayAccessible($value, $message = '') |
45
|
|
|
* @method static void nullOrIsCountable($value, $message = '') |
46
|
|
|
* @method static void nullOrIsIterable($value, $message = '') |
47
|
|
|
* @method static void nullOrIsInstanceOf($value, $class, $message = '') |
48
|
|
|
* @method static void nullOrNotInstanceOf($value, $class, $message = '') |
49
|
|
|
* @method static void nullOrIsInstanceOfAny($value, $classes, $message = '') |
50
|
|
|
* @method static void nullOrIsAOf($value, $classes, $message = '') |
51
|
|
|
* @method static void nullOrIsAnyOf($value, $classes, $message = '') |
52
|
|
|
* @method static void nullOrIsNotA($value, $classes, $message = '') |
53
|
|
|
* @method static void nullOrIsEmpty($value, $message = '') |
54
|
|
|
* @method static void nullOrNotEmpty($value, $message = '') |
55
|
|
|
* @method static void nullOrTrue($value, $message = '') |
56
|
|
|
* @method static void nullOrFalse($value, $message = '') |
57
|
|
|
* @method static void nullOrNotFalse($value, $message = '') |
58
|
|
|
* @method static void nullOrIp($value, $message = '') |
59
|
|
|
* @method static void nullOrIpv4($value, $message = '') |
60
|
|
|
* @method static void nullOrIpv6($value, $message = '') |
61
|
|
|
* @method static void nullOrEmail($value, $message = '') |
62
|
|
|
* @method static void nullOrUniqueValues($values, $message = '') |
63
|
|
|
* @method static void nullOrEq($value, $expect, $message = '') |
64
|
|
|
* @method static void nullOrNotEq($value, $expect, $message = '') |
65
|
|
|
* @method static void nullOrSame($value, $expect, $message = '') |
66
|
|
|
* @method static void nullOrNotSame($value, $expect, $message = '') |
67
|
|
|
* @method static void nullOrGreaterThan($value, $limit, $message = '') |
68
|
|
|
* @method static void nullOrGreaterThanEq($value, $limit, $message = '') |
69
|
|
|
* @method static void nullOrLessThan($value, $limit, $message = '') |
70
|
|
|
* @method static void nullOrLessThanEq($value, $limit, $message = '') |
71
|
|
|
* @method static void nullOrRange($value, $min, $max, $message = '') |
72
|
|
|
* @method static void nullOrOneOf($value, $values, $message = '') |
73
|
|
|
* @method static void nullOrContains($value, $subString, $message = '') |
74
|
|
|
* @method static void nullOrNotContains($value, $subString, $message = '') |
75
|
|
|
* @method static void nullOrNotWhitespaceOnly($value, $message = '') |
76
|
|
|
* @method static void nullOrStartsWith($value, $prefix, $message = '') |
77
|
|
|
* @method static void nullOrNotStartsWith($value, $prefix, $message = '') |
78
|
|
|
* @method static void nullOrStartsWithLetter($value, $message = '') |
79
|
|
|
* @method static void nullOrEndsWith($value, $suffix, $message = '') |
80
|
|
|
* @method static void nullOrNotEndsWith($value, $suffix, $message = '') |
81
|
|
|
* @method static void nullOrRegex($value, $pattern, $message = '') |
82
|
|
|
* @method static void nullOrNotRegex($value, $pattern, $message = '') |
83
|
|
|
* @method static void nullOrUnicodeLetters($value, $message = '') |
84
|
|
|
* @method static void nullOrAlpha($value, $message = '') |
85
|
|
|
* @method static void nullOrDigits($value, $message = '') |
86
|
|
|
* @method static void nullOrAlnum($value, $message = '') |
87
|
|
|
* @method static void nullOrLower($value, $message = '') |
88
|
|
|
* @method static void nullOrUpper($value, $message = '') |
89
|
|
|
* @method static void nullOrLength($value, $length, $message = '') |
90
|
|
|
* @method static void nullOrMinLength($value, $min, $message = '') |
91
|
|
|
* @method static void nullOrMaxLength($value, $max, $message = '') |
92
|
|
|
* @method static void nullOrLengthBetween($value, $min, $max, $message = '') |
93
|
|
|
* @method static void nullOrFileExists($value, $message = '') |
94
|
|
|
* @method static void nullOrFile($value, $message = '') |
95
|
|
|
* @method static void nullOrDirectory($value, $message = '') |
96
|
|
|
* @method static void nullOrReadable($value, $message = '') |
97
|
|
|
* @method static void nullOrWritable($value, $message = '') |
98
|
|
|
* @method static void nullOrClassExists($value, $message = '') |
99
|
|
|
* @method static void nullOrSubclassOf($value, $class, $message = '') |
100
|
|
|
* @method static void nullOrInterfaceExists($value, $message = '') |
101
|
|
|
* @method static void nullOrImplementsInterface($value, $interface, $message = '') |
102
|
|
|
* @method static void nullOrPropertyExists($value, $property, $message = '') |
103
|
|
|
* @method static void nullOrPropertyNotExists($value, $property, $message = '') |
104
|
|
|
* @method static void nullOrMethodExists($value, $method, $message = '') |
105
|
|
|
* @method static void nullOrMethodNotExists($value, $method, $message = '') |
106
|
|
|
* @method static void nullOrKeyExists($value, $key, $message = '') |
107
|
|
|
* @method static void nullOrKeyNotExists($value, $key, $message = '') |
108
|
|
|
* @method static void nullOrValidArrayKey($value, $message = '') |
109
|
|
|
* @method static void nullOrCount($value, $key, $message = '') |
110
|
|
|
* @method static void nullOrMinCount($value, $min, $message = '') |
111
|
|
|
* @method static void nullOrMaxCount($value, $max, $message = '') |
112
|
|
|
* @method static void nullOrIsList($value, $message = '') |
113
|
|
|
* @method static void nullOrIsNonEmptyList($value, $message = '') |
114
|
|
|
* @method static void nullOrIsMap($value, $message = '') |
115
|
|
|
* @method static void nullOrIsNonEmptyMap($value, $message = '') |
116
|
|
|
* @method static void nullOrCountBetween($value, $min, $max, $message = '') |
117
|
|
|
* @method static void nullOrUuid($values, $message = '') |
118
|
|
|
* @method static void nullOrThrows($expression, $class = 'Exception', $message = '') |
119
|
|
|
* @method static void allString($values, $message = '') |
120
|
|
|
* @method static void allStringNotEmpty($values, $message = '') |
121
|
|
|
* @method static void allInteger($values, $message = '') |
122
|
|
|
* @method static void allIntegerish($values, $message = '') |
123
|
|
|
* @method static void allFloat($values, $message = '') |
124
|
|
|
* @method static void allNumeric($values, $message = '') |
125
|
|
|
* @method static void allNatural($values, $message = '') |
126
|
|
|
* @method static void allBoolean($values, $message = '') |
127
|
|
|
* @method static void allScalar($values, $message = '') |
128
|
|
|
* @method static void allObject($values, $message = '') |
129
|
|
|
* @method static void allResource($values, $type = null, $message = '') |
130
|
|
|
* @method static void allIsCallable($values, $message = '') |
131
|
|
|
* @method static void allIsArray($values, $message = '') |
132
|
|
|
* @method static void allIsTraversable($values, $message = '') |
133
|
|
|
* @method static void allIsArrayAccessible($values, $message = '') |
134
|
|
|
* @method static void allIsCountable($values, $message = '') |
135
|
|
|
* @method static void allIsIterable($values, $message = '') |
136
|
|
|
* @method static void allIsInstanceOf($values, $class, $message = '') |
137
|
|
|
* @method static void allNotInstanceOf($values, $class, $message = '') |
138
|
|
|
* @method static void allIsInstanceOfAny($values, $classes, $message = '') |
139
|
|
|
* @method static void allIsAOf($values, $class, $message = '') |
140
|
|
|
* @method static void allIsAnyOf($values, $class, $message = '') |
141
|
|
|
* @method static void allIsNotA($values, $class, $message = '') |
142
|
|
|
* @method static void allNull($values, $message = '') |
143
|
|
|
* @method static void allNotNull($values, $message = '') |
144
|
|
|
* @method static void allIsEmpty($values, $message = '') |
145
|
|
|
* @method static void allNotEmpty($values, $message = '') |
146
|
|
|
* @method static void allTrue($values, $message = '') |
147
|
|
|
* @method static void allFalse($values, $message = '') |
148
|
|
|
* @method static void allNotFalse($values, $message = '') |
149
|
|
|
* @method static void allIp($values, $message = '') |
150
|
|
|
* @method static void allIpv4($values, $message = '') |
151
|
|
|
* @method static void allIpv6($values, $message = '') |
152
|
|
|
* @method static void allEmail($values, $message = '') |
153
|
|
|
* @method static void allUniqueValues($values, $message = '') |
154
|
|
|
* @method static void allEq($values, $expect, $message = '') |
155
|
|
|
* @method static void allNotEq($values, $expect, $message = '') |
156
|
|
|
* @method static void allSame($values, $expect, $message = '') |
157
|
|
|
* @method static void allNotSame($values, $expect, $message = '') |
158
|
|
|
* @method static void allGreaterThan($values, $limit, $message = '') |
159
|
|
|
* @method static void allGreaterThanEq($values, $limit, $message = '') |
160
|
|
|
* @method static void allLessThan($values, $limit, $message = '') |
161
|
|
|
* @method static void allLessThanEq($values, $limit, $message = '') |
162
|
|
|
* @method static void allRange($values, $min, $max, $message = '') |
163
|
|
|
* @method static void allOneOf($values, $values, $message = '') |
164
|
|
|
* @method static void allContains($values, $subString, $message = '') |
165
|
|
|
* @method static void allNotContains($values, $subString, $message = '') |
166
|
|
|
* @method static void allNotWhitespaceOnly($values, $message = '') |
167
|
|
|
* @method static void allStartsWith($values, $prefix, $message = '') |
168
|
|
|
* @method static void allNotStartsWith($values, $prefix, $message = '') |
169
|
|
|
* @method static void allStartsWithLetter($values, $message = '') |
170
|
|
|
* @method static void allEndsWith($values, $suffix, $message = '') |
171
|
|
|
* @method static void allNotEndsWith($values, $suffix, $message = '') |
172
|
|
|
* @method static void allRegex($values, $pattern, $message = '') |
173
|
|
|
* @method static void allNotRegex($values, $pattern, $message = '') |
174
|
|
|
* @method static void allUnicodeLetters($values, $message = '') |
175
|
|
|
* @method static void allAlpha($values, $message = '') |
176
|
|
|
* @method static void allDigits($values, $message = '') |
177
|
|
|
* @method static void allAlnum($values, $message = '') |
178
|
|
|
* @method static void allLower($values, $message = '') |
179
|
|
|
* @method static void allUpper($values, $message = '') |
180
|
|
|
* @method static void allLength($values, $length, $message = '') |
181
|
|
|
* @method static void allMinLength($values, $min, $message = '') |
182
|
|
|
* @method static void allMaxLength($values, $max, $message = '') |
183
|
|
|
* @method static void allLengthBetween($values, $min, $max, $message = '') |
184
|
|
|
* @method static void allFileExists($values, $message = '') |
185
|
|
|
* @method static void allFile($values, $message = '') |
186
|
|
|
* @method static void allDirectory($values, $message = '') |
187
|
|
|
* @method static void allReadable($values, $message = '') |
188
|
|
|
* @method static void allWritable($values, $message = '') |
189
|
|
|
* @method static void allClassExists($values, $message = '') |
190
|
|
|
* @method static void allSubclassOf($values, $class, $message = '') |
191
|
|
|
* @method static void allInterfaceExists($values, $message = '') |
192
|
|
|
* @method static void allImplementsInterface($values, $interface, $message = '') |
193
|
|
|
* @method static void allPropertyExists($values, $property, $message = '') |
194
|
|
|
* @method static void allPropertyNotExists($values, $property, $message = '') |
195
|
|
|
* @method static void allMethodExists($values, $method, $message = '') |
196
|
|
|
* @method static void allMethodNotExists($values, $method, $message = '') |
197
|
|
|
* @method static void allKeyExists($values, $key, $message = '') |
198
|
|
|
* @method static void allKeyNotExists($values, $key, $message = '') |
199
|
|
|
* @method static void allValidArrayKey($values, $message = '') |
200
|
|
|
* @method static void allCount($values, $key, $message = '') |
201
|
|
|
* @method static void allMinCount($values, $min, $message = '') |
202
|
|
|
* @method static void allMaxCount($values, $max, $message = '') |
203
|
|
|
* @method static void allCountBetween($values, $min, $max, $message = '') |
204
|
|
|
* @method static void allIsList($values, $message = '') |
205
|
|
|
* @method static void allIsNonEmptyList($values, $message = '') |
206
|
|
|
* @method static void allIsMap($values, $message = '') |
207
|
|
|
* @method static void allIsNonEmptyMap($values, $message = '') |
208
|
|
|
* @method static void allUuid($values, $message = '') |
209
|
|
|
* @method static void allThrows($expressions, $class = 'Exception', $message = '') |
210
|
|
|
* |
211
|
|
|
* @since 1.0 |
212
|
|
|
* |
213
|
|
|
* @author Bernhard Schussek <[email protected]> |
214
|
|
|
*/ |
215
|
|
|
class Assert |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
/** |
218
|
|
|
* @psalm-pure |
219
|
|
|
* @psalm-assert string $value |
220
|
|
|
* |
221
|
|
|
* @param mixed $value |
222
|
|
|
* @param string $message |
223
|
|
|
* |
224
|
|
|
* @throws InvalidArgumentException |
225
|
|
|
*/ |
226
|
241 |
|
public static function string($value, $message = '') |
227
|
|
|
{ |
228
|
241 |
|
if (!\is_string($value)) { |
229
|
45 |
|
static::reportInvalidArgument(\sprintf( |
230
|
45 |
|
$message ?: 'Expected a string. Got: %s', |
231
|
45 |
|
static::typeToString($value) |
232
|
|
|
)); |
233
|
|
|
} |
234
|
196 |
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @psalm-pure |
238
|
|
|
* @psalm-assert string $value |
239
|
|
|
* @psalm-assert !empty $value |
240
|
|
|
* |
241
|
|
|
* @param mixed $value |
242
|
|
|
* @param string $message |
243
|
|
|
* |
244
|
|
|
* @throws InvalidArgumentException |
245
|
|
|
*/ |
246
|
16 |
|
public static function stringNotEmpty($value, $message = '') |
247
|
|
|
{ |
248
|
16 |
|
static::string($value, $message); |
249
|
12 |
|
static::notEq($value, '', $message); |
250
|
8 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @psalm-pure |
254
|
|
|
* @psalm-assert int $value |
255
|
|
|
* |
256
|
|
|
* @param mixed $value |
257
|
|
|
* @param string $message |
258
|
|
|
* |
259
|
|
|
* @throws InvalidArgumentException |
260
|
|
|
*/ |
261
|
17 |
|
public static function integer($value, $message = '') |
262
|
|
|
{ |
263
|
17 |
|
if (!\is_int($value)) { |
264
|
13 |
|
static::reportInvalidArgument(\sprintf( |
265
|
13 |
|
$message ?: 'Expected an integer. Got: %s', |
266
|
13 |
|
static::typeToString($value) |
267
|
|
|
)); |
268
|
|
|
} |
269
|
4 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @psalm-pure |
273
|
|
|
* @psalm-assert numeric $value |
274
|
|
|
* |
275
|
|
|
* @param mixed $value |
276
|
|
|
* @param string $message |
277
|
|
|
* |
278
|
|
|
* @throws InvalidArgumentException |
279
|
|
|
*/ |
280
|
16 |
|
public static function integerish($value, $message = '') |
281
|
|
|
{ |
282
|
16 |
|
if (!\is_numeric($value) || $value != (int) $value) { |
283
|
4 |
|
static::reportInvalidArgument(\sprintf( |
284
|
4 |
|
$message ?: 'Expected an integerish value. Got: %s', |
285
|
4 |
|
static::typeToString($value) |
286
|
|
|
)); |
287
|
|
|
} |
288
|
12 |
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @psalm-pure |
292
|
|
|
* @psalm-assert float $value |
293
|
|
|
* |
294
|
|
|
* @param mixed $value |
295
|
|
|
* @param string $message |
296
|
|
|
* |
297
|
|
|
* @throws InvalidArgumentException |
298
|
|
|
*/ |
299
|
16 |
|
public static function float($value, $message = '') |
300
|
|
|
{ |
301
|
16 |
|
if (!\is_float($value)) { |
302
|
8 |
|
static::reportInvalidArgument(\sprintf( |
303
|
8 |
|
$message ?: 'Expected a float. Got: %s', |
304
|
8 |
|
static::typeToString($value) |
305
|
|
|
)); |
306
|
|
|
} |
307
|
8 |
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @psalm-pure |
311
|
|
|
* @psalm-assert numeric $value |
312
|
|
|
* |
313
|
|
|
* @param mixed $value |
314
|
|
|
* @param string $message |
315
|
|
|
* |
316
|
|
|
* @throws InvalidArgumentException |
317
|
|
|
*/ |
318
|
20 |
|
public static function numeric($value, $message = '') |
319
|
|
|
{ |
320
|
20 |
|
if (!\is_numeric($value)) { |
321
|
4 |
|
static::reportInvalidArgument(\sprintf( |
322
|
4 |
|
$message ?: 'Expected a numeric. Got: %s', |
323
|
4 |
|
static::typeToString($value) |
324
|
|
|
)); |
325
|
|
|
} |
326
|
16 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @psalm-pure |
330
|
|
|
* @psalm-assert int $value |
331
|
|
|
* |
332
|
|
|
* @param mixed $value |
333
|
|
|
* @param string $message |
334
|
|
|
* |
335
|
|
|
* @throws InvalidArgumentException |
336
|
|
|
*/ |
337
|
24 |
|
public static function natural($value, $message = '') |
338
|
|
|
{ |
339
|
24 |
|
if (!\is_int($value) || $value < 0) { |
340
|
16 |
|
static::reportInvalidArgument(\sprintf( |
341
|
16 |
|
$message ?: 'Expected a non-negative integer. Got %s', |
342
|
16 |
|
static::valueToString($value) |
343
|
|
|
)); |
344
|
|
|
} |
345
|
8 |
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @psalm-pure |
349
|
|
|
* @psalm-assert bool $value |
350
|
|
|
* |
351
|
|
|
* @param mixed $value |
352
|
|
|
* @param string $message |
353
|
|
|
* |
354
|
|
|
* @throws InvalidArgumentException |
355
|
|
|
*/ |
356
|
16 |
|
public static function boolean($value, $message = '') |
357
|
|
|
{ |
358
|
16 |
|
if (!\is_bool($value)) { |
359
|
8 |
|
static::reportInvalidArgument(\sprintf( |
360
|
8 |
|
$message ?: 'Expected a boolean. Got: %s', |
361
|
8 |
|
static::typeToString($value) |
362
|
|
|
)); |
363
|
|
|
} |
364
|
8 |
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @psalm-pure |
368
|
|
|
* @psalm-assert scalar $value |
369
|
|
|
* |
370
|
|
|
* @param mixed $value |
371
|
|
|
* @param string $message |
372
|
|
|
* |
373
|
|
|
* @throws InvalidArgumentException |
374
|
|
|
*/ |
375
|
23 |
|
public static function scalar($value, $message = '') |
376
|
|
|
{ |
377
|
23 |
|
if (!\is_scalar($value)) { |
378
|
11 |
|
static::reportInvalidArgument(\sprintf( |
379
|
11 |
|
$message ?: 'Expected a scalar. Got: %s', |
380
|
11 |
|
static::typeToString($value) |
381
|
|
|
)); |
382
|
|
|
} |
383
|
12 |
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @psalm-pure |
387
|
|
|
* @psalm-assert object $value |
388
|
|
|
* |
389
|
|
|
* @param mixed $value |
390
|
|
|
* @param string $message |
391
|
|
|
* |
392
|
|
|
* @throws InvalidArgumentException |
393
|
|
|
*/ |
394
|
23 |
|
public static function object($value, $message = '') |
395
|
|
|
{ |
396
|
23 |
|
if (!\is_object($value)) { |
397
|
15 |
|
static::reportInvalidArgument(\sprintf( |
398
|
15 |
|
$message ?: 'Expected an object. Got: %s', |
399
|
15 |
|
static::typeToString($value) |
400
|
|
|
)); |
401
|
|
|
} |
402
|
8 |
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @psalm-pure |
406
|
|
|
* @psalm-assert resource $value |
407
|
|
|
* |
408
|
|
|
* @param mixed $value |
409
|
|
|
* @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
410
|
|
|
* @param string $message |
411
|
|
|
* |
412
|
|
|
* @throws InvalidArgumentException |
413
|
|
|
*/ |
414
|
16 |
|
public static function resource($value, $type = null, $message = '') |
415
|
|
|
{ |
416
|
16 |
|
if (!\is_resource($value)) { |
417
|
4 |
|
static::reportInvalidArgument(\sprintf( |
418
|
4 |
|
$message ?: 'Expected a resource. Got: %s', |
419
|
4 |
|
static::typeToString($value) |
420
|
|
|
)); |
421
|
|
|
} |
422
|
|
|
|
423
|
12 |
|
if ($type && $type !== \get_resource_type($value)) { |
|
|
|
|
424
|
4 |
|
static::reportInvalidArgument(\sprintf( |
425
|
4 |
|
$message ?: 'Expected a resource of type %2$s. Got: %s', |
426
|
4 |
|
static::typeToString($value), |
427
|
4 |
|
$type |
428
|
|
|
)); |
429
|
|
|
} |
430
|
8 |
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @psalm-pure |
434
|
|
|
* @psalm-assert callable $value |
435
|
|
|
* |
436
|
|
|
* @param mixed $value |
437
|
|
|
* @param string $message |
438
|
|
|
* |
439
|
|
|
* @throws InvalidArgumentException |
440
|
|
|
*/ |
441
|
20 |
|
public static function isCallable($value, $message = '') |
442
|
|
|
{ |
443
|
20 |
|
if (!\is_callable($value)) { |
444
|
8 |
|
static::reportInvalidArgument(\sprintf( |
445
|
8 |
|
$message ?: 'Expected a callable. Got: %s', |
446
|
8 |
|
static::typeToString($value) |
447
|
|
|
)); |
448
|
|
|
} |
449
|
12 |
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @psalm-pure |
453
|
|
|
* @psalm-assert array $value |
454
|
|
|
* |
455
|
|
|
* @param mixed $value |
456
|
|
|
* @param string $message |
457
|
|
|
* |
458
|
|
|
* @throws InvalidArgumentException |
459
|
|
|
*/ |
460
|
20 |
|
public static function isArray($value, $message = '') |
461
|
|
|
{ |
462
|
20 |
|
if (!\is_array($value)) { |
463
|
12 |
|
static::reportInvalidArgument(\sprintf( |
464
|
12 |
|
$message ?: 'Expected an array. Got: %s', |
465
|
12 |
|
static::typeToString($value) |
466
|
|
|
)); |
467
|
|
|
} |
468
|
8 |
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* @psalm-pure |
472
|
|
|
* @psalm-assert iterable $value |
473
|
|
|
* |
474
|
|
|
* @deprecated use "isIterable" or "isInstanceOf" instead |
475
|
|
|
* |
476
|
|
|
* @param mixed $value |
477
|
|
|
* @param string $message |
478
|
|
|
* |
479
|
|
|
* @throws InvalidArgumentException |
480
|
|
|
*/ |
481
|
20 |
|
public static function isTraversable($value, $message = '') |
482
|
|
|
{ |
483
|
20 |
|
@\trigger_error( |
|
|
|
|
484
|
20 |
|
\sprintf( |
485
|
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.', |
486
|
20 |
|
__METHOD__ |
487
|
|
|
), |
488
|
20 |
|
\E_USER_DEPRECATED |
489
|
|
|
); |
490
|
|
|
|
491
|
20 |
|
if (!\is_array($value) && !($value instanceof Traversable)) { |
492
|
8 |
|
static::reportInvalidArgument(\sprintf( |
493
|
8 |
|
$message ?: 'Expected a traversable. Got: %s', |
494
|
8 |
|
static::typeToString($value) |
495
|
|
|
)); |
496
|
|
|
} |
497
|
12 |
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* @psalm-pure |
501
|
|
|
* @psalm-assert array|ArrayAccess $value |
502
|
|
|
* |
503
|
|
|
* @param mixed $value |
504
|
|
|
* @param string $message |
505
|
|
|
* |
506
|
|
|
* @throws InvalidArgumentException |
507
|
|
|
*/ |
508
|
20 |
View Code Duplication |
public static function isArrayAccessible($value, $message = '') |
509
|
|
|
{ |
510
|
20 |
|
if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
511
|
8 |
|
static::reportInvalidArgument(\sprintf( |
512
|
8 |
|
$message ?: 'Expected an array accessible. Got: %s', |
513
|
8 |
|
static::typeToString($value) |
514
|
|
|
)); |
515
|
|
|
} |
516
|
12 |
|
} |
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* @psalm-pure |
520
|
|
|
* @psalm-assert countable $value |
521
|
|
|
* |
522
|
|
|
* @param mixed $value |
523
|
|
|
* @param string $message |
524
|
|
|
* |
525
|
|
|
* @throws InvalidArgumentException |
526
|
|
|
*/ |
527
|
28 |
|
public static function isCountable($value, $message = '') |
528
|
|
|
{ |
529
|
|
|
if ( |
530
|
28 |
|
!\is_array($value) |
531
|
28 |
|
&& !($value instanceof Countable) |
532
|
28 |
|
&& !($value instanceof ResourceBundle) |
|
|
|
|
533
|
28 |
|
&& !($value instanceof SimpleXMLElement) |
534
|
|
|
) { |
535
|
12 |
|
static::reportInvalidArgument(\sprintf( |
536
|
12 |
|
$message ?: 'Expected a countable. Got: %s', |
537
|
12 |
|
static::typeToString($value) |
538
|
|
|
)); |
539
|
|
|
} |
540
|
16 |
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* @psalm-pure |
544
|
|
|
* @psalm-assert iterable $value |
545
|
|
|
* |
546
|
|
|
* @param mixed $value |
547
|
|
|
* @param string $message |
548
|
|
|
* |
549
|
|
|
* @throws InvalidArgumentException |
550
|
|
|
*/ |
551
|
1040 |
View Code Duplication |
public static function isIterable($value, $message = '') |
552
|
|
|
{ |
553
|
1040 |
|
if (!\is_array($value) && !($value instanceof Traversable)) { |
554
|
8 |
|
static::reportInvalidArgument(\sprintf( |
555
|
8 |
|
$message ?: 'Expected an iterable. Got: %s', |
556
|
8 |
|
static::typeToString($value) |
557
|
|
|
)); |
558
|
|
|
} |
559
|
1036 |
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* @psalm-pure |
563
|
|
|
* @psalm-template ExpectedType of object |
564
|
|
|
* @psalm-param class-string<ExpectedType> $class |
565
|
|
|
* @psalm-assert ExpectedType $value |
566
|
|
|
* |
567
|
|
|
* @param mixed $value |
568
|
|
|
* @param string|object $class |
569
|
|
|
* @param string $message |
570
|
|
|
* |
571
|
|
|
* @throws InvalidArgumentException |
572
|
|
|
*/ |
573
|
19 |
|
public static function isInstanceOf($value, $class, $message = '') |
574
|
|
|
{ |
575
|
19 |
|
if (!($value instanceof $class)) { |
576
|
15 |
|
static::reportInvalidArgument(\sprintf( |
577
|
15 |
|
$message ?: 'Expected an instance of %2$s. Got: %s', |
578
|
15 |
|
static::typeToString($value), |
579
|
15 |
|
$class |
580
|
|
|
)); |
581
|
|
|
} |
582
|
4 |
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* @psalm-pure |
586
|
|
|
* @psalm-template ExpectedType of object |
587
|
|
|
* @psalm-param class-string<ExpectedType> $class |
588
|
|
|
* @psalm-assert !ExpectedType $value |
589
|
|
|
* |
590
|
|
|
* @param mixed $value |
591
|
|
|
* @param string|object $class |
592
|
|
|
* @param string $message |
593
|
|
|
* |
594
|
|
|
* @throws InvalidArgumentException |
595
|
|
|
*/ |
596
|
16 |
|
public static function notInstanceOf($value, $class, $message = '') |
597
|
|
|
{ |
598
|
16 |
|
if ($value instanceof $class) { |
599
|
4 |
|
static::reportInvalidArgument(\sprintf( |
600
|
4 |
|
$message ?: 'Expected an instance other than %2$s. Got: %s', |
601
|
4 |
|
static::typeToString($value), |
602
|
4 |
|
$class |
603
|
|
|
)); |
604
|
|
|
} |
605
|
12 |
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* @psalm-pure |
609
|
|
|
* @psalm-param array<class-string> $classes |
610
|
|
|
* |
611
|
|
|
* @param mixed $value |
612
|
|
|
* @param array<object|string> $classes |
613
|
|
|
* @param string $message |
614
|
|
|
* |
615
|
|
|
* @throws InvalidArgumentException |
616
|
|
|
*/ |
617
|
20 |
|
public static function isInstanceOfAny($value, array $classes, $message = '') |
618
|
|
|
{ |
619
|
20 |
|
foreach ($classes as $class) { |
620
|
20 |
|
if ($value instanceof $class) { |
621
|
8 |
|
return; |
622
|
|
|
} |
623
|
|
|
} |
624
|
|
|
|
625
|
12 |
|
static::reportInvalidArgument(\sprintf( |
626
|
12 |
|
$message ?: 'Expected an instance of any of %2$s. Got: %s', |
627
|
12 |
|
static::typeToString($value), |
628
|
12 |
|
\implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
629
|
|
|
)); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* @psalm-pure |
634
|
|
|
* @psalm-template ExpectedType of object |
635
|
|
|
* @psalm-param class-string<ExpectedType> $class |
636
|
|
|
* @psalm-assert ExpectedType|class-string<ExpectedType> $value |
637
|
|
|
* |
638
|
|
|
* @param object|string $value |
639
|
|
|
* @param string $class |
640
|
|
|
* @param string $message |
641
|
|
|
* |
642
|
|
|
* @throws InvalidArgumentException |
643
|
|
|
*/ |
644
|
20 |
View Code Duplication |
public static function isAOf($value, $class, $message = '') |
645
|
|
|
{ |
646
|
20 |
|
static::string($class, 'Expected class as a string. Got: %s'); |
647
|
|
|
|
648
|
16 |
|
if (!\is_a($value, $class, \is_string($value))) { |
649
|
12 |
|
static::reportInvalidArgument(sprintf( |
650
|
12 |
|
$message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', |
651
|
12 |
|
static::typeToString($value), |
652
|
|
|
$class |
653
|
|
|
)); |
654
|
|
|
} |
655
|
4 |
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* @psalm-pure |
659
|
|
|
* @psalm-template UnexpectedType of object |
660
|
|
|
* @psalm-param class-string<UnexpectedType> $class |
661
|
|
|
* @psalm-assert !UnexpectedType $value |
662
|
|
|
* @psalm-assert !class-string<UnexpectedType> $value |
663
|
|
|
* |
664
|
|
|
* @param object|string $value |
665
|
|
|
* @param string $class |
666
|
|
|
* @param string $message |
667
|
|
|
* |
668
|
|
|
* @throws InvalidArgumentException |
669
|
|
|
*/ |
670
|
20 |
View Code Duplication |
public static function isNotA($value, $class, $message = '') |
671
|
|
|
{ |
672
|
20 |
|
static::string($class, 'Expected class as a string. Got: %s'); |
673
|
|
|
|
674
|
16 |
|
if (\is_a($value, $class, \is_string($value))) { |
675
|
4 |
|
static::reportInvalidArgument(sprintf( |
676
|
4 |
|
$message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', |
677
|
4 |
|
static::typeToString($value), |
678
|
|
|
$class |
679
|
|
|
)); |
680
|
|
|
} |
681
|
12 |
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* @psalm-pure |
685
|
|
|
* @psalm-param array<class-string> $classes |
686
|
|
|
* |
687
|
|
|
* @param object|string $value |
688
|
|
|
* @param string[] $classes |
689
|
|
|
* @param string $message |
690
|
|
|
* |
691
|
|
|
* @throws InvalidArgumentException |
692
|
|
|
*/ |
693
|
24 |
|
public static function isAnyOf($value, array $classes, $message = '') |
694
|
|
|
{ |
695
|
24 |
|
foreach ($classes as $class) { |
696
|
24 |
|
static::string($class, 'Expected class as a string. Got: %s'); |
697
|
|
|
|
698
|
20 |
|
if (\is_a($value, $class, \is_string($value))) { |
699
|
8 |
|
return; |
700
|
|
|
} |
701
|
|
|
} |
702
|
|
|
|
703
|
12 |
|
static::reportInvalidArgument(sprintf( |
704
|
12 |
|
$message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', |
705
|
12 |
|
static::typeToString($value), |
706
|
12 |
|
\implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
707
|
|
|
)); |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* @psalm-pure |
712
|
|
|
* @psalm-assert empty $value |
713
|
|
|
* |
714
|
|
|
* @param mixed $value |
715
|
|
|
* @param string $message |
716
|
|
|
* |
717
|
|
|
* @throws InvalidArgumentException |
718
|
|
|
*/ |
719
|
23 |
|
public static function isEmpty($value, $message = '') |
720
|
|
|
{ |
721
|
23 |
|
if (!empty($value)) { |
722
|
8 |
|
static::reportInvalidArgument(\sprintf( |
723
|
8 |
|
$message ?: 'Expected an empty value. Got: %s', |
724
|
8 |
|
static::valueToString($value) |
725
|
|
|
)); |
726
|
|
|
} |
727
|
15 |
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* @psalm-pure |
731
|
|
|
* @psalm-assert !empty $value |
732
|
|
|
* |
733
|
|
|
* @param mixed $value |
734
|
|
|
* @param string $message |
735
|
|
|
* |
736
|
|
|
* @throws InvalidArgumentException |
737
|
|
|
*/ |
738
|
55 |
|
public static function notEmpty($value, $message = '') |
739
|
|
|
{ |
740
|
55 |
|
if (empty($value)) { |
741
|
23 |
|
static::reportInvalidArgument(\sprintf( |
742
|
23 |
|
$message ?: 'Expected a non-empty value. Got: %s', |
743
|
23 |
|
static::valueToString($value) |
744
|
|
|
)); |
745
|
|
|
} |
746
|
32 |
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* @psalm-pure |
750
|
|
|
* @psalm-assert null $value |
751
|
|
|
* |
752
|
|
|
* @param mixed $value |
753
|
|
|
* @param string $message |
754
|
|
|
* |
755
|
|
|
* @throws InvalidArgumentException |
756
|
|
|
*/ |
757
|
11 |
|
public static function null($value, $message = '') |
758
|
|
|
{ |
759
|
11 |
|
if (null !== $value) { |
760
|
8 |
|
static::reportInvalidArgument(\sprintf( |
761
|
8 |
|
$message ?: 'Expected null. Got: %s', |
762
|
8 |
|
static::valueToString($value) |
763
|
|
|
)); |
764
|
|
|
} |
765
|
3 |
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* @psalm-pure |
769
|
|
|
* @psalm-assert !null $value |
770
|
|
|
* |
771
|
|
|
* @param mixed $value |
772
|
|
|
* @param string $message |
773
|
|
|
* |
774
|
|
|
* @throws InvalidArgumentException |
775
|
|
|
*/ |
776
|
11 |
|
public static function notNull($value, $message = '') |
777
|
|
|
{ |
778
|
11 |
|
if (null === $value) { |
779
|
3 |
|
static::reportInvalidArgument( |
780
|
3 |
|
$message ?: 'Expected a value other than null.' |
781
|
|
|
); |
782
|
|
|
} |
783
|
8 |
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* @psalm-pure |
787
|
|
|
* @psalm-assert true $value |
788
|
|
|
* |
789
|
|
|
* @param mixed $value |
790
|
|
|
* @param string $message |
791
|
|
|
* |
792
|
|
|
* @throws InvalidArgumentException |
793
|
|
|
*/ |
794
|
15 |
|
public static function true($value, $message = '') |
795
|
|
|
{ |
796
|
15 |
|
if (true !== $value) { |
797
|
11 |
|
static::reportInvalidArgument(\sprintf( |
798
|
11 |
|
$message ?: 'Expected a value to be true. Got: %s', |
799
|
11 |
|
static::valueToString($value) |
800
|
|
|
)); |
801
|
|
|
} |
802
|
4 |
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* @psalm-pure |
806
|
|
|
* @psalm-assert false $value |
807
|
|
|
* |
808
|
|
|
* @param mixed $value |
809
|
|
|
* @param string $message |
810
|
|
|
* |
811
|
|
|
* @throws InvalidArgumentException |
812
|
|
|
*/ |
813
|
19 |
|
public static function false($value, $message = '') |
814
|
|
|
{ |
815
|
19 |
|
if (false !== $value) { |
816
|
15 |
|
static::reportInvalidArgument(\sprintf( |
817
|
15 |
|
$message ?: 'Expected a value to be false. Got: %s', |
818
|
15 |
|
static::valueToString($value) |
819
|
|
|
)); |
820
|
|
|
} |
821
|
4 |
|
} |
822
|
|
|
|
823
|
|
|
/** |
824
|
|
|
* @psalm-assert !false $value |
825
|
|
|
* |
826
|
|
|
* @param mixed $value |
827
|
|
|
* @param string $message |
828
|
|
|
* |
829
|
|
|
* @throws InvalidArgumentException |
830
|
|
|
*/ |
831
|
19 |
|
public static function notFalse($value, $message = '') |
832
|
|
|
{ |
833
|
19 |
|
if (false === $value) { |
834
|
4 |
|
static::reportInvalidArgument( |
835
|
4 |
|
$message ?: 'Expected a value other than false.' |
836
|
|
|
); |
837
|
|
|
} |
838
|
15 |
|
} |
839
|
|
|
|
840
|
|
|
/** |
841
|
|
|
* @param mixed $value |
842
|
|
|
* @param string $message |
843
|
|
|
* |
844
|
|
|
* @throws InvalidArgumentException |
845
|
|
|
*/ |
846
|
51 |
View Code Duplication |
public static function ip($value, $message = '') |
847
|
|
|
{ |
848
|
51 |
|
if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
849
|
19 |
|
static::reportInvalidArgument(\sprintf( |
850
|
19 |
|
$message ?: 'Expected a value to be an IP. Got: %s', |
851
|
19 |
|
static::valueToString($value) |
852
|
|
|
)); |
853
|
|
|
} |
854
|
32 |
|
} |
855
|
|
|
|
856
|
|
|
/** |
857
|
|
|
* @param mixed $value |
858
|
|
|
* @param string $message |
859
|
|
|
* |
860
|
|
|
* @throws InvalidArgumentException |
861
|
|
|
*/ |
862
|
51 |
View Code Duplication |
public static function ipv4($value, $message = '') |
863
|
|
|
{ |
864
|
51 |
|
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
865
|
35 |
|
static::reportInvalidArgument(\sprintf( |
866
|
35 |
|
$message ?: 'Expected a value to be an IPv4. Got: %s', |
867
|
35 |
|
static::valueToString($value) |
868
|
|
|
)); |
869
|
|
|
} |
870
|
16 |
|
} |
871
|
|
|
|
872
|
|
|
/** |
873
|
|
|
* @param mixed $value |
874
|
|
|
* @param string $message |
875
|
|
|
* |
876
|
|
|
* @throws InvalidArgumentException |
877
|
|
|
*/ |
878
|
51 |
View Code Duplication |
public static function ipv6($value, $message = '') |
879
|
|
|
{ |
880
|
51 |
|
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
881
|
31 |
|
static::reportInvalidArgument(\sprintf( |
882
|
31 |
|
$message ?: 'Expected a value to be an IPv6. Got %s', |
883
|
31 |
|
static::valueToString($value) |
884
|
|
|
)); |
885
|
|
|
} |
886
|
20 |
|
} |
887
|
|
|
|
888
|
|
|
/** |
889
|
|
|
* @param mixed $value |
890
|
|
|
* @param string $message |
891
|
|
|
* |
892
|
|
|
* @throws InvalidArgumentException |
893
|
|
|
*/ |
894
|
20 |
View Code Duplication |
public static function email($value, $message = '') |
895
|
|
|
{ |
896
|
20 |
|
if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
897
|
12 |
|
static::reportInvalidArgument(\sprintf( |
898
|
12 |
|
$message ?: 'Expected a value to be a valid e-mail address. Got %s', |
899
|
12 |
|
static::valueToString($value) |
900
|
|
|
)); |
901
|
|
|
} |
902
|
8 |
|
} |
903
|
|
|
|
904
|
|
|
/** |
905
|
|
|
* Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
906
|
|
|
* |
907
|
|
|
* @param array $values |
908
|
|
|
* @param string $message |
909
|
|
|
* |
910
|
|
|
* @throws InvalidArgumentException |
911
|
|
|
*/ |
912
|
12 |
|
public static function uniqueValues(array $values, $message = '') |
913
|
|
|
{ |
914
|
12 |
|
$allValues = \count($values); |
915
|
12 |
|
$uniqueValues = \count(\array_unique($values)); |
916
|
|
|
|
917
|
12 |
|
if ($allValues !== $uniqueValues) { |
918
|
8 |
|
$difference = $allValues - $uniqueValues; |
919
|
|
|
|
920
|
8 |
|
static::reportInvalidArgument(\sprintf( |
921
|
8 |
|
$message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
922
|
8 |
|
$difference, |
923
|
8 |
|
(1 === $difference ? 'is' : 'are') |
924
|
|
|
)); |
925
|
|
|
} |
926
|
4 |
|
} |
927
|
|
|
|
928
|
|
|
/** |
929
|
|
|
* @param mixed $value |
930
|
|
|
* @param mixed $expect |
931
|
|
|
* @param string $message |
932
|
|
|
* |
933
|
|
|
* @throws InvalidArgumentException |
934
|
|
|
*/ |
935
|
33 |
|
public static function eq($value, $expect, $message = '') |
936
|
|
|
{ |
937
|
33 |
|
if ($expect != $value) { |
938
|
17 |
|
static::reportInvalidArgument(\sprintf( |
939
|
17 |
|
$message ?: 'Expected a value equal to %2$s. Got: %s', |
940
|
17 |
|
static::valueToString($value), |
941
|
17 |
|
static::valueToString($expect) |
942
|
|
|
)); |
943
|
|
|
} |
944
|
16 |
|
} |
945
|
|
|
|
946
|
|
|
/** |
947
|
|
|
* @param mixed $value |
948
|
|
|
* @param mixed $expect |
949
|
|
|
* @param string $message |
950
|
|
|
* |
951
|
|
|
* @throws InvalidArgumentException |
952
|
|
|
*/ |
953
|
28 |
|
public static function notEq($value, $expect, $message = '') |
954
|
|
|
{ |
955
|
28 |
|
if ($expect == $value) { |
956
|
16 |
|
static::reportInvalidArgument(\sprintf( |
957
|
16 |
|
$message ?: 'Expected a different value than %s.', |
958
|
16 |
|
static::valueToString($expect) |
959
|
|
|
)); |
960
|
|
|
} |
961
|
12 |
|
} |
962
|
|
|
|
963
|
|
|
/** |
964
|
|
|
* @psalm-pure |
965
|
|
|
* |
966
|
|
|
* @param mixed $value |
967
|
|
|
* @param mixed $expect |
968
|
|
|
* @param string $message |
969
|
|
|
* |
970
|
|
|
* @throws InvalidArgumentException |
971
|
|
|
*/ |
972
|
16 |
|
public static function same($value, $expect, $message = '') |
973
|
|
|
{ |
974
|
16 |
|
if ($expect !== $value) { |
975
|
12 |
|
static::reportInvalidArgument(\sprintf( |
976
|
12 |
|
$message ?: 'Expected a value identical to %2$s. Got: %s', |
977
|
12 |
|
static::valueToString($value), |
978
|
12 |
|
static::valueToString($expect) |
979
|
|
|
)); |
980
|
|
|
} |
981
|
4 |
|
} |
982
|
|
|
|
983
|
|
|
/** |
984
|
|
|
* @psalm-pure |
985
|
|
|
* |
986
|
|
|
* @param mixed $value |
987
|
|
|
* @param mixed $expect |
988
|
|
|
* @param string $message |
989
|
|
|
* |
990
|
|
|
* @throws InvalidArgumentException |
991
|
|
|
*/ |
992
|
16 |
|
public static function notSame($value, $expect, $message = '') |
993
|
|
|
{ |
994
|
16 |
|
if ($expect === $value) { |
995
|
4 |
|
static::reportInvalidArgument(\sprintf( |
996
|
4 |
|
$message ?: 'Expected a value not identical to %s.', |
997
|
4 |
|
static::valueToString($expect) |
998
|
|
|
)); |
999
|
|
|
} |
1000
|
12 |
|
} |
1001
|
|
|
|
1002
|
|
|
/** |
1003
|
|
|
* @psalm-pure |
1004
|
|
|
* |
1005
|
|
|
* @param mixed $value |
1006
|
|
|
* @param mixed $limit |
1007
|
|
|
* @param string $message |
1008
|
|
|
* |
1009
|
|
|
* @throws InvalidArgumentException |
1010
|
|
|
*/ |
1011
|
8 |
|
public static function greaterThan($value, $limit, $message = '') |
1012
|
|
|
{ |
1013
|
8 |
|
if ($value <= $limit) { |
1014
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1015
|
4 |
|
$message ?: 'Expected a value greater than %2$s. Got: %s', |
1016
|
4 |
|
static::valueToString($value), |
1017
|
4 |
|
static::valueToString($limit) |
1018
|
|
|
)); |
1019
|
|
|
} |
1020
|
4 |
|
} |
1021
|
|
|
|
1022
|
|
|
/** |
1023
|
|
|
* @psalm-pure |
1024
|
|
|
* |
1025
|
|
|
* @param mixed $value |
1026
|
|
|
* @param mixed $limit |
1027
|
|
|
* @param string $message |
1028
|
|
|
* |
1029
|
|
|
* @throws InvalidArgumentException |
1030
|
|
|
*/ |
1031
|
12 |
|
public static function greaterThanEq($value, $limit, $message = '') |
1032
|
|
|
{ |
1033
|
12 |
|
if ($value < $limit) { |
1034
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1035
|
4 |
|
$message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
1036
|
4 |
|
static::valueToString($value), |
1037
|
4 |
|
static::valueToString($limit) |
1038
|
|
|
)); |
1039
|
|
|
} |
1040
|
8 |
|
} |
1041
|
|
|
|
1042
|
|
|
/** |
1043
|
|
|
* @psalm-pure |
1044
|
|
|
* |
1045
|
|
|
* @param mixed $value |
1046
|
|
|
* @param mixed $limit |
1047
|
|
|
* @param string $message |
1048
|
|
|
* |
1049
|
|
|
* @throws InvalidArgumentException |
1050
|
|
|
*/ |
1051
|
9 |
|
public static function lessThan($value, $limit, $message = '') |
1052
|
|
|
{ |
1053
|
9 |
|
if ($value >= $limit) { |
1054
|
5 |
|
static::reportInvalidArgument(\sprintf( |
1055
|
5 |
|
$message ?: 'Expected a value less than %2$s. Got: %s', |
1056
|
5 |
|
static::valueToString($value), |
1057
|
5 |
|
static::valueToString($limit) |
1058
|
|
|
)); |
1059
|
|
|
} |
1060
|
4 |
|
} |
1061
|
|
|
|
1062
|
|
|
/** |
1063
|
|
|
* @psalm-pure |
1064
|
|
|
* |
1065
|
|
|
* @param mixed $value |
1066
|
|
|
* @param mixed $limit |
1067
|
|
|
* @param string $message |
1068
|
|
|
* |
1069
|
|
|
* @throws InvalidArgumentException |
1070
|
|
|
*/ |
1071
|
12 |
|
public static function lessThanEq($value, $limit, $message = '') |
1072
|
|
|
{ |
1073
|
12 |
|
if ($value > $limit) { |
1074
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1075
|
4 |
|
$message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
1076
|
4 |
|
static::valueToString($value), |
1077
|
4 |
|
static::valueToString($limit) |
1078
|
|
|
)); |
1079
|
|
|
} |
1080
|
8 |
|
} |
1081
|
|
|
|
1082
|
|
|
/** |
1083
|
|
|
* Inclusive range, so Assert::(3, 3, 5) passes. |
1084
|
|
|
* |
1085
|
|
|
* @psalm-pure |
1086
|
|
|
* |
1087
|
|
|
* @param mixed $value |
1088
|
|
|
* @param mixed $min |
1089
|
|
|
* @param mixed $max |
1090
|
|
|
* @param string $message |
1091
|
|
|
* |
1092
|
|
|
* @throws InvalidArgumentException |
1093
|
|
|
*/ |
1094
|
16 |
View Code Duplication |
public static function range($value, $min, $max, $message = '') |
1095
|
|
|
{ |
1096
|
16 |
|
if ($value < $min || $value > $max) { |
1097
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1098
|
8 |
|
$message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
1099
|
8 |
|
static::valueToString($value), |
1100
|
8 |
|
static::valueToString($min), |
1101
|
8 |
|
static::valueToString($max) |
1102
|
|
|
)); |
1103
|
|
|
} |
1104
|
8 |
|
} |
1105
|
|
|
|
1106
|
|
|
/** |
1107
|
|
|
* Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
1108
|
|
|
* |
1109
|
|
|
* @psalm-pure |
1110
|
|
|
* |
1111
|
|
|
* @param mixed $value |
1112
|
|
|
* @param array $values |
1113
|
|
|
* @param string $message |
1114
|
|
|
* |
1115
|
|
|
* @throws InvalidArgumentException |
1116
|
|
|
*/ |
1117
|
8 |
|
public static function oneOf($value, array $values, $message = '') |
1118
|
|
|
{ |
1119
|
8 |
|
if (!\in_array($value, $values, true)) { |
1120
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1121
|
4 |
|
$message ?: 'Expected one of: %2$s. Got: %s', |
1122
|
4 |
|
static::valueToString($value), |
1123
|
4 |
|
\implode(', ', \array_map(array('static', 'valueToString'), $values)) |
1124
|
|
|
)); |
1125
|
|
|
} |
1126
|
4 |
|
} |
1127
|
|
|
|
1128
|
|
|
/** |
1129
|
|
|
* @psalm-pure |
1130
|
|
|
* |
1131
|
|
|
* @param string $value |
1132
|
|
|
* @param string $subString |
1133
|
|
|
* @param string $message |
1134
|
|
|
* |
1135
|
|
|
* @throws InvalidArgumentException |
1136
|
|
|
*/ |
1137
|
80 |
View Code Duplication |
public static function contains($value, $subString, $message = '') |
1138
|
|
|
{ |
1139
|
80 |
|
if (false === \strpos($value, $subString)) { |
1140
|
32 |
|
static::reportInvalidArgument(\sprintf( |
1141
|
32 |
|
$message ?: 'Expected a value to contain %2$s. Got: %s', |
1142
|
32 |
|
static::valueToString($value), |
1143
|
32 |
|
static::valueToString($subString) |
1144
|
|
|
)); |
1145
|
|
|
} |
1146
|
48 |
|
} |
1147
|
|
|
|
1148
|
|
|
/** |
1149
|
|
|
* @psalm-pure |
1150
|
|
|
* |
1151
|
|
|
* @param string $value |
1152
|
|
|
* @param string $subString |
1153
|
|
|
* @param string $message |
1154
|
|
|
* |
1155
|
|
|
* @throws InvalidArgumentException |
1156
|
|
|
*/ |
1157
|
80 |
View Code Duplication |
public static function notContains($value, $subString, $message = '') |
1158
|
|
|
{ |
1159
|
80 |
|
if (false !== \strpos($value, $subString)) { |
1160
|
48 |
|
static::reportInvalidArgument(\sprintf( |
1161
|
48 |
|
$message ?: '%2$s was not expected to be contained in a value. Got: %s', |
1162
|
48 |
|
static::valueToString($value), |
1163
|
48 |
|
static::valueToString($subString) |
1164
|
|
|
)); |
1165
|
|
|
} |
1166
|
32 |
|
} |
1167
|
|
|
|
1168
|
|
|
/** |
1169
|
|
|
* @psalm-pure |
1170
|
|
|
* |
1171
|
|
|
* @param string $value |
1172
|
|
|
* @param string $message |
1173
|
|
|
* |
1174
|
|
|
* @throws InvalidArgumentException |
1175
|
|
|
*/ |
1176
|
40 |
|
public static function notWhitespaceOnly($value, $message = '') |
1177
|
|
|
{ |
1178
|
40 |
|
if (\preg_match('/^\s*$/', $value)) { |
1179
|
24 |
|
static::reportInvalidArgument(\sprintf( |
1180
|
24 |
|
$message ?: 'Expected a non-whitespace string. Got: %s', |
1181
|
24 |
|
static::valueToString($value) |
1182
|
|
|
)); |
1183
|
|
|
} |
1184
|
16 |
|
} |
1185
|
|
|
|
1186
|
|
|
/** |
1187
|
|
|
* @psalm-pure |
1188
|
|
|
* |
1189
|
|
|
* @param string $value |
1190
|
|
|
* @param string $prefix |
1191
|
|
|
* @param string $message |
1192
|
|
|
* |
1193
|
|
|
* @throws InvalidArgumentException |
1194
|
|
|
*/ |
1195
|
48 |
View Code Duplication |
public static function startsWith($value, $prefix, $message = '') |
1196
|
|
|
{ |
1197
|
48 |
|
if (0 !== \strpos($value, $prefix)) { |
1198
|
32 |
|
static::reportInvalidArgument(\sprintf( |
1199
|
32 |
|
$message ?: 'Expected a value to start with %2$s. Got: %s', |
1200
|
32 |
|
static::valueToString($value), |
1201
|
32 |
|
static::valueToString($prefix) |
1202
|
|
|
)); |
1203
|
|
|
} |
1204
|
16 |
|
} |
1205
|
|
|
|
1206
|
|
|
/** |
1207
|
|
|
* @psalm-pure |
1208
|
|
|
* |
1209
|
|
|
* @param string $value |
1210
|
|
|
* @param string $prefix |
1211
|
|
|
* @param string $message |
1212
|
|
|
* |
1213
|
|
|
* @throws InvalidArgumentException |
1214
|
|
|
*/ |
1215
|
48 |
View Code Duplication |
public static function notStartsWith($value, $prefix, $message = '') |
1216
|
|
|
{ |
1217
|
48 |
|
if (0 === \strpos($value, $prefix)) { |
1218
|
16 |
|
static::reportInvalidArgument(\sprintf( |
1219
|
16 |
|
$message ?: 'Expected a value not to start with %2$s. Got: %s', |
1220
|
16 |
|
static::valueToString($value), |
1221
|
16 |
|
static::valueToString($prefix) |
1222
|
|
|
)); |
1223
|
|
|
} |
1224
|
32 |
|
} |
1225
|
|
|
|
1226
|
|
|
/** |
1227
|
|
|
* @psalm-pure |
1228
|
|
|
* |
1229
|
|
|
* @param mixed $value |
1230
|
|
|
* @param string $message |
1231
|
|
|
* |
1232
|
|
|
* @throws InvalidArgumentException |
1233
|
|
|
*/ |
1234
|
35 |
|
public static function startsWithLetter($value, $message = '') |
1235
|
|
|
{ |
1236
|
35 |
|
static::string($value); |
1237
|
|
|
|
1238
|
24 |
|
$valid = isset($value[0]); |
1239
|
|
|
|
1240
|
24 |
|
if ($valid) { |
1241
|
20 |
|
$locale = \setlocale(LC_CTYPE, 0); |
1242
|
20 |
|
\setlocale(LC_CTYPE, 'C'); |
1243
|
20 |
|
$valid = \ctype_alpha($value[0]); |
1244
|
20 |
|
\setlocale(LC_CTYPE, $locale); |
1245
|
|
|
} |
1246
|
|
|
|
1247
|
24 |
|
if (!$valid) { |
1248
|
12 |
|
static::reportInvalidArgument(\sprintf( |
1249
|
12 |
|
$message ?: 'Expected a value to start with a letter. Got: %s', |
1250
|
12 |
|
static::valueToString($value) |
1251
|
|
|
)); |
1252
|
|
|
} |
1253
|
12 |
|
} |
1254
|
|
|
|
1255
|
|
|
/** |
1256
|
|
|
* @psalm-pure |
1257
|
|
|
* |
1258
|
|
|
* @param string $value |
1259
|
|
|
* @param string $suffix |
1260
|
|
|
* @param string $message |
1261
|
|
|
* |
1262
|
|
|
* @throws InvalidArgumentException |
1263
|
|
|
*/ |
1264
|
48 |
View Code Duplication |
public static function endsWith($value, $suffix, $message = '') |
1265
|
|
|
{ |
1266
|
48 |
|
if ($suffix !== \substr($value, -\strlen($suffix))) { |
1267
|
32 |
|
static::reportInvalidArgument(\sprintf( |
1268
|
32 |
|
$message ?: 'Expected a value to end with %2$s. Got: %s', |
1269
|
32 |
|
static::valueToString($value), |
1270
|
32 |
|
static::valueToString($suffix) |
1271
|
|
|
)); |
1272
|
|
|
} |
1273
|
16 |
|
} |
1274
|
|
|
|
1275
|
|
|
/** |
1276
|
|
|
* @psalm-pure |
1277
|
|
|
* |
1278
|
|
|
* @param string $value |
1279
|
|
|
* @param string $suffix |
1280
|
|
|
* @param string $message |
1281
|
|
|
* |
1282
|
|
|
* @throws InvalidArgumentException |
1283
|
|
|
*/ |
1284
|
48 |
View Code Duplication |
public static function notEndsWith($value, $suffix, $message = '') |
1285
|
|
|
{ |
1286
|
48 |
|
if ($suffix === \substr($value, -\strlen($suffix))) { |
1287
|
16 |
|
static::reportInvalidArgument(\sprintf( |
1288
|
16 |
|
$message ?: 'Expected a value not to end with %2$s. Got: %s', |
1289
|
16 |
|
static::valueToString($value), |
1290
|
16 |
|
static::valueToString($suffix) |
1291
|
|
|
)); |
1292
|
|
|
} |
1293
|
32 |
|
} |
1294
|
|
|
|
1295
|
|
|
/** |
1296
|
|
|
* @psalm-pure |
1297
|
|
|
* |
1298
|
|
|
* @param string $value |
1299
|
|
|
* @param string $pattern |
1300
|
|
|
* @param string $message |
1301
|
|
|
* |
1302
|
|
|
* @throws InvalidArgumentException |
1303
|
|
|
*/ |
1304
|
12 |
|
public static function regex($value, $pattern, $message = '') |
1305
|
|
|
{ |
1306
|
12 |
|
if (!\preg_match($pattern, $value)) { |
1307
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1308
|
8 |
|
$message ?: 'The value %s does not match the expected pattern.', |
1309
|
8 |
|
static::valueToString($value) |
1310
|
|
|
)); |
1311
|
|
|
} |
1312
|
4 |
|
} |
1313
|
|
|
|
1314
|
|
|
/** |
1315
|
|
|
* @psalm-pure |
1316
|
|
|
* |
1317
|
|
|
* @param string $value |
1318
|
|
|
* @param string $pattern |
1319
|
|
|
* @param string $message |
1320
|
|
|
* |
1321
|
|
|
* @throws InvalidArgumentException |
1322
|
|
|
*/ |
1323
|
12 |
|
public static function notRegex($value, $pattern, $message = '') |
1324
|
|
|
{ |
1325
|
12 |
|
if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
1326
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1327
|
4 |
|
$message ?: 'The value %s matches the pattern %s (at offset %d).', |
1328
|
4 |
|
static::valueToString($value), |
1329
|
4 |
|
static::valueToString($pattern), |
1330
|
4 |
|
$matches[0][1] |
1331
|
|
|
)); |
1332
|
|
|
} |
1333
|
8 |
|
} |
1334
|
|
|
|
1335
|
|
|
/** |
1336
|
|
|
* @psalm-pure |
1337
|
|
|
* |
1338
|
|
|
* @param mixed $value |
1339
|
|
|
* @param string $message |
1340
|
|
|
* |
1341
|
|
|
* @throws InvalidArgumentException |
1342
|
|
|
*/ |
1343
|
28 |
View Code Duplication |
public static function unicodeLetters($value, $message = '') |
1344
|
|
|
{ |
1345
|
28 |
|
static::string($value); |
1346
|
|
|
|
1347
|
28 |
|
if (!\preg_match('/^\p{L}+$/u', $value)) { |
1348
|
16 |
|
static::reportInvalidArgument(\sprintf( |
1349
|
16 |
|
$message ?: 'Expected a value to contain only Unicode letters. Got: %s', |
1350
|
16 |
|
static::valueToString($value) |
1351
|
|
|
)); |
1352
|
|
|
} |
1353
|
12 |
|
} |
1354
|
|
|
|
1355
|
|
|
/** |
1356
|
|
|
* @psalm-pure |
1357
|
|
|
* |
1358
|
|
|
* @param mixed $value |
1359
|
|
|
* @param string $message |
1360
|
|
|
* |
1361
|
|
|
* @throws InvalidArgumentException |
1362
|
|
|
*/ |
1363
|
20 |
View Code Duplication |
public static function alpha($value, $message = '') |
1364
|
|
|
{ |
1365
|
20 |
|
static::string($value); |
1366
|
|
|
|
1367
|
12 |
|
$locale = \setlocale(LC_CTYPE, 0); |
1368
|
12 |
|
\setlocale(LC_CTYPE, 'C'); |
1369
|
12 |
|
$valid = !\ctype_alpha($value); |
1370
|
12 |
|
\setlocale(LC_CTYPE, $locale); |
1371
|
|
|
|
1372
|
12 |
|
if ($valid) { |
1373
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1374
|
8 |
|
$message ?: 'Expected a value to contain only letters. Got: %s', |
1375
|
8 |
|
static::valueToString($value) |
1376
|
|
|
)); |
1377
|
|
|
} |
1378
|
4 |
|
} |
1379
|
|
|
|
1380
|
|
|
/** |
1381
|
|
|
* @psalm-pure |
1382
|
|
|
* |
1383
|
|
|
* @param string $value |
1384
|
|
|
* @param string $message |
1385
|
|
|
* |
1386
|
|
|
* @throws InvalidArgumentException |
1387
|
|
|
*/ |
1388
|
12 |
View Code Duplication |
public static function digits($value, $message = '') |
1389
|
|
|
{ |
1390
|
12 |
|
$locale = \setlocale(LC_CTYPE, 0); |
1391
|
12 |
|
\setlocale(LC_CTYPE, 'C'); |
1392
|
12 |
|
$valid = !\ctype_digit($value); |
1393
|
12 |
|
\setlocale(LC_CTYPE, $locale); |
1394
|
|
|
|
1395
|
12 |
|
if ($valid) { |
1396
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1397
|
8 |
|
$message ?: 'Expected a value to contain digits only. Got: %s', |
1398
|
8 |
|
static::valueToString($value) |
1399
|
|
|
)); |
1400
|
|
|
} |
1401
|
4 |
|
} |
1402
|
|
|
|
1403
|
|
|
/** |
1404
|
|
|
* @psalm-pure |
1405
|
|
|
* |
1406
|
|
|
* @param string $value |
1407
|
|
|
* @param string $message |
1408
|
|
|
* |
1409
|
|
|
* @throws InvalidArgumentException |
1410
|
|
|
*/ |
1411
|
12 |
View Code Duplication |
public static function alnum($value, $message = '') |
1412
|
|
|
{ |
1413
|
12 |
|
$locale = \setlocale(LC_CTYPE, 0); |
1414
|
12 |
|
\setlocale(LC_CTYPE, 'C'); |
1415
|
12 |
|
$valid = !\ctype_alnum($value); |
1416
|
12 |
|
\setlocale(LC_CTYPE, $locale); |
1417
|
|
|
|
1418
|
12 |
|
if ($valid) { |
1419
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1420
|
8 |
|
$message ?: 'Expected a value to contain letters and digits only. Got: %s', |
1421
|
8 |
|
static::valueToString($value) |
1422
|
|
|
)); |
1423
|
|
|
} |
1424
|
4 |
|
} |
1425
|
|
|
|
1426
|
|
|
/** |
1427
|
|
|
* @psalm-pure |
1428
|
|
|
* @psalm-assert lowercase-string $value |
1429
|
|
|
* |
1430
|
|
|
* @param string $value |
1431
|
|
|
* @param string $message |
1432
|
|
|
* |
1433
|
|
|
* @throws InvalidArgumentException |
1434
|
|
|
*/ |
1435
|
16 |
View Code Duplication |
public static function lower($value, $message = '') |
1436
|
|
|
{ |
1437
|
16 |
|
$locale = \setlocale(LC_CTYPE, 0); |
1438
|
16 |
|
\setlocale(LC_CTYPE, 'C'); |
1439
|
16 |
|
$valid = !\ctype_lower($value); |
1440
|
16 |
|
\setlocale(LC_CTYPE, $locale); |
1441
|
|
|
|
1442
|
16 |
|
if ($valid) { |
1443
|
12 |
|
static::reportInvalidArgument(\sprintf( |
1444
|
12 |
|
$message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
1445
|
12 |
|
static::valueToString($value) |
1446
|
|
|
)); |
1447
|
|
|
} |
1448
|
4 |
|
} |
1449
|
|
|
|
1450
|
|
|
/** |
1451
|
|
|
* @psalm-pure |
1452
|
|
|
* @psalm-assert !lowercase-string $value |
1453
|
|
|
* |
1454
|
|
|
* @param string $value |
1455
|
|
|
* @param string $message |
1456
|
|
|
* |
1457
|
|
|
* @throws InvalidArgumentException |
1458
|
|
|
*/ |
1459
|
16 |
View Code Duplication |
public static function upper($value, $message = '') |
1460
|
|
|
{ |
1461
|
16 |
|
$locale = \setlocale(LC_CTYPE, 0); |
1462
|
16 |
|
\setlocale(LC_CTYPE, 'C'); |
1463
|
16 |
|
$valid = !\ctype_upper($value); |
1464
|
16 |
|
\setlocale(LC_CTYPE, $locale); |
1465
|
|
|
|
1466
|
16 |
|
if ($valid) { |
1467
|
12 |
|
static::reportInvalidArgument(\sprintf( |
1468
|
12 |
|
$message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
1469
|
12 |
|
static::valueToString($value) |
1470
|
|
|
)); |
1471
|
|
|
} |
1472
|
4 |
|
} |
1473
|
|
|
|
1474
|
|
|
/** |
1475
|
|
|
* @psalm-pure |
1476
|
|
|
* |
1477
|
|
|
* @param string $value |
1478
|
|
|
* @param int $length |
1479
|
|
|
* @param string $message |
1480
|
|
|
* |
1481
|
|
|
* @throws InvalidArgumentException |
1482
|
|
|
*/ |
1483
|
36 |
|
public static function length($value, $length, $message = '') |
1484
|
|
|
{ |
1485
|
36 |
|
if ($length !== static::strlen($value)) { |
1486
|
24 |
|
static::reportInvalidArgument(\sprintf( |
1487
|
24 |
|
$message ?: 'Expected a value to contain %2$s characters. Got: %s', |
1488
|
24 |
|
static::valueToString($value), |
1489
|
24 |
|
$length |
1490
|
|
|
)); |
1491
|
|
|
} |
1492
|
12 |
|
} |
1493
|
|
|
|
1494
|
|
|
/** |
1495
|
|
|
* Inclusive min. |
1496
|
|
|
* |
1497
|
|
|
* @psalm-pure |
1498
|
|
|
* |
1499
|
|
|
* @param string $value |
1500
|
|
|
* @param int|float $min |
1501
|
|
|
* @param string $message |
1502
|
|
|
* |
1503
|
|
|
* @throws InvalidArgumentException |
1504
|
|
|
*/ |
1505
|
36 |
|
public static function minLength($value, $min, $message = '') |
1506
|
|
|
{ |
1507
|
36 |
|
if (static::strlen($value) < $min) { |
1508
|
12 |
|
static::reportInvalidArgument(\sprintf( |
1509
|
12 |
|
$message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
1510
|
12 |
|
static::valueToString($value), |
1511
|
12 |
|
$min |
1512
|
|
|
)); |
1513
|
|
|
} |
1514
|
24 |
|
} |
1515
|
|
|
|
1516
|
|
|
/** |
1517
|
|
|
* Inclusive max. |
1518
|
|
|
* |
1519
|
|
|
* @psalm-pure |
1520
|
|
|
* |
1521
|
|
|
* @param string $value |
1522
|
|
|
* @param int|float $max |
1523
|
|
|
* @param string $message |
1524
|
|
|
* |
1525
|
|
|
* @throws InvalidArgumentException |
1526
|
|
|
*/ |
1527
|
36 |
|
public static function maxLength($value, $max, $message = '') |
1528
|
|
|
{ |
1529
|
36 |
|
if (static::strlen($value) > $max) { |
1530
|
12 |
|
static::reportInvalidArgument(\sprintf( |
1531
|
12 |
|
$message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
1532
|
12 |
|
static::valueToString($value), |
1533
|
12 |
|
$max |
1534
|
|
|
)); |
1535
|
|
|
} |
1536
|
24 |
|
} |
1537
|
|
|
|
1538
|
|
|
/** |
1539
|
|
|
* Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
1540
|
|
|
* |
1541
|
|
|
* @psalm-pure |
1542
|
|
|
* |
1543
|
|
|
* @param string $value |
1544
|
|
|
* @param int|float $min |
1545
|
|
|
* @param int|float $max |
1546
|
|
|
* @param string $message |
1547
|
|
|
* |
1548
|
|
|
* @throws InvalidArgumentException |
1549
|
|
|
*/ |
1550
|
60 |
View Code Duplication |
public static function lengthBetween($value, $min, $max, $message = '') |
1551
|
|
|
{ |
1552
|
60 |
|
$length = static::strlen($value); |
1553
|
|
|
|
1554
|
60 |
|
if ($length < $min || $length > $max) { |
1555
|
24 |
|
static::reportInvalidArgument(\sprintf( |
1556
|
24 |
|
$message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
1557
|
24 |
|
static::valueToString($value), |
1558
|
24 |
|
$min, |
1559
|
24 |
|
$max |
1560
|
|
|
)); |
1561
|
|
|
} |
1562
|
36 |
|
} |
1563
|
|
|
|
1564
|
|
|
/** |
1565
|
|
|
* Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
1566
|
|
|
* |
1567
|
|
|
* @param mixed $value |
1568
|
|
|
* @param string $message |
1569
|
|
|
* |
1570
|
|
|
* @throws InvalidArgumentException |
1571
|
|
|
*/ |
1572
|
36 |
View Code Duplication |
public static function fileExists($value, $message = '') |
1573
|
|
|
{ |
1574
|
36 |
|
static::string($value); |
1575
|
|
|
|
1576
|
36 |
|
if (!\file_exists($value)) { |
1577
|
12 |
|
static::reportInvalidArgument(\sprintf( |
1578
|
12 |
|
$message ?: 'The file %s does not exist.', |
1579
|
12 |
|
static::valueToString($value) |
1580
|
|
|
)); |
1581
|
|
|
} |
1582
|
24 |
|
} |
1583
|
|
|
|
1584
|
|
|
/** |
1585
|
|
|
* @param mixed $value |
1586
|
|
|
* @param string $message |
1587
|
|
|
* |
1588
|
|
|
* @throws InvalidArgumentException |
1589
|
|
|
*/ |
1590
|
12 |
View Code Duplication |
public static function file($value, $message = '') |
1591
|
|
|
{ |
1592
|
12 |
|
static::fileExists($value, $message); |
1593
|
|
|
|
1594
|
8 |
|
if (!\is_file($value)) { |
1595
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1596
|
4 |
|
$message ?: 'The path %s is not a file.', |
1597
|
4 |
|
static::valueToString($value) |
1598
|
|
|
)); |
1599
|
|
|
} |
1600
|
4 |
|
} |
1601
|
|
|
|
1602
|
|
|
/** |
1603
|
|
|
* @param mixed $value |
1604
|
|
|
* @param string $message |
1605
|
|
|
* |
1606
|
|
|
* @throws InvalidArgumentException |
1607
|
|
|
*/ |
1608
|
12 |
View Code Duplication |
public static function directory($value, $message = '') |
1609
|
|
|
{ |
1610
|
12 |
|
static::fileExists($value, $message); |
1611
|
|
|
|
1612
|
8 |
|
if (!\is_dir($value)) { |
1613
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1614
|
4 |
|
$message ?: 'The path %s is no directory.', |
1615
|
4 |
|
static::valueToString($value) |
1616
|
|
|
)); |
1617
|
|
|
} |
1618
|
4 |
|
} |
1619
|
|
|
|
1620
|
|
|
/** |
1621
|
|
|
* @param string $value |
1622
|
|
|
* @param string $message |
1623
|
|
|
* |
1624
|
|
|
* @throws InvalidArgumentException |
1625
|
|
|
*/ |
1626
|
|
|
public static function readable($value, $message = '') |
1627
|
|
|
{ |
1628
|
|
|
if (!\is_readable($value)) { |
1629
|
|
|
static::reportInvalidArgument(\sprintf( |
1630
|
|
|
$message ?: 'The path %s is not readable.', |
1631
|
|
|
static::valueToString($value) |
1632
|
|
|
)); |
1633
|
|
|
} |
1634
|
|
|
} |
1635
|
|
|
|
1636
|
|
|
/** |
1637
|
|
|
* @param string $value |
1638
|
|
|
* @param string $message |
1639
|
|
|
* |
1640
|
|
|
* @throws InvalidArgumentException |
1641
|
|
|
*/ |
1642
|
|
|
public static function writable($value, $message = '') |
1643
|
|
|
{ |
1644
|
|
|
if (!\is_writable($value)) { |
1645
|
|
|
static::reportInvalidArgument(\sprintf( |
1646
|
|
|
$message ?: 'The path %s is not writable.', |
1647
|
|
|
static::valueToString($value) |
1648
|
|
|
)); |
1649
|
|
|
} |
1650
|
|
|
} |
1651
|
|
|
|
1652
|
|
|
/** |
1653
|
|
|
* @param mixed $value |
1654
|
|
|
* @param string $message |
1655
|
|
|
* |
1656
|
|
|
* @throws InvalidArgumentException |
1657
|
|
|
*/ |
1658
|
8 |
|
public static function classExists($value, $message = '') |
1659
|
|
|
{ |
1660
|
8 |
|
if (!\class_exists($value)) { |
1661
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1662
|
4 |
|
$message ?: 'Expected an existing class name. Got: %s', |
1663
|
4 |
|
static::valueToString($value) |
1664
|
|
|
)); |
1665
|
|
|
} |
1666
|
4 |
|
} |
1667
|
|
|
|
1668
|
|
|
/** |
1669
|
|
|
* @psalm-pure |
1670
|
|
|
* @psalm-template ExpectedType of object |
1671
|
|
|
* @psalm-param class-string<ExpectedType> $class |
1672
|
|
|
* @psalm-assert class-string<ExpectedType>|ExpectedType $value |
1673
|
|
|
* |
1674
|
|
|
* @param mixed $value |
1675
|
|
|
* @param string|object $class |
1676
|
|
|
* @param string $message |
1677
|
|
|
* |
1678
|
|
|
* @throws InvalidArgumentException |
1679
|
|
|
*/ |
1680
|
8 |
|
public static function subclassOf($value, $class, $message = '') |
1681
|
|
|
{ |
1682
|
8 |
|
if (!\is_subclass_of($value, $class)) { |
1683
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1684
|
4 |
|
$message ?: 'Expected a sub-class of %2$s. Got: %s', |
1685
|
4 |
|
static::valueToString($value), |
1686
|
4 |
|
static::valueToString($class) |
1687
|
|
|
)); |
1688
|
|
|
} |
1689
|
4 |
|
} |
1690
|
|
|
|
1691
|
|
|
/** |
1692
|
|
|
* @param mixed $value |
1693
|
|
|
* @param string $message |
1694
|
|
|
* |
1695
|
|
|
* @throws InvalidArgumentException |
1696
|
|
|
*/ |
1697
|
8 |
|
public static function interfaceExists($value, $message = '') |
1698
|
|
|
{ |
1699
|
8 |
|
if (!\interface_exists($value)) { |
1700
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1701
|
4 |
|
$message ?: 'Expected an existing interface name. got %s', |
1702
|
4 |
|
static::valueToString($value) |
1703
|
|
|
)); |
1704
|
|
|
} |
1705
|
4 |
|
} |
1706
|
|
|
|
1707
|
|
|
/** |
1708
|
|
|
* @psalm-pure |
1709
|
|
|
* @psalm-template ExpectedType of object |
1710
|
|
|
* @psalm-param class-string<ExpectedType> $interface |
1711
|
|
|
* @psalm-assert class-string<ExpectedType> $value |
1712
|
|
|
* |
1713
|
|
|
* @param mixed $value |
1714
|
|
|
* @param mixed $interface |
1715
|
|
|
* @param string $message |
1716
|
|
|
* |
1717
|
|
|
* @throws InvalidArgumentException |
1718
|
|
|
*/ |
1719
|
8 |
View Code Duplication |
public static function implementsInterface($value, $interface, $message = '') |
1720
|
|
|
{ |
1721
|
8 |
|
if (!\in_array($interface, \class_implements($value))) { |
1722
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1723
|
4 |
|
$message ?: 'Expected an implementation of %2$s. Got: %s', |
1724
|
4 |
|
static::valueToString($value), |
1725
|
4 |
|
static::valueToString($interface) |
1726
|
|
|
)); |
1727
|
|
|
} |
1728
|
4 |
|
} |
1729
|
|
|
|
1730
|
|
|
/** |
1731
|
|
|
* @psalm-pure |
1732
|
|
|
* @psalm-param class-string|object $classOrObject |
1733
|
|
|
* |
1734
|
|
|
* @param string|object $classOrObject |
1735
|
|
|
* @param mixed $property |
1736
|
|
|
* @param string $message |
1737
|
|
|
* |
1738
|
|
|
* @throws InvalidArgumentException |
1739
|
|
|
*/ |
1740
|
12 |
View Code Duplication |
public static function propertyExists($classOrObject, $property, $message = '') |
1741
|
|
|
{ |
1742
|
12 |
|
if (!\property_exists($classOrObject, $property)) { |
1743
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1744
|
4 |
|
$message ?: 'Expected the property %s to exist.', |
1745
|
4 |
|
static::valueToString($property) |
1746
|
|
|
)); |
1747
|
|
|
} |
1748
|
8 |
|
} |
1749
|
|
|
|
1750
|
|
|
/** |
1751
|
|
|
* @psalm-pure |
1752
|
|
|
* @psalm-param class-string|object $classOrObject |
1753
|
|
|
* |
1754
|
|
|
* @param string|object $classOrObject |
1755
|
|
|
* @param mixed $property |
1756
|
|
|
* @param string $message |
1757
|
|
|
* |
1758
|
|
|
* @throws InvalidArgumentException |
1759
|
|
|
*/ |
1760
|
12 |
View Code Duplication |
public static function propertyNotExists($classOrObject, $property, $message = '') |
1761
|
|
|
{ |
1762
|
12 |
|
if (\property_exists($classOrObject, $property)) { |
1763
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1764
|
8 |
|
$message ?: 'Expected the property %s to not exist.', |
1765
|
8 |
|
static::valueToString($property) |
1766
|
|
|
)); |
1767
|
|
|
} |
1768
|
4 |
|
} |
1769
|
|
|
|
1770
|
|
|
/** |
1771
|
|
|
* @psalm-pure |
1772
|
|
|
* @psalm-param class-string|object $classOrObject |
1773
|
|
|
* |
1774
|
|
|
* @param string|object $classOrObject |
1775
|
|
|
* @param mixed $method |
1776
|
|
|
* @param string $message |
1777
|
|
|
* |
1778
|
|
|
* @throws InvalidArgumentException |
1779
|
|
|
*/ |
1780
|
27 |
View Code Duplication |
public static function methodExists($classOrObject, $method, $message = '') |
1781
|
|
|
{ |
1782
|
27 |
|
if (!\method_exists($classOrObject, $method)) { |
1783
|
19 |
|
static::reportInvalidArgument(\sprintf( |
1784
|
19 |
|
$message ?: 'Expected the method %s to exist.', |
1785
|
19 |
|
static::valueToString($method) |
1786
|
|
|
)); |
1787
|
|
|
} |
1788
|
8 |
|
} |
1789
|
|
|
|
1790
|
|
|
/** |
1791
|
|
|
* @psalm-pure |
1792
|
|
|
* @psalm-param class-string|object $classOrObject |
1793
|
|
|
* |
1794
|
|
|
* @param string|object $classOrObject |
1795
|
|
|
* @param mixed $method |
1796
|
|
|
* @param string $message |
1797
|
|
|
* |
1798
|
|
|
* @throws InvalidArgumentException |
1799
|
|
|
*/ |
1800
|
27 |
View Code Duplication |
public static function methodNotExists($classOrObject, $method, $message = '') |
1801
|
|
|
{ |
1802
|
27 |
|
if (\method_exists($classOrObject, $method)) { |
1803
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1804
|
8 |
|
$message ?: 'Expected the method %s to not exist.', |
1805
|
8 |
|
static::valueToString($method) |
1806
|
|
|
)); |
1807
|
|
|
} |
1808
|
19 |
|
} |
1809
|
|
|
|
1810
|
|
|
/** |
1811
|
|
|
* @psalm-pure |
1812
|
|
|
* |
1813
|
|
|
* @param array $array |
1814
|
|
|
* @param string|int $key |
1815
|
|
|
* @param string $message |
1816
|
|
|
* |
1817
|
|
|
* @throws InvalidArgumentException |
1818
|
|
|
*/ |
1819
|
12 |
View Code Duplication |
public static function keyExists($array, $key, $message = '') |
1820
|
|
|
{ |
1821
|
12 |
|
if (!(isset($array[$key]) || \array_key_exists($key, $array))) { |
1822
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1823
|
4 |
|
$message ?: 'Expected the key %s to exist.', |
1824
|
4 |
|
static::valueToString($key) |
1825
|
|
|
)); |
1826
|
|
|
} |
1827
|
8 |
|
} |
1828
|
|
|
|
1829
|
|
|
/** |
1830
|
|
|
* @psalm-pure |
1831
|
|
|
* |
1832
|
|
|
* @param array $array |
1833
|
|
|
* @param string|int $key |
1834
|
|
|
* @param string $message |
1835
|
|
|
* |
1836
|
|
|
* @throws InvalidArgumentException |
1837
|
|
|
*/ |
1838
|
12 |
View Code Duplication |
public static function keyNotExists($array, $key, $message = '') |
1839
|
|
|
{ |
1840
|
12 |
|
if (isset($array[$key]) || \array_key_exists($key, $array)) { |
1841
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1842
|
8 |
|
$message ?: 'Expected the key %s to not exist.', |
1843
|
8 |
|
static::valueToString($key) |
1844
|
|
|
)); |
1845
|
|
|
} |
1846
|
4 |
|
} |
1847
|
|
|
|
1848
|
|
|
/** |
1849
|
|
|
* Checks if a value is a valid array key (int or string). |
1850
|
|
|
* |
1851
|
|
|
* @psalm-pure |
1852
|
|
|
* @psalm-assert array-key $value |
1853
|
|
|
* |
1854
|
|
|
* @param mixed $value |
1855
|
|
|
* @param string $message |
1856
|
|
|
* |
1857
|
|
|
* @throws InvalidArgumentException |
1858
|
|
|
*/ |
1859
|
28 |
|
public static function validArrayKey($value, $message = '') |
1860
|
|
|
{ |
1861
|
28 |
|
if (!(\is_int($value) || \is_string($value))) { |
1862
|
20 |
|
static::reportInvalidArgument(\sprintf( |
1863
|
20 |
|
$message ?: 'Expected string or integer. Got: %s', |
1864
|
20 |
|
static::typeToString($value) |
1865
|
|
|
)); |
1866
|
|
|
} |
1867
|
8 |
|
} |
1868
|
|
|
|
1869
|
|
|
/** |
1870
|
|
|
* Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
1871
|
|
|
* |
1872
|
|
|
* @param Countable|array $array |
1873
|
|
|
* @param int $number |
1874
|
|
|
* @param string $message |
1875
|
|
|
* |
1876
|
|
|
* @throws InvalidArgumentException |
1877
|
|
|
*/ |
1878
|
8 |
|
public static function count($array, $number, $message = '') |
1879
|
|
|
{ |
1880
|
8 |
|
static::eq( |
1881
|
8 |
|
\count($array), |
1882
|
|
|
$number, |
1883
|
8 |
|
\sprintf( |
1884
|
8 |
|
$message ?: 'Expected an array to contain %d elements. Got: %d.', |
1885
|
8 |
|
$number, |
1886
|
8 |
|
\count($array) |
1887
|
|
|
) |
1888
|
|
|
); |
1889
|
4 |
|
} |
1890
|
|
|
|
1891
|
|
|
/** |
1892
|
|
|
* Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
1893
|
|
|
* |
1894
|
|
|
* @param Countable|array $array |
1895
|
|
|
* @param int|float $min |
1896
|
|
|
* @param string $message |
1897
|
|
|
* |
1898
|
|
|
* @throws InvalidArgumentException |
1899
|
|
|
*/ |
1900
|
12 |
View Code Duplication |
public static function minCount($array, $min, $message = '') |
1901
|
|
|
{ |
1902
|
12 |
|
if (\count($array) < $min) { |
1903
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1904
|
4 |
|
$message ?: 'Expected an array to contain at least %2$d elements. Got: %d', |
1905
|
4 |
|
\count($array), |
1906
|
4 |
|
$min |
1907
|
|
|
)); |
1908
|
|
|
} |
1909
|
8 |
|
} |
1910
|
|
|
|
1911
|
|
|
/** |
1912
|
|
|
* Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
1913
|
|
|
* |
1914
|
|
|
* @param Countable|array $array |
1915
|
|
|
* @param int|float $max |
1916
|
|
|
* @param string $message |
1917
|
|
|
* |
1918
|
|
|
* @throws InvalidArgumentException |
1919
|
|
|
*/ |
1920
|
12 |
View Code Duplication |
public static function maxCount($array, $max, $message = '') |
1921
|
|
|
{ |
1922
|
12 |
|
if (\count($array) > $max) { |
1923
|
4 |
|
static::reportInvalidArgument(\sprintf( |
1924
|
4 |
|
$message ?: 'Expected an array to contain at most %2$d elements. Got: %d', |
1925
|
4 |
|
\count($array), |
1926
|
4 |
|
$max |
1927
|
|
|
)); |
1928
|
|
|
} |
1929
|
8 |
|
} |
1930
|
|
|
|
1931
|
|
|
/** |
1932
|
|
|
* Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
1933
|
|
|
* |
1934
|
|
|
* @param Countable|array $array |
1935
|
|
|
* @param int|float $min |
1936
|
|
|
* @param int|float $max |
1937
|
|
|
* @param string $message |
1938
|
|
|
* |
1939
|
|
|
* @throws InvalidArgumentException |
1940
|
|
|
*/ |
1941
|
20 |
|
public static function countBetween($array, $min, $max, $message = '') |
1942
|
|
|
{ |
1943
|
20 |
|
$count = \count($array); |
1944
|
|
|
|
1945
|
20 |
|
if ($count < $min || $count > $max) { |
1946
|
8 |
|
static::reportInvalidArgument(\sprintf( |
1947
|
8 |
|
$message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d', |
1948
|
8 |
|
$count, |
1949
|
8 |
|
$min, |
1950
|
8 |
|
$max |
1951
|
|
|
)); |
1952
|
|
|
} |
1953
|
12 |
|
} |
1954
|
|
|
|
1955
|
|
|
/** |
1956
|
|
|
* @psalm-pure |
1957
|
|
|
* @psalm-assert list $array |
1958
|
|
|
* |
1959
|
|
|
* @param mixed $array |
1960
|
|
|
* @param string $message |
1961
|
|
|
* |
1962
|
|
|
* @throws InvalidArgumentException |
1963
|
|
|
*/ |
1964
|
80 |
View Code Duplication |
public static function isList($array, $message = '') |
1965
|
|
|
{ |
1966
|
80 |
|
if (!\is_array($array) || $array !== \array_values($array)) { |
1967
|
32 |
|
static::reportInvalidArgument( |
1968
|
32 |
|
$message ?: 'Expected list - non-associative array.' |
1969
|
|
|
); |
1970
|
|
|
} |
1971
|
48 |
|
} |
1972
|
|
|
|
1973
|
|
|
/** |
1974
|
|
|
* @psalm-pure |
1975
|
|
|
* @psalm-assert list $array |
1976
|
|
|
* @psalm-assert !empty $array |
1977
|
|
|
* |
1978
|
|
|
* @param mixed $array |
1979
|
|
|
* @param string $message |
1980
|
|
|
* |
1981
|
|
|
* @throws InvalidArgumentException |
1982
|
|
|
*/ |
1983
|
40 |
|
public static function isNonEmptyList($array, $message = '') |
1984
|
|
|
{ |
1985
|
40 |
|
static::isList($array, $message); |
1986
|
24 |
|
static::notEmpty($array, $message); |
1987
|
20 |
|
} |
1988
|
|
|
|
1989
|
|
|
/** |
1990
|
|
|
* @psalm-pure |
1991
|
|
|
* @psalm-template T |
1992
|
|
|
* @psalm-param mixed|array<T> $array |
1993
|
|
|
* @psalm-assert array<string, T> $array |
1994
|
|
|
* |
1995
|
|
|
* @param mixed $array |
1996
|
|
|
* @param string $message |
1997
|
|
|
* |
1998
|
|
|
* @throws InvalidArgumentException |
1999
|
|
|
*/ |
2000
|
32 |
|
public static function isMap($array, $message = '') |
2001
|
|
|
{ |
2002
|
|
|
if ( |
2003
|
32 |
|
!\is_array($array) || |
2004
|
32 |
|
\array_keys($array) !== \array_filter(\array_keys($array), '\is_string') |
2005
|
|
|
) { |
2006
|
16 |
|
static::reportInvalidArgument( |
2007
|
16 |
|
$message ?: 'Expected map - associative array with string keys.' |
2008
|
|
|
); |
2009
|
|
|
} |
2010
|
16 |
|
} |
2011
|
|
|
|
2012
|
|
|
/** |
2013
|
|
|
* @psalm-pure |
2014
|
|
|
* @psalm-template T |
2015
|
|
|
* @psalm-param mixed|array<T> $array |
2016
|
|
|
* @psalm-assert array<string, T> $array |
2017
|
|
|
* @psalm-assert !empty $array |
2018
|
|
|
* |
2019
|
|
|
* @param mixed $array |
2020
|
|
|
* @param string $message |
2021
|
|
|
* |
2022
|
|
|
* @throws InvalidArgumentException |
2023
|
|
|
*/ |
2024
|
16 |
|
public static function isNonEmptyMap($array, $message = '') |
2025
|
|
|
{ |
2026
|
16 |
|
static::isMap($array, $message); |
2027
|
8 |
|
static::notEmpty($array, $message); |
2028
|
4 |
|
} |
2029
|
|
|
|
2030
|
|
|
/** |
2031
|
|
|
* @psalm-pure |
2032
|
|
|
* |
2033
|
|
|
* @param string $value |
2034
|
|
|
* @param string $message |
2035
|
|
|
* |
2036
|
|
|
* @throws InvalidArgumentException |
2037
|
|
|
*/ |
2038
|
56 |
|
public static function uuid($value, $message = '') |
2039
|
|
|
{ |
2040
|
56 |
|
$value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
2041
|
|
|
|
2042
|
|
|
// The nil UUID is special form of UUID that is specified to have all |
2043
|
|
|
// 128 bits set to zero. |
2044
|
56 |
|
if ('00000000-0000-0000-0000-000000000000' === $value) { |
2045
|
4 |
|
return; |
2046
|
|
|
} |
2047
|
|
|
|
2048
|
52 |
|
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)) { |
2049
|
20 |
|
static::reportInvalidArgument(\sprintf( |
2050
|
20 |
|
$message ?: 'Value %s is not a valid UUID.', |
2051
|
20 |
|
static::valueToString($value) |
2052
|
|
|
)); |
2053
|
|
|
} |
2054
|
32 |
|
} |
2055
|
|
|
|
2056
|
|
|
/** |
2057
|
|
|
* @psalm-param class-string<Throwable> |
2058
|
|
|
* |
2059
|
|
|
* @param Closure $expression |
2060
|
|
|
* @param string $class |
2061
|
|
|
* @param string $message |
2062
|
|
|
* |
2063
|
|
|
* @throws InvalidArgumentException |
2064
|
|
|
*/ |
2065
|
24 |
|
public static function throws(Closure $expression, $class = 'Exception', $message = '') |
2066
|
|
|
{ |
2067
|
24 |
|
static::string($class); |
2068
|
|
|
|
2069
|
24 |
|
$actual = 'none'; |
2070
|
|
|
|
2071
|
|
|
try { |
2072
|
24 |
|
$expression(); |
2073
|
24 |
|
} catch (Exception $e) { |
2074
|
20 |
|
$actual = \get_class($e); |
2075
|
20 |
|
if ($e instanceof $class) { |
2076
|
20 |
|
return; |
2077
|
|
|
} |
2078
|
4 |
|
} catch (Throwable $e) { |
|
|
|
|
2079
|
4 |
|
$actual = \get_class($e); |
2080
|
4 |
|
if ($e instanceof $class) { |
2081
|
4 |
|
return; |
2082
|
|
|
} |
2083
|
|
|
} |
2084
|
|
|
|
2085
|
8 |
|
static::reportInvalidArgument($message ?: \sprintf( |
2086
|
8 |
|
'Expected to throw "%s", got "%s"', |
2087
|
8 |
|
$class, |
2088
|
8 |
|
$actual |
2089
|
|
|
)); |
2090
|
|
|
} |
2091
|
|
|
|
2092
|
|
|
/** |
2093
|
|
|
* @throws BadMethodCallException |
2094
|
|
|
*/ |
2095
|
1635 |
|
public static function __callStatic($name, $arguments) |
2096
|
|
|
{ |
2097
|
1635 |
|
if ('nullOr' === \substr($name, 0, 6)) { |
2098
|
604 |
|
if (null !== $arguments[0]) { |
2099
|
499 |
|
$method = \lcfirst(\substr($name, 6)); |
2100
|
499 |
|
\call_user_func_array(array('static', $method), $arguments); |
2101
|
|
|
} |
2102
|
|
|
|
2103
|
352 |
|
return; |
2104
|
|
|
} |
2105
|
|
|
|
2106
|
1031 |
|
if ('all' === \substr($name, 0, 3)) { |
2107
|
1030 |
|
static::isIterable($arguments[0]); |
2108
|
|
|
|
2109
|
1030 |
|
$method = \lcfirst(\substr($name, 3)); |
2110
|
1030 |
|
$args = $arguments; |
2111
|
|
|
|
2112
|
1030 |
|
foreach ($arguments[0] as $entry) { |
2113
|
1030 |
|
$args[0] = $entry; |
2114
|
|
|
|
2115
|
1030 |
|
\call_user_func_array(array('static', $method), $args); |
2116
|
|
|
} |
2117
|
|
|
|
2118
|
502 |
|
return; |
2119
|
|
|
} |
2120
|
|
|
|
2121
|
1 |
|
throw new BadMethodCallException('No such method: '.$name); |
2122
|
|
|
} |
2123
|
|
|
|
2124
|
|
|
/** |
2125
|
|
|
* @param mixed $value |
2126
|
|
|
* |
2127
|
|
|
* @return string |
2128
|
|
|
*/ |
2129
|
747 |
|
protected static function valueToString($value) |
2130
|
|
|
{ |
2131
|
747 |
|
if (null === $value) { |
2132
|
20 |
|
return 'null'; |
2133
|
|
|
} |
2134
|
|
|
|
2135
|
729 |
|
if (true === $value) { |
2136
|
15 |
|
return 'true'; |
2137
|
|
|
} |
2138
|
|
|
|
2139
|
719 |
|
if (false === $value) { |
2140
|
25 |
|
return 'false'; |
2141
|
|
|
} |
2142
|
|
|
|
2143
|
694 |
|
if (\is_array($value)) { |
2144
|
21 |
|
return 'array'; |
2145
|
|
|
} |
2146
|
|
|
|
2147
|
673 |
|
if (\is_object($value)) { |
2148
|
3 |
|
if (\method_exists($value, '__toString')) { |
2149
|
1 |
|
return \get_class($value).': '.self::valueToString($value->__toString()); |
2150
|
|
|
} |
2151
|
|
|
|
2152
|
2 |
|
if ($value instanceof DateTime || $value instanceof DateTimeImmutable) { |
2153
|
1 |
|
return \get_class($value).': '.self::valueToString($value->format('c')); |
2154
|
|
|
} |
2155
|
|
|
|
2156
|
1 |
|
return \get_class($value); |
2157
|
|
|
} |
2158
|
|
|
|
2159
|
672 |
|
if (\is_resource($value)) { |
2160
|
1 |
|
return 'resource'; |
2161
|
|
|
} |
2162
|
|
|
|
2163
|
672 |
|
if (\is_string($value)) { |
2164
|
570 |
|
return '"'.$value.'"'; |
2165
|
|
|
} |
2166
|
|
|
|
2167
|
110 |
|
return (string) $value; |
2168
|
|
|
} |
2169
|
|
|
|
2170
|
|
|
/** |
2171
|
|
|
* @param mixed $value |
2172
|
|
|
* |
2173
|
|
|
* @return string |
2174
|
|
|
*/ |
2175
|
251 |
|
protected static function typeToString($value) |
2176
|
|
|
{ |
2177
|
251 |
|
return \is_object($value) ? \get_class($value) : \gettype($value); |
2178
|
|
|
} |
2179
|
|
|
|
2180
|
168 |
|
protected static function strlen($value) |
2181
|
|
|
{ |
2182
|
168 |
|
if (!\function_exists('mb_detect_encoding')) { |
2183
|
|
|
return \strlen($value); |
2184
|
|
|
} |
2185
|
|
|
|
2186
|
168 |
|
if (false === $encoding = \mb_detect_encoding($value)) { |
2187
|
|
|
return \strlen($value); |
2188
|
|
|
} |
2189
|
|
|
|
2190
|
168 |
|
return \mb_strlen($value, $encoding); |
2191
|
|
|
} |
2192
|
|
|
|
2193
|
|
|
/** |
2194
|
|
|
* @param string $message |
2195
|
|
|
* |
2196
|
|
|
* @throws InvalidArgumentException |
2197
|
|
|
* |
2198
|
|
|
* @psalm-pure this method is not supposed to perform side-effects |
2199
|
|
|
*/ |
2200
|
1061 |
|
protected static function reportInvalidArgument($message) |
2201
|
|
|
{ |
2202
|
1061 |
|
throw new InvalidArgumentException($message); |
2203
|
|
|
} |
2204
|
|
|
|
2205
|
|
|
private function __construct() |
2206
|
|
|
{ |
2207
|
|
|
} |
2208
|
|
|
} |
2209
|
|
|
|