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