Blame view

vendor/webmozart/assert/src/Assert.php 31.6 KB
196bf1b1   Etienne Pallier   modifs de droits ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

/*
 * This file is part of the webmozart/assert package.
 *
 * (c) Bernhard Schussek <bschussek@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Webmozart\Assert;

use BadMethodCallException;
use InvalidArgumentException;
use Traversable;
4b7c9c89   Thibault Ajas   maj installation.sh
17
18
19
use Exception;
use Throwable;
use Closure;
196bf1b1   Etienne Pallier   modifs de droits ...
20
21
22
23
24
25
26
27
28
29
30
31

/**
 * Efficient assertions to validate the input/output of your methods.
 *
 * @method static void nullOrString($value, $message = '')
 * @method static void nullOrStringNotEmpty($value, $message = '')
 * @method static void nullOrInteger($value, $message = '')
 * @method static void nullOrIntegerish($value, $message = '')
 * @method static void nullOrFloat($value, $message = '')
 * @method static void nullOrNumeric($value, $message = '')
 * @method static void nullOrBoolean($value, $message = '')
 * @method static void nullOrScalar($value, $message = '')
4b7c9c89   Thibault Ajas   maj installation.sh
32
 * @method static void nullOrObject($value, $message = '')
196bf1b1   Etienne Pallier   modifs de droits ...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 * @method static void nullOrResource($value, $type = null, $message = '')
 * @method static void nullOrIsCallable($value, $message = '')
 * @method static void nullOrIsArray($value, $message = '')
 * @method static void nullOrIsTraversable($value, $message = '')
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
 * @method static void nullOrIsEmpty($value, $message = '')
 * @method static void nullOrNotEmpty($value, $message = '')
 * @method static void nullOrTrue($value, $message = '')
 * @method static void nullOrFalse($value, $message = '')
 * @method static void nullOrEq($value, $value2, $message = '')
 * @method static void nullOrNotEq($value,$value2,  $message = '')
 * @method static void nullOrSame($value, $value2, $message = '')
 * @method static void nullOrNotSame($value, $value2, $message = '')
 * @method static void nullOrGreaterThan($value, $value2, $message = '')
 * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
 * @method static void nullOrLessThan($value, $value2, $message = '')
 * @method static void nullOrLessThanEq($value, $value2, $message = '')
 * @method static void nullOrRange($value, $min, $max, $message = '')
 * @method static void nullOrOneOf($value, $values, $message = '')
 * @method static void nullOrContains($value, $subString, $message = '')
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
 * @method static void nullOrStartsWithLetter($value, $message = '')
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
 * @method static void nullOrRegex($value, $pattern, $message = '')
 * @method static void nullOrAlpha($value, $message = '')
 * @method static void nullOrDigits($value, $message = '')
 * @method static void nullOrAlnum($value, $message = '')
 * @method static void nullOrLower($value, $message = '')
 * @method static void nullOrUpper($value, $message = '')
 * @method static void nullOrLength($value, $length, $message = '')
 * @method static void nullOrMinLength($value, $min, $message = '')
 * @method static void nullOrMaxLength($value, $max, $message = '')
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
 * @method static void nullOrFileExists($value, $message = '')
 * @method static void nullOrFile($value, $message = '')
 * @method static void nullOrDirectory($value, $message = '')
 * @method static void nullOrReadable($value, $message = '')
 * @method static void nullOrWritable($value, $message = '')
 * @method static void nullOrClassExists($value, $message = '')
 * @method static void nullOrSubclassOf($value, $class, $message = '')
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
4b7c9c89   Thibault Ajas   maj installation.sh
75
76
77
78
79
80
81
82
 * @method static void nullOrPropertyExists($value, $property, $message = '')
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
 * @method static void nullOrMethodExists($value, $method, $message = '')
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
 * @method static void nullOrKeyExists($value, $key, $message = '')
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
 * @method static void nullOrCount($value, $key, $message = '')
 * @method static void nullOrUuid($values, $message = '')
196bf1b1   Etienne Pallier   modifs de droits ...
83
84
85
86
87
88
89
90
 * @method static void allString($values, $message = '')
 * @method static void allStringNotEmpty($values, $message = '')
 * @method static void allInteger($values, $message = '')
 * @method static void allIntegerish($values, $message = '')
 * @method static void allFloat($values, $message = '')
 * @method static void allNumeric($values, $message = '')
 * @method static void allBoolean($values, $message = '')
 * @method static void allScalar($values, $message = '')
4b7c9c89   Thibault Ajas   maj installation.sh
91
 * @method static void allObject($values, $message = '')
196bf1b1   Etienne Pallier   modifs de droits ...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 * @method static void allResource($values, $type = null, $message = '')
 * @method static void allIsCallable($values, $message = '')
 * @method static void allIsArray($values, $message = '')
 * @method static void allIsTraversable($values, $message = '')
 * @method static void allIsInstanceOf($values, $class, $message = '')
 * @method static void allNotInstanceOf($values, $class, $message = '')
 * @method static void allNull($values, $message = '')
 * @method static void allNotNull($values, $message = '')
 * @method static void allIsEmpty($values, $message = '')
 * @method static void allNotEmpty($values, $message = '')
 * @method static void allTrue($values, $message = '')
 * @method static void allFalse($values, $message = '')
 * @method static void allEq($values, $value2, $message = '')
 * @method static void allNotEq($values,$value2,  $message = '')
 * @method static void allSame($values, $value2, $message = '')
 * @method static void allNotSame($values, $value2, $message = '')
 * @method static void allGreaterThan($values, $value2, $message = '')
 * @method static void allGreaterThanEq($values, $value2, $message = '')
 * @method static void allLessThan($values, $value2, $message = '')
 * @method static void allLessThanEq($values, $value2, $message = '')
 * @method static void allRange($values, $min, $max, $message = '')
 * @method static void allOneOf($values, $values, $message = '')
 * @method static void allContains($values, $subString, $message = '')
 * @method static void allStartsWith($values, $prefix, $message = '')
 * @method static void allStartsWithLetter($values, $message = '')
 * @method static void allEndsWith($values, $suffix, $message = '')
 * @method static void allRegex($values, $pattern, $message = '')
 * @method static void allAlpha($values, $message = '')
 * @method static void allDigits($values, $message = '')
 * @method static void allAlnum($values, $message = '')
 * @method static void allLower($values, $message = '')
 * @method static void allUpper($values, $message = '')
 * @method static void allLength($values, $length, $message = '')
 * @method static void allMinLength($values, $min, $message = '')
 * @method static void allMaxLength($values, $max, $message = '')
 * @method static void allLengthBetween($values, $min, $max, $message = '')
 * @method static void allFileExists($values, $message = '')
 * @method static void allFile($values, $message = '')
 * @method static void allDirectory($values, $message = '')
 * @method static void allReadable($values, $message = '')
 * @method static void allWritable($values, $message = '')
 * @method static void allClassExists($values, $message = '')
 * @method static void allSubclassOf($values, $class, $message = '')
 * @method static void allImplementsInterface($values, $interface, $message = '')
4b7c9c89   Thibault Ajas   maj installation.sh
136
137
138
139
140
141
142
143
 * @method static void allPropertyExists($values, $property, $message = '')
 * @method static void allPropertyNotExists($values, $property, $message = '')
 * @method static void allMethodExists($values, $method, $message = '')
 * @method static void allMethodNotExists($values, $method, $message = '')
 * @method static void allKeyExists($values, $key, $message = '')
 * @method static void allKeyNotExists($values, $key, $message = '')
 * @method static void allCount($values, $key, $message = '')
 * @method static void allUuid($values, $message = '')
196bf1b1   Etienne Pallier   modifs de droits ...
144
145
146
147
148
149
150
151
152
153
 *
 * @since  1.0
 *
 * @author Bernhard Schussek <bschussek@gmail.com>
 */
