Completed
Pull Request — master (#155)
by Raphael
02:21
created

Assertion::alnum()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 2
nop 3
1
<?php
2
/**
3
 * Assert
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * If you did not receive a copy of the license and are unable to
10
 * obtain it through the world-wide-web, please send an email
11
 * to [email protected] so I can send you a copy immediately.
12
 */
13
14
namespace Assert;
15
16
use BadMethodCallException;
17
18
/**
19
 * Assert library
20
 *
21
 * @author Benjamin Eberlei <[email protected]>
22
 *
23
 * @method static void allAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric for all values.
24
 * @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths for all values.
25
 * @method static void allBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean for all values.
26
 * @method static void allChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices for all values.
27
 * @method static void allChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null) Determines if the values array has every choice as key and that this choice has content for all values.
28
 * @method static void allClassExists($value, $message = null, $propertyPath = null) Assert that the class exists for all values.
29
 * @method static void allContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars for all values.
30
 * @method static void allCount($countable, $count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count for all values.
31
 * @method static void allDate($value, $format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format for all values.
32
 * @method static void allDigit($value, $message = null, $propertyPath = null) Validates if an integer or integerish is a digit for all values.
33
 * @method static void allDirectory($value, $message = null, $propertyPath = null) Assert that a directory exists for all values.
34
 * @method static void allEmail($value, $message = null, $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL) for all values.
35
 * @method static void allEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars for all values.
36
 * @method static void allEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) for all values.
37
 * @method static void allFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False for all values.
38
 * @method static void allFile($value, $message = null, $propertyPath = null) Assert that a file exists for all values.
39
 * @method static void allFloat($value, $message = null, $propertyPath = null) Assert that value is a php float for all values.
40
 * @method static void allGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit for all values.
41
 * @method static void allGreaterThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit for all values.
42
 * @method static void allImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface for all values.
43
 * @method static void allInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} for all values.
44
 * @method static void allInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer for all values.
45
 * @method static void allIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish for all values.
46
 * @method static void allIsArray($value, $message = null, $propertyPath = null) Assert that value is an array for all values.
47
 * @method static void allIsArrayAccessible($value, $message = null, $propertyPath = null) Assert that value is an array or an array-accessible object for all values.
48
 * @method static void allIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable for all values.
49
 * @method static void allIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is instance of given class-name for all values.
50
 * @method static void allIsJsonString($value, $message = null, $propertyPath = null) Assert that the given string is a valid json string for all values.
51
 * @method static void allIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object for all values.
52
 * @method static void allIsTraversable($value, $message = null, $propertyPath = null) Assert that value is an array or a traversable object for all values.
53
 * @method static void allKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array for all values.
54
 * @method static void allKeyIsset($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() for all values.
55
 * @method static void allLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length for all values.
56
 * @method static void allLessOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit for all values.
57
 * @method static void allLessThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less than given limit for all values.
58
 * @method static void allMax($value, $maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit for all values.
59
 * @method static void allMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string value is not longer than $maxLength chars for all values.
60
 * @method static void allMethodExists($value, $object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object for all values.
61
 * @method static void allMin($value, $minValue, $message = null, $propertyPath = null) Assert that a value is at least as big as a given limit for all values.
62
 * @method static void allMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that a string is at least $minLength chars long for all values.
63
 * @method static void allNoContent($value, $message = null, $propertyPath = null) Assert that value is empty for all values.
64
 * @method static void allNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank for all values.
65
 * @method static void allNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty for all values.
66
 * @method static void allNotEmptyKey($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty for all values.
67
 * @method static void allNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) for all values.
68
 * @method static void allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name for all values.
69
 * @method static void allNotNull($value, $message = null, $propertyPath = null) Assert that value is not null for all values.
70
 * @method static void allNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) for all values.
71
 * @method static void allNumeric($value, $message = null, $propertyPath = null) Assert that value is numeric for all values.
72
 * @method static void allRange($value, $minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers for all values.
73
 * @method static void allReadable($value, $message = null, $propertyPath = null) Assert that the value is something readable for all values.
74
 * @method static void allRegex($value, $pattern, $message = null, $propertyPath = null) Assert that value matches a regex for all values.
75
 * @method static void allSame($value, $value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===) for all values.
76
 * @method static void allScalar($value, $message = null, $propertyPath = null) Assert that value is a PHP scalar for all values.
77
 * @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars for all values.
78
 * @method static void allString($value, $message = null, $propertyPath = null) Assert that value is a string for all values.
79
 * @method static void allSubclassOf($value, $className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name for all values.
80
 * @method static void allTrue($value, $message = null, $propertyPath = null) Assert that the value is boolean True for all values.
81
 * @method static void allUrl($value, $message = null, $propertyPath = null) Assert that value is an URL for all values.
82
 * @method static void allUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID for all values.
83
 * @method static void allWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable for all values.
84
 * @method static void nullOrAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric or that the value is null.
85
 * @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths or that the value is null.
86
 * @method static void nullOrBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean or that the value is null.
87
 * @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices or that the value is null.
88
 * @method static void nullOrChoicesNotEmpty($values, $choices, $message = null, $propertyPath = null) Determines if the values array has every choice as key and that this choice has content or that the value is null.
89
 * @method static void nullOrClassExists($value, $message = null, $propertyPath = null) Assert that the class exists or that the value is null.
90
 * @method static void nullOrContains($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars or that the value is null.
91
 * @method static void nullOrCount($countable, $count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count or that the value is null.
92
 * @method static void nullOrDate($value, $format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format or that the value is null.
93
 * @method static void nullOrDigit($value, $message = null, $propertyPath = null) Validates if an integer or integerish is a digit or that the value is null.
94
 * @method static void nullOrDirectory($value, $message = null, $propertyPath = null) Assert that a directory exists or that the value is null.
95
 * @method static void nullOrEmail($value, $message = null, $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL) or that the value is null.
96
 * @method static void nullOrEndsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars or that the value is null.
97
 * @method static void nullOrEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) or that the value is null.
98
 * @method static void nullOrFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False or that the value is null.
99
 * @method static void nullOrFile($value, $message = null, $propertyPath = null) Assert that a file exists or that the value is null.
100
 * @method static void nullOrFloat($value, $message = null, $propertyPath = null) Assert that value is a php float or that the value is null.
101
 * @method static void nullOrGreaterOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit or that the value is null.
102
 * @method static void nullOrGreaterThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit or that the value is null.
103
 * @method static void nullOrImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface or that the value is null.
104
 * @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} or that the value is null.
105
 * @method static void nullOrInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer or that the value is null.
106
 * @method static void nullOrIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish or that the value is null.
107
 * @method static void nullOrIsArray($value, $message = null, $propertyPath = null) Assert that value is an array or that the value is null.
108
 * @method static void nullOrIsArrayAccessible($value, $message = null, $propertyPath = null) Assert that value is an array or an array-accessible object or that the value is null.
109
 * @method static void nullOrIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable or that the value is null.
110
 * @method static void nullOrIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is instance of given class-name or that the value is null.
111
 * @method static void nullOrIsJsonString($value, $message = null, $propertyPath = null) Assert that the given string is a valid json string or that the value is null.
112
 * @method static void nullOrIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object or that the value is null.
113
 * @method static void nullOrIsTraversable($value, $message = null, $propertyPath = null) Assert that value is an array or a traversable object or that the value is null.
114
 * @method static void nullOrKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array or that the value is null.
115
 * @method static void nullOrKeyIsset($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() or that the value is null.
116
 * @method static void nullOrLength($value, $length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length or that the value is null.
117
 * @method static void nullOrLessOrEqualThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit or that the value is null.
118
 * @method static void nullOrLessThan($value, $limit, $message = null, $propertyPath = null) Determines if the value is less than given limit or that the value is null.
119
 * @method static void nullOrMax($value, $maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit or that the value is null.
120
 * @method static void nullOrMaxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string value is not longer than $maxLength chars or that the value is null.
121
 * @method static void nullOrMethodExists($value, $object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object or that the value is null.
122
 * @method static void nullOrMin($value, $minValue, $message = null, $propertyPath = null) Assert that a value is at least as big as a given limit or that the value is null.
123
 * @method static void nullOrMinLength($value, $minLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that a string is at least $minLength chars long or that the value is null.
124
 * @method static void nullOrNoContent($value, $message = null, $propertyPath = null) Assert that value is empty or that the value is null.
125
 * @method static void nullOrNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank or that the value is null.
126
 * @method static void nullOrNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty or that the value is null.
127
 * @method static void nullOrNotEmptyKey($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty or that the value is null.
128
 * @method static void nullOrNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
129
 * @method static void nullOrNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name or that the value is null.
130
 * @method static void nullOrNotNull($value, $message = null, $propertyPath = null) Assert that value is not null or that the value is null.
131
 * @method static void nullOrNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) or that the value is null.
132
 * @method static void nullOrNumeric($value, $message = null, $propertyPath = null) Assert that value is numeric or that the value is null.
133
 * @method static void nullOrRange($value, $minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers or that the value is null.
134
 * @method static void nullOrReadable($value, $message = null, $propertyPath = null) Assert that the value is something readable or that the value is null.
135
 * @method static void nullOrRegex($value, $pattern, $message = null, $propertyPath = null) Assert that value matches a regex or that the value is null.
136
 * @method static void nullOrSame($value, $value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===) or that the value is null.
137
 * @method static void nullOrScalar($value, $message = null, $propertyPath = null) Assert that value is a PHP scalar or that the value is null.
138
 * @method static void nullOrStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars or that the value is null.
139
 * @method static void nullOrString($value, $message = null, $propertyPath = null) Assert that value is a string or that the value is null.
140
 * @method static void nullOrSubclassOf($value, $className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name or that the value is null.
141
 * @method static void nullOrTrue($value, $message = null, $propertyPath = null) Assert that the value is boolean True or that the value is null.
142
 * @method static void nullOrUrl($value, $message = null, $propertyPath = null) Assert that value is an URL or that the value is null.
143
 * @method static void nullOrUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID or that the value is null.
144
 * @method static void nullOrWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable or that the value is null.
145
 */
146
class Assertion
147
{
148
    const INVALID_FLOAT             = 9;
149
    const INVALID_INTEGER           = 10;
150
    const INVALID_DIGIT             = 11;
151
    const INVALID_INTEGERISH        = 12;
152
    const INVALID_BOOLEAN           = 13;
153
    const VALUE_EMPTY               = 14;
154
    const VALUE_NULL                = 15;
155
    const INVALID_STRING            = 16;
156
    const INVALID_REGEX             = 17;
157
    const INVALID_MIN_LENGTH        = 18;
158
    const INVALID_MAX_LENGTH        = 19;
159
    const INVALID_STRING_START      = 20;
160
    const INVALID_STRING_CONTAINS   = 21;
161
    const INVALID_CHOICE            = 22;
162
    const INVALID_NUMERIC           = 23;
163
    const INVALID_ARRAY             = 24;
164
    const INVALID_KEY_EXISTS        = 26;
165
    const INVALID_NOT_BLANK         = 27;
166
    const INVALID_INSTANCE_OF       = 28;
167
    const INVALID_SUBCLASS_OF       = 29;
168
    const INVALID_RANGE             = 30;
169
    const INVALID_ALNUM             = 31;
170
    const INVALID_TRUE              = 32;
171
    const INVALID_EQ                = 33;
172
    const INVALID_SAME              = 34;
173
    const INVALID_MIN               = 35;
174
    const INVALID_MAX               = 36;
175
    const INVALID_LENGTH            = 37;
176
    const INVALID_FALSE             = 38;
177
    const INVALID_STRING_END        = 39;
178
    const INVALID_UUID              = 40;
179
    const INVALID_COUNT             = 41;
180
    const INVALID_NOT_EQ            = 42;
181
    const INVALID_NOT_SAME          = 43;
182
    const INVALID_TRAVERSABLE       = 44;
183
    const INVALID_ARRAY_ACCESSIBLE  = 45;
184
    const INVALID_KEY_ISSET         = 46;
185
    const INVALID_VALUE_IN_ARRAY    = 47;
186
    const INVALID_DIRECTORY         = 101;
187
    const INVALID_FILE              = 102;
188
    const INVALID_READABLE          = 103;
189
    const INVALID_WRITEABLE         = 104;
190
    const INVALID_CLASS             = 105;
191
    const INVALID_EMAIL             = 201;
192
    const INTERFACE_NOT_IMPLEMENTED = 202;
193
    const INVALID_URL               = 203;
194
    const INVALID_NOT_INSTANCE_OF   = 204;
195
    const VALUE_NOT_EMPTY           = 205;
196
    const INVALID_JSON_STRING       = 206;
197
    const INVALID_OBJECT            = 207;
198
    const INVALID_METHOD            = 208;
199
    const INVALID_SCALAR            = 209;
200
    const INVALID_LESS              = 210;
201
    const INVALID_LESS_OR_EQUAL     = 211;
202
    const INVALID_GREATER           = 212;
203
    const INVALID_GREATER_OR_EQUAL  = 212;
204
    const INVALID_DATE              = 213;
205
    const INVALID_CALLABLE          = 214;
206
207
    /**
208
     * Exception to throw when an assertion failed.
209
     *
210
     * @var string
211
     */
212
    static protected $exceptionClass = 'Assert\InvalidArgumentException';
213
214
    /**
215
     * Helper method that handles building the assertion failure exceptions.
216
     * They are returned from this method so that the stack trace still shows
217
     * the assertions method.
218
     */
219
    protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
220
    {
221
        $exceptionClass = static::$exceptionClass;
222
        return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
223
    }
224
225
    /**
226
     * Assert that two values are equal (using == ).
227
     *
228
     * @param mixed $value
229
     * @param mixed $value2
230
     * @param string|null $message
231
     * @param string|null $propertyPath
232
     * @return void
233
     * @throws \Assert\AssertionFailedException
234
     */
235
    public static function eq($value, $value2, $message = null, $propertyPath = null)
236
    {
237
        if ($value != $value2) {
238
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
239
                $message ?: 'Value "%s" does not equal expected value "%s".',
240
                self::stringify($value),
241
                self::stringify($value2)
242
            );
243
244
            throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
245
        }
246
    }
247
248
    /**
249
     * Assert that two values are the same (using ===).
250
     *
251
     * @param mixed $value
252
     * @param mixed $value2
253
     * @param string|null $message
254
     * @param string|null $propertyPath
255
     * @return void
256
     * @throws \Assert\AssertionFailedException
257
     */
258
    public static function same($value, $value2, $message = null, $propertyPath = null)
259
    {
260
        if ($value !== $value2) {
261
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
262
                $message ?: 'Value "%s" is not the same as expected value "%s".',
263
                self::stringify($value),
264
                self::stringify($value2)
265
            );
266
267
            throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
268
        }
269
    }
