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