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