270
271
    /**
272
     * Assert that two values are not equal (using == ).
273
     *
274
     * @param mixed $value1
275
     * @param mixed $value2
276
     * @param string|null $message
277
     * @param string|null $propertyPath
278
     * @return void
279
     * @throws \Assert\AssertionFailedException
280
     */
281
    public static function notEq($value1, $value2, $message = null, $propertyPath = null)
282
    {
283
        if ($value1 == $value2) {
284
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
285
                $message ?: 'Value "%s" is equal to expected value "%s".',
286
                self::stringify($value1),
287
                self::stringify($value2)
288
            );
289
            throw static::createException($value1, $message,static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
290
        }
291
    }
292
293
    /**
294
     * Assert that two values are not the same (using === ).
295
     *
296
     * @param mixed $value1
297
     * @param mixed $value2
298
     * @param string|null $message
299
     * @param string|null $propertyPath
300
     * @return void
301
     * @throws \Assert\AssertionFailedException
302
     */
303
    public static function notSame($value1, $value2, $message = null, $propertyPath = null)
304
    {
305
        if ($value1 === $value2) {
306
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
307
                $message ?: 'Value "%s" is the same as expected value "%s".',
308
                self::stringify($value1),
309
                self::stringify($value2)
310
            );
311
            throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
312
        }