class Assert
{
    public static function string($value, $message = '')
    {
        if (!is_string($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
154
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
155
                $message ?: 'Expected a string. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
156
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
157
158
159
160
161
162
            ));
        }
    }

    public static function stringNotEmpty($value, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
163
164
        static::string($value, $message);
        static::notEmpty($value, $message);
196bf1b1   Etienne Pallier   modifs de droits ...
165
166
167
168
169
    }

    public static function integer($value, $message = '')
    {
        if (!is_int($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
170
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
171
                $message ?: 'Expected an integer. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
172
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
173
174
175
176
177
178
179
            ));
        }
    }

    public static function integerish($value, $message = '')
    {
        if (!is_numeric($value) || $value != (int) $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
180
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
181
                $message ?: 'Expected an integerish value. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
182
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
183
184
185
186
187
188
189
            ));
        }
    }

    public static function float($value, $message = '')
    {
        if (!is_float($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
190
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
191
                $message ?: 'Expected a float. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
192
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
193
194
195
196
197
198
199
            ));
        }
    }

    public static function numeric($value, $message = '')
    {
        if (!is_numeric($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
200
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
201
                $message ?: 'Expected a numeric. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
202
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
203
204
205
206
207
208
209
            ));
        }
    }

    public static function boolean($value, $message = '')
    {
        if (!is_bool($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
210
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
211
                $message ?: 'Expected a boolean. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
212
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
213
214
215
216
217
218
219
            ));
        }
    }

    public static function scalar($value, $message = '')
    {
        if (!is_scalar($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
220
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
221
                $message ?: 'Expected a scalar. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
222
223
224
225
226
227
228
229
230
231
232
                static::typeToString($value)
            ));
        }
    }

    public static function object($value, $message = '')
    {
        if (!is_object($value)) {
            static::reportInvalidArgument(sprintf(
                $message ?: 'Expected an object. Got: %s',
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
233
234
235
236
237
238
239
            ));
        }
    }

    public static function resource($value, $type = null, $message = '')
    {
        if (!is_resource($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
240
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
241
                $message ?: 'Expected a resource. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
242
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
243
244
245
246
            ));
        }

        if ($type && $type !== get_resource_type($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
247
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
248
                $message ?: 'Expected a resource of type %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
249
                static::typeToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
250
251
252
253
254
255
256
257
                $type
            ));
        }
    }

    public static function isCallable($value, $message = '')
    {
        if (!is_callable($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
258
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
259
                $message ?: 'Expected a callable. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
260
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
261
262
263
264
265
266
267
            ));
        }
    }

    public static function isArray($value, $message = '')
    {
        if (!is_array($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
268
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
269
                $message ?: 'Expected an array. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
270
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
271
272
273
274
275
276
277
            ));
        }
    }

    public static function isTraversable($value, $message = '')
    {
        if (!is_array($value) && !($value instanceof Traversable)) {
4b7c9c89   Thibault Ajas   maj installation.sh
278
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
279
                $message ?: 'Expected a traversable. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
280
                static::typeToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
281
282
283
284
285
286
287
            ));
        }
    }

    public static function isInstanceOf($value, $class, $message = '')
    {
        if (!($value instanceof $class)) {
4b7c9c89   Thibault Ajas   maj installation.sh
288
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
289
                $message ?: 'Expected an instance of %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
290
                static::typeToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
291
292
293
294
295
296
297
298
                $class
            ));
        }
    }

    public static function notInstanceOf($value, $class, $message = '')
    {
        if ($value instanceof $class) {
4b7c9c89   Thibault Ajas   maj installation.sh
299
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
300
                $message ?: 'Expected an instance other than %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
301
                static::typeToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
302
303
304
305
306
307
308
309
                $class
            ));
        }
    }

    public static function isEmpty($value, $message = '')
    {
        if (!empty($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
310
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
311
                $message ?: 'Expected an empty value. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
312
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
313
314
315
316
317
318
319
            ));
        }
    }

    public static function notEmpty($value, $message = '')
    {
        if (empty($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
320
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
321
                $message ?: 'Expected a non-empty value. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
322
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
323
324
325
326
327
328
329
            ));
        }
    }

    public static function null($value, $message = '')
    {
        if (null !== $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
330
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
331
                $message ?: 'Expected null. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
332
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
333
334
335
336
337
338
339
            ));
        }
    }

    public static function notNull($value, $message = '')
    {
        if (null === $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
340
            static::reportInvalidArgument(
196bf1b1   Etienne Pallier   modifs de droits ...
341
342
343
344
345
346
347
348
                $message ?: 'Expected a value other than null.'
            );
        }
    }

    public static function true($value, $message = '')
    {
        if (true !== $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
349
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
350
                $message ?: 'Expected a value to be true. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
351
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
352
353
354
355
356
357
358
            ));
        }
    }

    public static function false($value, $message = '')
    {
        if (false !== $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
359
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
360
                $message ?: 'Expected a value to be false. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
361
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
362
363
364
365
366
367
368
            ));
        }
    }

    public static function eq($value, $value2, $message = '')
    {
        if ($value2 != $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
369
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
370
                $message ?: 'Expected a value equal to %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
371
372
                static::valueToString($value),
                static::valueToString($value2)
196bf1b1   Etienne Pallier   modifs de droits ...
373
374
375
376
377
378
379
            ));
        }
    }

    public static function notEq($value, $value2, $message = '')
    {
        if ($value2 == $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
380
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
381
                $message ?: 'Expected a different value than %s.',
4b7c9c89   Thibault Ajas   maj installation.sh
382
                static::valueToString($value2)
196bf1b1   Etienne Pallier   modifs de droits ...
383
384
385
386
387
388
389
            ));
        }
    }

    public static function same($value, $value2, $message = '')
    {
        if ($value2 !== $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
390
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
391
                $message ?: 'Expected a value identical to %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
392
393
                static::valueToString($value),
                static::valueToString($value2)
196bf1b1   Etienne Pallier   modifs de droits ...
394
395
396
397
398
399
400
            ));
        }
    }

    public static function notSame($value, $value2, $message = '')
    {
        if ($value2 === $value) {
4b7c9c89   Thibault Ajas   maj installation.sh
401
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
402
                $message ?: 'Expected a value not identical to %s.',
4b7c9c89   Thibault Ajas   maj installation.sh
403
                static::valueToString($value2)
196bf1b1   Etienne Pallier   modifs de droits ...
404
405
406
407
408
409
410
            ));
        }
    }

    public static function greaterThan($value, $limit, $message = '')
    {
        if ($value <= $limit) {
4b7c9c89   Thibault Ajas   maj installation.sh
411
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
412
                $message ?: 'Expected a value greater than %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
413
414
                static::valueToString($value),
                static::valueToString($limit)
196bf1b1   Etienne Pallier   modifs de droits ...
415
416
417
418
419
420
421
            ));
        }
    }

    public static function greaterThanEq($value, $limit, $message = '')
    {
        if ($value < $limit) {
4b7c9c89   Thibault Ajas   maj installation.sh
422
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
423
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
424
425
                static::valueToString($value),
                static::valueToString($limit)
196bf1b1   Etienne Pallier   modifs de droits ...
426
427
428
429
430
431
432
            ));
        }
    }

    public static function lessThan($value, $limit, $message = '')
    {
        if ($value >= $limit) {
4b7c9c89   Thibault Ajas   maj installation.sh
433
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
434
                $message ?: 'Expected a value less than %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
435
436
                static::valueToString($value),
                static::valueToString($limit)
196bf1b1   Etienne Pallier   modifs de droits ...
437
438
439
440
441
442
443
            ));
        }
    }

    public static function lessThanEq($value, $limit, $message = '')
    {
        if ($value > $limit) {
4b7c9c89   Thibault Ajas   maj installation.sh
444
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
445
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
446
447
                static::valueToString($value),
                static::valueToString($limit)
196bf1b1   Etienne Pallier   modifs de droits ...
448
449
450
451
452
453
454
            ));
        }
    }

    public static function range($value, $min, $max, $message = '')
    {
        if ($value < $min || $value > $max) {
4b7c9c89   Thibault Ajas   maj installation.sh
455
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
456
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
457
458
459
                static::valueToString($value),
                static::valueToString($min),
                static::valueToString($max)
196bf1b1   Etienne Pallier   modifs de droits ...
460
461
462
463
464
465
466
            ));
        }
    }

    public static function oneOf($value, array $values, $message = '')
    {
        if (!in_array($value, $values, true)) {
4b7c9c89   Thibault Ajas   maj installation.sh
467
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
468
                $message ?: 'Expected one of: %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
469
470
                static::valueToString($value),
                implode(', ', array_map(array('static', 'valueToString'), $values))
196bf1b1   Etienne Pallier   modifs de droits ...
471
472
473
474
475
476
477
            ));
        }
    }

    public static function contains($value, $subString, $message = '')
    {
        if (false === strpos($value, $subString)) {
4b7c9c89   Thibault Ajas   maj installation.sh
478
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
479
                $message ?: 'Expected a value to contain %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
480
481
                static::valueToString($value),
                static::valueToString($subString)
196bf1b1   Etienne Pallier   modifs de droits ...
482
483
484
485
486
487
488
            ));
        }
    }

    public static function startsWith($value, $prefix, $message = '')
    {
        if (0 !== strpos($value, $prefix)) {
4b7c9c89   Thibault Ajas   maj installation.sh
489
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
490
                $message ?: 'Expected a value to start with %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
491
492
                static::valueToString($value),
                static::valueToString($prefix)
196bf1b1   Etienne Pallier   modifs de droits ...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
            ));
        }
    }

    public static function startsWithLetter($value, $message = '')
    {
        $valid = isset($value[0]);

        if ($valid) {
            $locale = setlocale(LC_CTYPE, 0);
            setlocale(LC_CTYPE, 'C');
            $valid = ctype_alpha($value[0]);
            setlocale(LC_CTYPE, $locale);
        }

        if (!$valid) {
4b7c9c89   Thibault Ajas   maj installation.sh
509
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
510
                $message ?: 'Expected a value to start with a letter. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
511
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
512
513
514
515
516
517
            ));
        }
    }

    public static function endsWith($value, $suffix, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
518
519
        if ($suffix !== substr($value, -static::strlen($suffix))) {
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
520
                $message ?: 'Expected a value to end with %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
521
522
                static::valueToString($value),
                static::valueToString($suffix)
196bf1b1   Etienne Pallier   modifs de droits ...
523
524
525
526
527
528
529
            ));
        }
    }

    public static function regex($value, $pattern, $message = '')
    {
        if (!preg_match($pattern, $value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
530
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
531
                $message ?: 'The value %s does not match the expected pattern.',
4b7c9c89   Thibault Ajas   maj installation.sh
532
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
533
534
535
536
537
538
539
540
541
542
543
544
            ));
        }
    }

    public static function alpha($value, $message = '')
    {
        $locale = setlocale(LC_CTYPE, 0);
        setlocale(LC_CTYPE, 'C');
        $valid = !ctype_alpha($value);
        setlocale(LC_CTYPE, $locale);

        if ($valid) {
4b7c9c89   Thibault Ajas   maj installation.sh
545
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
546
                $message ?: 'Expected a value to contain only letters. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
547
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
548
549
550
551
552
553
554
555
556
557
558
559
            ));
        }
    }

    public static function digits($value, $message = '')
    {
        $locale = setlocale(LC_CTYPE, 0);
        setlocale(LC_CTYPE, 'C');
        $valid = !ctype_digit($value);
        setlocale(LC_CTYPE, $locale);

        if ($valid) {
4b7c9c89   Thibault Ajas   maj installation.sh
560
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
561
                $message ?: 'Expected a value to contain digits only. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
562
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
563
564
565
566
567
568
569
570
571
572
573
574
            ));
        }
    }

    public static function alnum($value, $message = '')
    {
        $locale = setlocale(LC_CTYPE, 0);
        setlocale(LC_CTYPE, 'C');
        $valid = !ctype_alnum($value);
        setlocale(LC_CTYPE, $locale);

        if ($valid) {
4b7c9c89   Thibault Ajas   maj installation.sh
575
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
576
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
577
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
578
579
580
581
582
583
584
585
586
587
588
589
            ));
        }
    }

    public static function lower($value, $message = '')
    {
        $locale = setlocale(LC_CTYPE, 0);
        setlocale(LC_CTYPE, 'C');
        $valid = !ctype_lower($value);
        setlocale(LC_CTYPE, $locale);

        if ($valid) {
4b7c9c89   Thibault Ajas   maj installation.sh
590
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
591
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
592
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
593
594
595
596
597
598
599
600
601
602
603
604
            ));
        }
    }

    public static function upper($value, $message = '')
    {
        $locale = setlocale(LC_CTYPE, 0);
        setlocale(LC_CTYPE, 'C');
        $valid = !ctype_upper($value);
        setlocale(LC_CTYPE, $locale);

        if ($valid) {
4b7c9c89   Thibault Ajas   maj installation.sh
605
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
606
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
607
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
608
609
610
611
612
613
            ));
        }
    }

    public static function length($value, $length, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
614
615
        if ($length !== static::strlen($value)) {
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
616
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
617
                static::valueToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
618
619
620
621
622
623
624
                $length
            ));
        }
    }

    public static function minLength($value, $min, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
625
626
        if (static::strlen($value) < $min) {
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
627
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
628
                static::valueToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
629
630
631
632
633
634
635
                $min
            ));
        }
    }

    public static function maxLength($value, $max, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
636
637
        if (static::strlen($value) > $max) {
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
638
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
639
                static::valueToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
640
641
642
643
644
645
646
                $max
            ));
        }
    }

    public static function lengthBetween($value, $min, $max, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
647
        $length = static::strlen($value);
196bf1b1   Etienne Pallier   modifs de droits ...
648
649

        if ($length < $min || $length > $max) {
4b7c9c89   Thibault Ajas   maj installation.sh
650
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
651
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
652
                static::valueToString($value),
196bf1b1   Etienne Pallier   modifs de droits ...
653
654
655
656
657
658
659
660
                $min,
                $max
            ));
        }
    }

    public static function fileExists($value, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
661
        static::string($value);
196bf1b1   Etienne Pallier   modifs de droits ...
662
663

        if (!file_exists($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
664
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
665
                $message ?: 'The file %s does not exist.',
4b7c9c89   Thibault Ajas   maj installation.sh
666
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
667
668
669
670
671
672
            ));
        }
    }

    public static function file($value, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
673
        static::fileExists($value, $message);
196bf1b1   Etienne Pallier   modifs de droits ...
674
675

        if (!is_file($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
676
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
677
                $message ?: 'The path %s is not a file.',
4b7c9c89   Thibault Ajas   maj installation.sh
678
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
679
680
681
682
683
684
            ));
        }
    }

    public static function directory($value, $message = '')
    {
4b7c9c89   Thibault Ajas   maj installation.sh
685
        static::fileExists($value, $message);
196bf1b1   Etienne Pallier   modifs de droits ...
686
687

        if (!is_dir($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
688
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
689
                $message ?: 'The path %s is no directory.',
4b7c9c89   Thibault Ajas   maj installation.sh
690
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
691
692
693
694
695
696
697
            ));
        }
    }

    public static function readable($value, $message = '')
    {
        if (!is_readable($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
698
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
699
                $message ?: 'The path %s is not readable.',
4b7c9c89   Thibault Ajas   maj installation.sh
700
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
701
702
703
704
705
706
707
            ));
        }
    }

    public static function writable($value, $message = '')
    {
        if (!is_writable($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
708
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
709
                $message ?: 'The path %s is not writable.',
4b7c9c89   Thibault Ajas   maj installation.sh
710
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
711
712
713
714
715
716
717
            ));
        }
    }

    public static function classExists($value, $message = '')
    {
        if (!class_exists($value)) {
4b7c9c89   Thibault Ajas   maj installation.sh
718
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
719
                $message ?: 'Expected an existing class name. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
720
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
721
722
723
724
725
726
727
            ));
        }
    }

    public static function subclassOf($value, $class, $message = '')
    {
        if (!is_subclass_of($value, $class)) {
4b7c9c89   Thibault Ajas   maj installation.sh
728
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
729
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
730
731
                static::valueToString($value),
                static::valueToString($class)
196bf1b1   Etienne Pallier   modifs de droits ...
732
733
734
735
736
737
738
            ));
        }
    }

    public static function implementsInterface($value, $interface, $message = '')
    {
        if (!in_array($interface, class_implements($value))) {
4b7c9c89   Thibault Ajas   maj installation.sh
739
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
740
                $message ?: 'Expected an implementation of %2$s. Got: %s',
4b7c9c89   Thibault Ajas   maj installation.sh
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
                static::valueToString($value),
                static::valueToString($interface)
            ));
        }
    }

    public static function propertyExists($classOrObject, $property, $message = '')
    {
        if (!property_exists($classOrObject, $property)) {
            static::reportInvalidArgument(sprintf(
                $message ?: 'Expected the property %s to exist.',
                static::valueToString($property)
            ));
        }
    }

    public static function propertyNotExists($classOrObject, $property, $message = '')
    {
        if (property_exists($classOrObject, $property)) {
            static::reportInvalidArgument(sprintf(
                $message ?: 'Expected the property %s to not exist.',
                static::valueToString($property)
            ));
        }
    }

    public static function methodExists($classOrObject, $method, $message = '')
    {
        if (!method_exists($classOrObject, $method)) {
            static::reportInvalidArgument(sprintf(
                $message ?: 'Expected the method %s to exist.',
                static::valueToString($method)
            ));
        }
    }

    public static function methodNotExists($classOrObject, $method, $message = '')
    {
        if (method_exists($classOrObject, $method)) {
            static::reportInvalidArgument(sprintf(
                $message ?: 'Expected the method %s to not exist.',
                static::valueToString($method)
196bf1b1   Etienne Pallier   modifs de droits ...
783
784
785
786
787
788
789
            ));
        }
    }

    public static function keyExists($array, $key, $message = '')
    {
        if (!array_key_exists($key, $array)) {
4b7c9c89   Thibault Ajas   maj installation.sh
790
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
791
                $message ?: 'Expected the key %s to exist.',
4b7c9c89   Thibault Ajas   maj installation.sh
792
                static::valueToString($key)
196bf1b1   Etienne Pallier   modifs de droits ...
793
794
795
796
797
798
799
            ));
        }
    }

    public static function keyNotExists($array, $key, $message = '')
    {
        if (array_key_exists($key, $array)) {
4b7c9c89   Thibault Ajas   maj installation.sh
800
            static::reportInvalidArgument(sprintf(
196bf1b1   Etienne Pallier   modifs de droits ...
801
                $message ?: 'Expected the key %s to not exist.',
4b7c9c89   Thibault Ajas   maj installation.sh
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
                static::valueToString($key)
            ));
        }
    }

    public static function count($array, $number, $message = '')
    {
        static::eq(
            count($array),
            $number,
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
        );
    }

    public static function uuid($value, $message = '')
    {
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);

        // The nil UUID is special form of UUID that is specified to have all
        // 128 bits set to zero.
        if ('00000000-0000-0000-0000-000000000000' === $value) {
            return;
        }

        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)) {
            static::reportInvalidArgument(sprintf(
                $message ?: 'Value %s is not a valid UUID.',
                static::valueToString($value)
196bf1b1   Etienne Pallier   modifs de droits ...
830
831
832
833
            ));
        }
    }

