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