313
    }
314
315
    /**
316
     * Assert that value is not in array of choices.
317
     *
318
     * @param mixed $value1
319
     * @param array $choices
320
     * @param string|null $message
321
     * @param string|null $propertyPath
322
     * @return void
323
     * @throws \Assert\AssertionFailedException
324
     */
325
    public static function notInArray($value1, array $choices, $message = null, $propertyPath = null)
326
    {
327
        if (in_array($value1, $choices) === true) {
328
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
329
                $message ?: 'Value "%s" is in given "%s".',
330
                self::stringify($value1),
331
                self::stringify($choices)
332
            );
333
            throw static::createException($value1, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath);
334
        }
335
    }
336
337
    /**
338
     * Assert that value is a php integer.
339
     *
340
     * @param mixed $value
341
     * @param string|null $message
342
     * @param string|null $propertyPath
343
     * @return void
344
     * @throws \Assert\AssertionFailedException
345
     */
346
    public static function integer($value, $message = null, $propertyPath = null)
347
    {
348
        if ( ! is_int($value)) {
349
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
350
                $message ?: 'Value "%s" is not an integer.',
351
                self::stringify($value)
352
            );
353
354
            throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
355
        }
356
    }
357
358
    /**
359
     * Assert that value is a php float.
360
     *
361
     * @param mixed $value
362
     * @param string|null $message
363
     * @param string|null $propertyPath
364
     * @return void
365
     * @throws \Assert\AssertionFailedException
366
     */
367
    public static function float($value, $message = null, $propertyPath = null)
368
    {
369
        if ( ! is_float($value)) {
370
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
371
                $message ?: 'Value "%s" is not a float.',
372
                self::stringify($value)
373
            );
374
375
            throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
376
        }
377
    }
378
379
    /**
380
     * Validates if an integer or integerish is a digit.
381
     *
382
     * @param mixed $value
383
     * @param string|null $message
384
     * @param string|null $propertyPath
385
     * @return void
386
     * @throws \Assert\AssertionFailedException
387
     */
388
    public static function digit($value, $message = null, $propertyPath = null)
389
    {
390
        if ( ! ctype_digit((string)$value)) {
391
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
392
                $message ?: 'Value "%s" is not a digit.',
393
                self::stringify($value)
394
            );
395
396
            throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
397
        }
398
    }
399
400
    /**
401
     * Assert that value is a php integer'ish.
402
     *
403
     * @param mixed $value
404
     * @param string|null $message
405
     * @param string|null $propertyPath
406
     * @return void
407
     * @throws \Assert\AssertionFailedException
408
     */
409
    public static function integerish($value, $message = null, $propertyPath = null)
410
    {
411
        if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
412
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
413
                $message ?: 'Value "%s" is not an integer or a number castable to integer.',
414
                self::stringify($value)
415
            );
416
417
            throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
418
        }
419
    }
420
421
    /**
422
     * Assert that value is php boolean
423
     *
424
     * @param mixed $value
425
     * @param string|null $message
426
     * @param string|null $propertyPath
427
     * @return void
428
     * @throws \Assert\AssertionFailedException
429
     */
430
    public static function boolean($value, $message = null, $propertyPath = null)
431
    {
432
        if ( ! is_bool($value)) {
433
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
434
                $message ?: 'Value "%s" is not a boolean.',
435
                self::stringify($value)
436
            );
437
438
            throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
439
        }
440
    }
441
442
    /**
443
     * Assert that value is a PHP scalar
444
     *
445
     * @param mixed $value
446
     * @param string|null $message
447
     * @param string|null $propertyPath
448
     * @return void
449
     * @throws \Assert\AssertionFailedException
450
     */
451
    public static function scalar($value, $message = null, $propertyPath = null)
452
    {
453
        if (!is_scalar($value)) {
454
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
455
                $message ?: 'Value "%s" is not a scalar.',
456
                self::stringify($value)
457
            );
458
459
            throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
460
        }