4b7c9c89   Thibault Ajas   maj installation.sh
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
    {
        static::string($class);

        $actual = 'none';
        try {
            $expression();
        } catch (Exception $e) {
            $actual = get_class($e);
            if ($e instanceof $class) {
                return;
            }
        } catch (Throwable $e) {
            $actual = get_class($e);
            if ($e instanceof $class) {
                return;
            }
        }

        static::reportInvalidArgument($message ?: sprintf(
            'Expected to throw "%s", got "%s"',
            $class,
            $actual
        ));
    }

196bf1b1   Etienne Pallier   modifs de droits ...
860
861
862
863
864
865
866
867
868
869
870
871
    public static function __callStatic($name, $arguments)
    {
        if ('nullOr' === substr($name, 0, 6)) {
            if (null !== $arguments[0]) {
                $method = lcfirst(substr($name, 6));
                call_user_func_array(array('static', $method), $arguments);
            }

            return;
        }

        if ('all' === substr($name, 0, 3)) {
4b7c9c89   Thibault Ajas   maj installation.sh
872
            static::isTraversable($arguments[0]);
196bf1b1   Etienne Pallier   modifs de droits ...
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939

            $method = lcfirst(substr($name, 3));
            $args = $arguments;

            foreach ($arguments[0] as $entry) {
                $args[0] = $entry;

                call_user_func_array(array('static', $method), $args);
            }

            return;
        }

        throw new BadMethodCallException('No such method: '.$name);
    }

    protected static function valueToString($value)
    {
        if (null === $value) {
            return 'null';
        }

        if (true === $value) {
            return 'true';
        }

        if (false === $value) {
            return 'false';
        }

        if (is_array($value)) {
            return 'array';
        }

        if (is_object($value)) {
            return get_class($value);
        }

        if (is_resource($value)) {
            return 'resource';
        }

        if (is_string($value)) {
            return '"'.$value.'"';
        }

        return (string) $value;
    }

    protected static function typeToString($value)
    {
        return is_object($value) ? get_class($value) : gettype($value);
    }

    protected static function strlen($value)
    {
        if (!function_exists('mb_detect_encoding')) {
            return strlen($value);
        }

        if (false === $encoding = mb_detect_encoding($value)) {
            return strlen($value);
        }

        return mb_strwidth($value, $encoding);
    }

4b7c9c89   Thibault Ajas   maj installation.sh
940
941
942
943
944
    protected static function reportInvalidArgument($message)
    {
        throw new InvalidArgumentException($message);
    }

196bf1b1   Etienne Pallier   modifs de droits ...
945
946
947
948
    private function __construct()
    {
    }
}