461
    }
462
463
    /**
464
     * Assert that value is not empty
465
     *
466
     * @param mixed $value
467
     * @param string|null $message
468
     * @param string|null $propertyPath
469
     * @return void
470
     * @throws \Assert\AssertionFailedException
471
     */
472
    public static function notEmpty($value, $message = null, $propertyPath = null)
473
    {
474
        if (empty($value)) {
475
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
476
                $message ?: 'Value "%s" is empty, but non empty value was expected.',
477
                self::stringify($value)
478
            );
479
480
            throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
481
        }
482
    }
483
484
    /**
485
     * Assert that value is empty
486
     *
487
     * @param mixed $value
488
     * @param string|null $message
489
     * @param string|null $propertyPath
490
     * @return void
491
     * @throws \Assert\AssertionFailedException
492
     */
493
    public static function noContent($value, $message = null, $propertyPath = null)
494
    {
495
        if (!empty($value)) {
496
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
497
                $message ?: 'Value "%s" is not empty, but empty value was expected.',
498
                self::stringify($value)
499
            );
500
501
            throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
502
        }
503
    }
504
505
    /**
506
     * Assert that value is not null
507
     *
508
     * @param mixed $value
509
     * @param string|null $message
510
     * @param string|null $propertyPath
511
     * @return void
512
     * @throws \Assert\AssertionFailedException
513
     */
514
    public static function notNull($value, $message = null, $propertyPath = null)
515
    {
516
        if ($value === null) {
517
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
518
                $message ?: 'Value "%s" is null, but non null value was expected.',
519
                self::stringify($value)
520
            );
521
522
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
523
        }
524
    }
525
526
    /**
527
     * Assert that value is a string
528
     *
529
     * @param mixed $value
530
     * @param string|null $message
531
     * @param string|null $propertyPath
532
     * @return void
533
     * @throws \Assert\AssertionFailedException
534
     */
535
    public static function string($value, $message = null, $propertyPath = null)
536
    {
537
        if ( ! is_string($value)) {
538
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
539
                $message ?: 'Value "%s" expected to be string, type %s given.',
540
                self::stringify($value),
541
                gettype($value)
542
            );
543
544
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
545
        }
546
    }
547
548
    /**
549
     * Assert that value matches a regex
550
     *
551
     * @param mixed $value
552
     * @param string $pattern
553
     * @param string|null $message
554
     * @param string|null $propertyPath
555
     * @return void
556
     * @throws \Assert\AssertionFailedException
557
     */
558
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
559
    {
560
        static::string($value, $message, $propertyPath);
561
562
        if ( ! preg_match($pattern, $value)) {
563
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
564
                $message ?: 'Value "%s" does not match expression.',
565
                self::stringify($value)
566
            );
567
568
            throw static::createException($value, $message, static::INVALID_REGEX , $propertyPath, array('pattern' => $pattern));
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
569
        }
570
    }
571
572
    /**
573
     * Assert that string has a given length.
574
     *
575
     * @param mixed $value
576
     * @param int $length
577
     * @param string|null $message
578
     * @param string|null $propertyPath
579
     * @param string $encoding
580
     * @return void
581
     * @throws \Assert\AssertionFailedException
582
     */
583
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
584
    {
585
        static::string($value, $message, $propertyPath);
586
587
        if (mb_strlen($value, $encoding) !== $length) {
588
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
589
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
590
                self::stringify($value),
591
                $length,
592
                mb_strlen($value, $encoding)
593
            );
594
595
            $constraints = array('length' => $length, 'encoding' => $encoding);
596
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
597
        }
598
    }
599
600
    /**
601
     * Assert that a string is at least $minLength chars long.
602
     *
603
     * @param mixed $value
604
     * @param int $minLength
605
     * @param string|null $message
606
     * @param string|null $propertyPath
607
     * @param string $encoding
608
     * @return void
609
     * @throws \Assert\AssertionFailedException
610
     */
611
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
612
    {
613
        static::string($value, $message, $propertyPath);
614
615
        if (mb_strlen($value, $encoding) < $minLength) {
616
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
617
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
618
                self::stringify($value),
619
                $minLength,
620
                mb_strlen($value, $encoding)
621
            );
622
623
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
624
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
625
        }
626
    }
627
628
    /**
629
     * Assert that string value is not longer than $maxLength chars.
630
     *
631
     * @param mixed $value
632
     * @param integer $maxLength
633
     * @param string|null $message
634
     * @param string|null $propertyPath
635
     * @param string $encoding
636
     * @return void
637
     * @throws \Assert\AssertionFailedException
638
     */
639
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
640
    {
641
        static::string($value, $message, $propertyPath);
642
643
        if (mb_strlen($value, $encoding) > $maxLength) {
644
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
645
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
646
                self::stringify($value),
647
                $maxLength,
648
                mb_strlen($value, $encoding)
649
            );
650
651
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
652
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
653
        }
654
    }
655
656
    /**
657
     * Assert that string length is between min,max lengths.
658
     *
659
     * @param mixed $value
660
     * @param integer $minLength
661
     * @param integer $maxLength
662
     * @param string|null $message
663
     * @param string|null $propertyPath
664
     * @param string $encoding
665
     * @return void
666
     * @throws \Assert\AssertionFailedException
667
     */
668
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
669
    {
670
        static::string($value, $message, $propertyPath);
671
672
        if (mb_strlen($value, $encoding) < $minLength) {
673
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
674
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
675
                self::stringify($value),
676
                $minLength,
677
                mb_strlen($value, $encoding)
678
            );
679
680
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
681
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
682
        }
683
684
        if (mb_strlen($value, $encoding) > $maxLength) {
685
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
686
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
687
                self::stringify($value),
688
                $maxLength,
689
                mb_strlen($value, $encoding)
690
            );
691
692
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
693
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
694
        }
695
    }
696
697
    /**
698
     * Assert that string starts with a sequence of chars.
699
     *
700
     * @param mixed $string
701
     * @param string $needle
702
     * @param string|null $message
703
     * @param string|null $propertyPath
704
     * @param string $encoding
705
     * @return void
706
     * @throws \Assert\AssertionFailedException
707
     */
708
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
709
    {
710
        static::string($string, $message, $propertyPath);
711
712
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
713
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
714
                $message ?: 'Value "%s" does not start with "%s".',
715
                self::stringify($string),
716
                self::stringify($needle)
717
            );
718
719
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
720
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
721
        }
722
    }
723
724
    /**
725
     * Assert that string ends with a sequence of chars.
726
     *
727
     * @param mixed $string
728
     * @param string $needle
729
     * @param string|null $message
730
     * @param string|null $propertyPath
731
     * @param string $encoding
732
     * @return void
733
     * @throws \Assert\AssertionFailedException
734
     */
735
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
736
    {
737
        static::string($string, $message, $propertyPath);
738
739
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
740
741
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
742
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
743
                $message ?: 'Value "%s" does not end with "%s".',
744
                self::stringify($string),
745
                self::stringify($needle)
746
            );
747
748
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
749
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
750
        }
751
    }
752
753
    /**
754
     * Assert that string contains a sequence of chars.
755
     *
756
     * @param mixed $string
757
     * @param string $needle
758
     * @param string|null $message
759
     * @param string|null $propertyPath
760
     * @param string $encoding
761
     * @return void
762
     * @throws \Assert\AssertionFailedException
763
     */
764
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
765
    {
766
        static::string($string, $message, $propertyPath);
767
768
        if (mb_strpos($string, $needle, null, $encoding) === false) {
769
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
770
                $message ?: 'Value "%s" does not contain "%s".',
771
                self::stringify($string),
772
                self::stringify($needle)
773
            );
774
775
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
776
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
777
        }
778
    }
779
780
    /**
781
     * Assert that value is in array of choices.
782
     *
783
     * @param mixed $value
784
     * @param array $choices
785
     * @param string|null $message
786
     * @param string|null $propertyPath
787
     * @return void
788
     * @throws \Assert\AssertionFailedException
789
     */
790
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
791
    {
792
        if ( ! in_array($value, $choices, true)) {
793
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
794
                $message ?: 'Value "%s" is not an element of the valid values: %s',
795
                self::stringify($value),
796
                implode(", ", array_map('Assert\Assertion::stringify', $choices))
797
            );
798
799
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
800
        }
801
    }
802
803
    /**
804
     * Alias of {@see choice()}
805
     *
806
     * @throws \Assert\AssertionFailedException
807
     */
808
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
809
    {
810
        static::choice($value, $choices, $message, $propertyPath);
811
    }
812
813
    /**
814
     * Assert that value is numeric.
815
     *
816
     * @param mixed $value
817
     * @param string|null $message
818
     * @param string|null $propertyPath
819
     * @return void
820
     * @throws \Assert\AssertionFailedException
821
     */
822
    public static function numeric($value, $message = null, $propertyPath = null)
823
    {
824
        if ( ! is_numeric($value)) {
825
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
826
                $message ?: 'Value "%s" is not numeric.',
827
                self::stringify($value)
828
            );
829
830
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
831
        }
832
    }
833
834
    /**
835
     * Assert that value is an array.
836
     *
837
     * @param mixed $value
838
     * @param string|null $message
839
     * @param string|null $propertyPath
840
     * @return void
841
     * @throws \Assert\AssertionFailedException
842
     */
843
    public static function isArray($value, $message = null, $propertyPath = null)
844
    {
845
        if ( ! is_array($value)) {
846
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
847
                $message ?: 'Value "%s" is not an array.',
848
                self::stringify($value)
849
            );
850
851
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
852
        }
853
    }
854
855
    /**
856
     * Assert that value is an array or a traversable object.
857
     *
858
     * @param mixed $value
859
     * @param string|null $message
860
     * @param string|null $propertyPath
861
     * @return void
862
     * @throws \Assert\AssertionFailedException
863
     */
864
    public static function isTraversable($value, $message = null, $propertyPath = null)
865
    {
866
        if ( ! is_array($value) && ! $value instanceof \Traversable) {
867
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
868
                $message ?: 'Value "%s" is not an array and does not implement Traversable.',
869
                self::stringify($value)
870
            );
871
872
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
873
        }
874
    }
875
876
    /**
877
     * Assert that value is an array or an array-accessible object.
878
     *
879
     * @param mixed $value
880
     * @param string|null $message
881
     * @param string|null $propertyPath
882
     * @return void
883
     * @throws \Assert\AssertionFailedException
884
     */
885
    public static function isArrayAccessible($value, $message = null, $propertyPath = null)
886
    {
887
        if ( ! is_array($value) && ! $value instanceof \ArrayAccess) {
888
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
889
                $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
890
                self::stringify($value)
891
            );
892
893
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
894
        }
895
    }
896
897
    /**
898
     * Assert that key exists in an array
899
     *
900
     * @param mixed $value
901
     * @param string|integer $key
902
     * @param string|null $message
903
     * @param string|null $propertyPath
904
     * @return void
905
     * @throws \Assert\AssertionFailedException
906
     */
907
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
908
    {
909
        static::isArray($value, $message, $propertyPath);
910
911
        if ( ! array_key_exists($key, $value)) {
912
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
913
                $message ?: 'Array does not contain an element with key "%s"',
914
                self::stringify($key)
915
            );
916
917
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
918
        }
919
    }
920
921
    /**
922
     * Assert that key exists in an array/array-accessible object using isset()
923
     *
924
     * @param mixed $value
925
     * @param string|integer $key
926
     * @param string|null $message
927
     * @param string|null $propertyPath
928
     * @return void
929
     * @throws \Assert\AssertionFailedException
930
     */
931
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
932
    {
933
        static::isArrayAccessible($value, $message, $propertyPath);
934
935
        if ( ! isset($value[$key])) {
936
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
937
                $message ?: 'The element with key "%s" was not found',
938
                self::stringify($key)
939
            );
940
941
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
942
        }
943
    }
944
945
    /**
946
     * Assert that key exists in an array/array-accessible object and it's value is not empty.
947
     *
948
     * @param mixed $value
949
     * @param string|integer $key
950
     * @param string|null $message
951
     * @param string|null $propertyPath
952
     * @return void
953
     * @throws \Assert\AssertionFailedException
954
     */
955
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
956
    {
957
        static::keyIsset($value, $key, $message, $propertyPath);
958
        static::notEmpty($value[$key], $message, $propertyPath);
959
    }
960
961
    /**
962
     * Assert that value is not blank
963
     *
964
     * @param mixed $value
965
     * @param string|null $message
966
     * @param string|null $propertyPath
967
     * @return void
968
     * @throws \Assert\AssertionFailedException
969
     */
970
    public static function notBlank($value, $message = null, $propertyPath = null)
971
    {
972
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
973
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
974
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
975
                self::stringify($value)
976
            );
977
978
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
979
        }
980
    }
981
982
    /**
983
     * Assert that value is instance of given class-name.
984
     *
985
     * @param mixed $value
986
     * @param string $className
987
     * @param string|null $message
988
     * @param string|null $propertyPath
989
     * @return void
990
     * @throws \Assert\AssertionFailedException
991
     */
992
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
993
    {
994
        if ( ! ($value instanceof $className)) {
995
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
996
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
997
                self::stringify($value),
998
                $className
999
            );
1000
1001
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
1002
        }
1003
    }
1004
1005
    /**
1006
     * Assert that value is not instance of given class-name.
1007
     *
1008
     * @param mixed $value
1009
     * @param string $className
1010
     * @param string|null $message
1011
     * @param string|null $propertyPath
1012
     * @return void
1013
     * @throws \Assert\AssertionFailedException
1014
     */
1015
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
1016
    {
1017
        if ($value instanceof $className) {
1018
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1019
                $message ?: 'Class "%s" was not expected to be instanceof of "%s".',
1020
                self::stringify($value),
1021
                $className
1022
            );
1023
1024
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1025
        }
1026
    }
1027
1028
    /**
1029
     * Assert that value is subclass of given class-name.
1030
     *
1031
     * @param mixed $value
1032
     * @param string $className
1033
     * @param string|null $message
1034
     * @param string|null $propertyPath
1035
     * @return void
1036
     * @throws \Assert\AssertionFailedException
1037
     */
1038
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1039
    {
1040
        if ( ! is_subclass_of($value, $className)) {
1041
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1042
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1043
                self::stringify($value),
1044
                $className
1045
            );
1046
1047
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1048
        }
1049
    }
1050
1051
    /**
1052
     * Assert that value is in range of numbers.
1053
     *
1054
     * @param mixed $value
1055
     * @param integer $minValue
1056
     * @param integer $maxValue
1057
     * @param string|null $message
1058
     * @param string|null $propertyPath
1059
     * @return void
1060
     * @throws \Assert\AssertionFailedException
1061
     */
1062
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1063
    {
1064
        static::numeric($value, $message, $propertyPath);
1065
1066
        if ($value < $minValue || $value > $maxValue) {
1067
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1068
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1069
                self::stringify($value),
1070
                self::stringify($minValue),
1071
                self::stringify($maxValue)
1072
            );
1073
1074
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1075
        }
1076
    }
1077
1078
    /**
1079
     * Assert that a value is at least as big as a given limit
1080
     *
1081
     * @param mixed $value
1082
     * @param mixed $minValue
1083
     * @param string|null $message
1084
     * @param string|null $propertyPath
1085
     * @return void
1086
     * @throws \Assert\AssertionFailedException
1087
     */
1088
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1089
    {
1090
        static::numeric($value, $message, $propertyPath);
1091
1092
        if ($value < $minValue) {
1093
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1094
                $message ?: 'Number "%s" was expected to be at least "%d".',
1095
                self::stringify($value),
1096
                self::stringify($minValue)
1097
            );
1098
1099
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1100
        }
1101
    }
1102
1103
    /**
1104
     * Assert that a number is smaller as a given limit
1105
     *
1106
     * @param mixed $value
1107
     * @param mixed $maxValue
1108
     * @param string|null $message
1109
     * @param string|null $propertyPath
1110
     * @return void
1111
     * @throws \Assert\AssertionFailedException
1112
     */
1113
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1114
    {
1115
        static::numeric($value, $message, $propertyPath);
1116
1117
        if ($value > $maxValue) {
1118
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1119
                $message ?: 'Number "%s" was expected to be at most "%d".',
1120
                self::stringify($value),
1121
                self::stringify($maxValue)
1122
            );
1123
1124
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1125
        }
1126
    }
1127
1128
    /**
1129
     * Assert that a file exists
1130
     *
1131
     * @param string $value
1132
     * @param string|null $message
1133
     * @param string|null $propertyPath
1134
     * @return void
1135
     * @throws \Assert\AssertionFailedException
1136
     */
1137
    public static function file($value, $message = null, $propertyPath = null)
1138
    {
1139
        static::string($value, $message, $propertyPath);
1140
        static::notEmpty($value, $message, $propertyPath);
1141
1142
        if ( ! is_file($value)) {
1143
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1144
                $message ?: 'File "%s" was expected to exist.',
1145
                self::stringify($value)
1146
            );
1147
1148
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1149
        }
1150
    }
1151
1152
    /**
1153
     * Assert that a directory exists
1154
     *
1155
     * @param string $value
1156
     * @param string|null $message
1157
     * @param string|null $propertyPath
1158
     * @return void
1159
     * @throws \Assert\AssertionFailedException
1160
     */
1161
    public static function directory($value, $message = null, $propertyPath = null)
1162
    {
1163
        static::string($value, $message, $propertyPath);
1164
1165
        if ( ! is_dir($value)) {
1166
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1167
                $message ?: 'Path "%s" was expected to be a directory.',
1168
                self::stringify($value)
1169
            );
1170
1171
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1172
        }
1173
    }
1174
1175
    /**
1176
     * Assert that the value is something readable
1177
     *
1178
     * @param string $value
1179
     * @param string|null $message
1180
     * @param string|null $propertyPath
1181
     * @return void
1182
     * @throws \Assert\AssertionFailedException
1183
     */
1184
    public static function readable($value, $message = null, $propertyPath = null)
1185
    {
1186
        static::string($value, $message, $propertyPath);
1187
1188
        if ( ! is_readable($value)) {
1189
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1190
                $message ?: 'Path "%s" was expected to be readable.',
1191
                self::stringify($value)
1192
            );
1193
1194
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1195
        }
1196
    }
1197
1198
    /**
1199
     * Assert that the value is something writeable
1200
     *
1201
     * @param string $value
1202
     * @param string|null $message
1203
     * @param string|null $propertyPath
1204
     * @return void
1205
     * @throws \Assert\AssertionFailedException
1206
     */
1207
    public static function writeable($value, $message = null, $propertyPath = null)
1208
    {
1209
        static::string($value, $message, $propertyPath);
1210
1211
        if ( ! is_writeable($value)) {
1212
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1213
                $message ?: 'Path "%s" was expected to be writeable.',
1214
                self::stringify($value)
1215
            );
1216
1217
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1218
        }
1219
    }
1220
1221
    /**
1222
     * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL).
1223
     *
1224
     * @param mixed $value
1225
     * @param string|null $message
1226
     * @param string|null $propertyPath
1227
     * @return void
1228
     * @throws \Assert\AssertionFailedException
1229
     */
1230
    public static function email($value, $message = null, $propertyPath = null)
1231
    {
1232
        static::string($value, $message, $propertyPath);
1233
1234
        if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1235
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1236
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1237
                self::stringify($value)
1238
            );
1239
1240
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1241
        } else {
1242
            $host = substr($value, strpos($value, '@') + 1);
1243
1244
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1245
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1246
                $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1247
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1248
                    self::stringify($value)
1249
                );
1250
1251
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1252
            }
1253
        }
1254
    }
1255
1256
    /**
1257
     * Assert that value is an URL.
1258
     *
1259
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1260
     *
1261
     * @param mixed $value
1262
     * @param string|null $message
1263
     * @param string|null $propertyPath
1264
     * @return void
1265
     * @throws \Assert\AssertionFailedException
1266
     *
1267
     *
1268
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1269
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1270
     */
1271
    public static function url($value, $message = null, $propertyPath = null)
1272
    {
1273
        static::string($value, $message, $propertyPath);
1274
1275
        $protocols = array('http', 'https');
1276
1277
        $pattern = '~^
1278
            (%s)://                                 # protocol
1279
            (
1280
                ([\pL\pN\pS-]+\.)+[\pL]+                   # a domain name
1281
                    |                                     #  or
1282
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}      # a IP address
1283
                    |                                     #  or
1284
                \[
1285
                    (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
1286
                \]  # a IPv6 address
1287
            )
1288
            (:[0-9]+)?                              # a port (optional)
1289
            (/?|/\S+)                               # a /, nothing or a / with something
1290
        $~ixu';
1291
1292
        $pattern = sprintf($pattern, implode('|', $protocols));
1293
1294
        if (!preg_match($pattern, $value)) {
1295
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1296
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1297
                self::stringify($value)
1298
            );
1299
1300
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1301
        }
1302
1303
    }
1304
1305
    /**
1306
     * Assert that value is alphanumeric.
1307
     *
1308
     * @param mixed $value
1309
     * @param string|null $message
1310
     * @param string|null $propertyPath
1311
     * @return void
1312
     * @throws \Assert\AssertionFailedException
1313
     */
1314
    public static function alnum($value, $message = null, $propertyPath = null)
1315
    {
1316
        try {
1317
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1318
        } catch(AssertionFailedException $e) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after CATCH keyword; 0 found
Loading history...
1319
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1320
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1321
                self::stringify($value)
1322
            );
1323
1324
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1325
        }
1326
    }
1327
1328
    /**
1329
     * Assert that the value is boolean True.
1330
     *
1331
     * @param mixed $value
1332
     * @param string|null $message
1333
     * @param string|null $propertyPath
1334
     * @return void
1335
     * @throws \Assert\AssertionFailedException
1336
     */
1337
    public static function true($value, $message = null, $propertyPath = null)
1338
    {
1339
        if ($value !== true) {
1340
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1341
                $message ?: 'Value "%s" is not TRUE.',
1342
                self::stringify($value)
1343
            );
1344
1345
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1346
        }
1347
    }
1348
1349
    /**
1350
     * Assert that the value is boolean False.
1351
     *
1352
     * @param mixed $value
1353
     * @param string|null $message
1354
     * @param string|null $propertyPath
1355
     * @return void
1356
     * @throws \Assert\AssertionFailedException
1357
     */
1358
    public static function false($value, $message = null, $propertyPath = null)
1359
    {
1360
        if ($value !== false) {
1361
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1362
                $message ?: 'Value "%s" is not FALSE.',
1363
                self::stringify($value)
1364
            );
1365
1366
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1367
        }
1368
    }
1369
1370
    /**
1371
     * Assert that the class exists.
1372
     *
1373
     * @param mixed $value
1374
     * @param string|null $message
1375
     * @param string|null $propertyPath
1376
     * @return void
1377
     * @throws \Assert\AssertionFailedException
1378
     */
1379
    public static function classExists($value, $message = null, $propertyPath = null)
1380
    {
1381
        if ( ! class_exists($value)) {
1382
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1383
                $message ?: 'Class "%s" does not exist.',
1384
                self::stringify($value)
1385
            );
1386
1387
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1388
        }
1389
    }
1390
1391
    /**
1392
     * Assert that the class implements the interface
1393
     *
1394
     * @param mixed $class
1395
     * @param string $interfaceName
1396
     * @param string|null $message
1397
     * @param string|null $propertyPath
1398
     * @return void
1399
     * @throws \Assert\AssertionFailedException
1400
     */
1401
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1402
    {
1403
        $reflection = new \ReflectionClass($class);
1404
        if ( ! $reflection->implementsInterface($interfaceName)) {
1405
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1406
                $message ?: 'Class "%s" does not implement interface "%s".',
1407
                self::stringify($class),
1408
                self::stringify($interfaceName)
1409
            );
1410
1411
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1412
        }
1413
    }
1414
1415
    /**
1416
     * Assert that the given string is a valid json string.
1417
     *
1418
     * NOTICE:
1419
     * Since this does a json_decode to determine its validity
1420
     * you probably should consider, when using the variable
1421
     * content afterwards, just to decode and check for yourself instead
1422
     * of using this assertion.
1423
     *
1424
     * @param mixed $value
1425
     * @param string|null $message
1426
     * @param string|null $propertyPath
1427
     * @return void
1428
     * @throws \Assert\AssertionFailedException
1429
     */
1430
    public static function isJsonString($value, $message = null, $propertyPath = null)
1431
    {
1432
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1433
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1434
                $message ?: 'Value "%s" is not a valid JSON string.',
1435
                self::stringify($value)
1436
            );
1437
1438
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1439
        }
1440
    }
1441
1442
    /**
1443
     * Assert that the given string is a valid UUID
1444
     *
1445
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1446
     *
1447
     * @param string $value
1448
     * @param string|null $message
1449
     * @param string|null $propertyPath
1450
     * @return void
1451
     * @throws \Assert\AssertionFailedException
1452
     */
1453
    public static function uuid($value, $message = null, $propertyPath = null)
1454
    {
1455
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
1456
1457
        if ($value === '00000000-0000-0000-0000-000000000000') {
1458
            return;
1459
        }
1460
1461
        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)) {
1462
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1463
                $message ?: 'Value "%s" is not a valid UUID.',
1464
                self::stringify($value)
1465
            );
1466
1467
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1468
        }
1469
    }
1470
1471
    /**
1472
     * Assert that the count of countable is equal to count.
1473
     *
1474
     * @param array|\Countable $countable
1475
     * @param int              $count
1476
     * @param string           $message
0 ignored issues
show
Documentation introduced by
Should the type for parameter $message not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1477
     * @param string           $propertyPath
0 ignored issues
show
Documentation introduced by
Should the type for parameter $propertyPath not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1478
     * @return void
1479
     * @throws \Assert\AssertionFailedException
1480
     */
1481
    public static function count($countable, $count, $message = null, $propertyPath = null)
1482
    {
1483
        if ($count !== count($countable)) {
1484
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1485
                $message ?: 'List does not contain exactly "%d" elements.',
1486
                self::stringify($count)
1487
            );
1488
1489
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1490
        }
1491
    }
1492
1493
    /**
1494
     * static call handler to implement:
1495
     *  - "null or assertion" delegation
1496
     *  - "all" delegation
1497
     */
1498
    public static function __callStatic($method, $args)
1499
    {
1500
        if (strpos($method, "nullOr") === 0) {
1501
            if ( ! array_key_exists(0, $args)) {
1502
                throw new BadMethodCallException("Missing the first argument.");
1503
            }
1504
1505
            if ($args[0] === null) {
1506
                return;
1507
            }
1508
1509
            $method = substr($method, 6);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $method. This often makes code more readable.
Loading history...
1510
1511
            return call_user_func_array(array(get_called_class(), $method), $args);
1512
        }
1513
1514
        if (strpos($method, "all") === 0) {
1515
            if ( ! array_key_exists(0, $args)) {
1516
                throw new BadMethodCallException("Missing the first argument.");
1517
            }
1518
1519
            static::isTraversable($args[0]);
1520
1521
            $method      = substr($method, 3);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $method. This often makes code more readable.
Loading history...
1522
            $values      = array_shift($args);
1523
            $calledClass = get_called_class();
1524
1525
            foreach ($values as $value) {
1526
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1527
            }
1528
1529
            return;
1530
        }
1531
1532
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1533
    }
1534
1535
    /**
1536
     * Determines if the values array has every choice as key and that this choice has content.
1537
     *
1538
     * @param array $values
1539
     * @param array $choices
1540
     * @param null  $message
1541
     * @param null  $propertyPath
1542
     */
1543
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1544
    {
1545
        self::notEmpty($values, $message, $propertyPath);
1546
1547
        foreach ($choices as $choice) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1548
1549
            self::notEmptyKey($values, $choice, $message, $propertyPath);
1550
        }
1551
    }
1552
1553
    /**
1554
     * Determines that the named method is defined in the provided object.
1555
     *
1556
     * @param string $value
1557
     * @param mixed  $object
1558
     * @param null   $message
1559
     * @param null   $propertyPath
1560
     *
1561
     * @throws
1562
     */
1563
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1564
    {
1565
        self::isObject($object, $message, $propertyPath);
1566
1567
        if (!method_exists($object, $value)) {
1568
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1569
                $message ?: 'Expected "%s" does not exist in provided object.',
1570
                self::stringify($value)
1571
            );
1572
1573
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1574
        }
1575
    }
1576
1577
    /**
1578
     * Determines that the provided value is an object.
1579
     *
1580
     * @param mixed $value
1581
     * @param null  $message
1582
     * @param null  $propertyPath
1583
     */
1584
    public static function isObject($value, $message = null, $propertyPath = null)
1585
    {
1586
        if (!is_object($value)) {
1587
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1588
                $message ?: 'Provided "%s" is not a valid object.',
1589
                self::stringify($value)
1590
            );
1591
1592
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1593
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1594
        }
1595
    }
1596
1597
    /**
1598
     * Determines if the value is less than given limit.
1599
     *
1600
     * @param mixed $value
1601
     * @param mixed $limit
1602
     * @param null  $message
1603
     * @param null  $propertyPath
1604
     */
1605
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1606
    {
1607
        if ($value >= $limit) {
1608
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1609
                $message ?: 'Provided "%s" is not less than "%s".',
1610
                self::stringify($value),
1611
                self::stringify($limit)
1612
            );
1613
1614
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1615
        }
1616
    }
1617
1618
    /**
1619
     * Determines if the value is less or than given limit.
1620
     *
1621
     * @param mixed $value
1622
     * @param mixed $limit
1623
     * @param null  $message
1624
     * @param null  $propertyPath
1625
     */
1626
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1627
    {
1628
        if ($value > $limit) {
1629
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1630
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1631
                self::stringify($value),
1632
                self::stringify($limit)
1633
            );
1634
1635
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1636
        }
1637
    }
1638
1639
    /**
1640
     * Determines if the value is greater than given limit.
1641
     *
1642
     * @param mixed $value
1643
     * @param mixed $limit
1644
     * @param null  $message
1645
     * @param null  $propertyPath
1646
     */
1647
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1648
    {
1649
        if ($value <= $limit) {
1650
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1651
                $message ?: 'Provided "%s" is not greater than "%s".',
1652
                self::stringify($value),
1653
                self::stringify($limit)
1654
            );
1655
1656
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1657
        }
1658
    }
1659
1660
    /**
1661
     * Determines if the value is greater or equal than given limit.
1662
     *
1663
     * @param mixed $value
1664
     * @param mixed $limit
1665
     * @param null  $message
1666
     * @param null  $propertyPath
1667
     */
1668
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1669
    {
1670
        if ($value < $limit) {
1671
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1672
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1673
                self::stringify($value),
1674
                self::stringify($limit)
1675
            );
1676
1677
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1678
        }
1679
    }
1680
1681
    /**
1682
     * Assert that date is valid and corresponds to the given format.
1683
     *
1684
     * @param string      $value
1685
     * @param string      $format supports all of the options date(), except for the following:
1686
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
1687
     * @param string|null $message
1688
     * @param string|null $propertyPath
1689
     *
1690
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
1691
     */
1692
     public static function date($value, $format, $message = null, $propertyPath = null)
1693
     {
1694
         static::string($value, $message, $propertyPath);
1695
         static::string($format, $message, $propertyPath);
1696
1697
         $dateTime = \DateTime::createFromFormat($format, $value);
1698
1699
         if (false === $dateTime || $value !== $dateTime->format($format)) {
1700
             $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1701
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
1702
                 self::stringify($value),
1703
                 self::stringify($format)
1704
             );
1705
1706
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1707
         }
1708
     }
1709
1710
    /**
1711
     * Determines that the provided value is callable.
1712
     *
1713
     * @param mixed $value
1714
     * @param null $message
1715
     * @param null $propertyPath
1716
     */
1717
    public static function isCallable($value, $message = null, $propertyPath = null)
1718
    {
1719
        if (!is_callable($value)) {
1720
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1721
                $message ?: 'Provided "%s" is not a callable.',
1722
                self::stringify($value)
1723
            );
1724
1725
            throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath);
1726
        }
1727
    }
1728
1729
    /**
1730
     * Make a string version of a value.
1731
     *
1732
     * @param mixed $value
1733
     * @return string
1734
     */
1735
    protected static function stringify($value)
1736
    {
1737
        if (is_bool($value)) {
1738
            return $value ? '<TRUE>' : '<FALSE>';
1739
        }
1740
1741
        if (is_scalar($value)) {
1742
            $val = (string)$value;
1743
1744
            if (strlen($val) > 100) {
1745
                $val = substr($val, 0, 97) . '...';
1746
            }
1747
1748
            return $val;
1749
        }
1750
1751
        if (is_array($value)) {
1752
            return '<ARRAY>';
1753
        }
1754
1755
        if (is_object($value)) {
1756
            return get_class($value);
1757
        }
1758
1759
        if (is_resource($value)) {
1760
            return '<RESOURCE>';
1761
        }
1762
1763
        if ($value === NULL) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
1764
            return '<NULL>';
1765
        }
1766
1767
        return 'unknown';
1768
    }
1769
}
1770
1771