Completed
Pull Request — master (#151)
by
unknown
02:23
created

Assertion::contains()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 5
b 0
f 0
nc 2
nop 5
dl 0
loc 15
rs 9.4285
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 allNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices for all values.
69
 * @method static void allNotIsInstanceOf($value, $className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name for all values.
70
 * @method static void allNotNull($value, $message = null, $propertyPath = null) Assert that value is not null for all values.
71
 * @method static void allNotSame($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ) for all values.
72
 * @method static void allNumeric($value, $message = null, $propertyPath = null) Assert that value is numeric for all values.
73
 * @method static void allRange($value, $minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers for all values.
74
 * @method static void allReadable($value, $message = null, $propertyPath = null) Assert that the value is something readable for all values.
75
 * @method static void allRegex($value, $pattern, $message = null, $propertyPath = null) Assert that value matches a regex for all values.
76
 * @method static void allSame($value, $value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===) for all values.
77
 * @method static void allScalar($value, $message = null, $propertyPath = null) Assert that value is a PHP scalar for all values.
78
 * @method static void allStartsWith($string, $needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars for all values.
79
 * @method static void allString($value, $message = null, $propertyPath = null) Assert that value is a string for all values.
80
 * @method static void allSubclassOf($value, $className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name for all values.
81
 * @method static void allTrue($value, $message = null, $propertyPath = null) Assert that the value is boolean True for all values.
82
 * @method static void allUrl($value, $message = null, $propertyPath = null) Assert that value is an URL for all values.
83
 * @method static void allUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID for all values.
84
 * @method static void allWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable for all values.
85
 * @method static void nullOrAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric or that the value is null.
86
 * @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.
87
 * @method static void nullOrBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean or that the value is null.
88
 * @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices or that the value is null.
89
 * @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.
90
 * @method static void nullOrClassExists($value, $message = null, $propertyPath = null) Assert that the class exists or that the value is null.
91
 * @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.
92
 * @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.
93
 * @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.
94
 * @method static void nullOrDigit($value, $message = null, $propertyPath = null) Validates if an integer or integerish is a digit or that the value is null.
95
 * @method static void nullOrDirectory($value, $message = null, $propertyPath = null) Assert that a directory exists or that the value is null.
96
 * @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.
97
 * @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.
98
 * @method static void nullOrEq($value, $value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ) or that the value is null.
99
 * @method static void nullOrFalse($value, $message = null, $propertyPath = null) Assert that the value is boolean False or that the value is null.
100
 * @method static void nullOrFile($value, $message = null, $propertyPath = null) Assert that a file exists or that the value is null.
101
 * @method static void nullOrFloat($value, $message = null, $propertyPath = null) Assert that value is a php float or that the value is null.
102
 * @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.
103
 * @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.
104
 * @method static void nullOrImplementsInterface($class, $interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface or that the value is null.
105
 * @method static void nullOrInArray($value, $choices, $message = null, $propertyPath = null) Alias of {@see choice()} or that the value is null.
106
 * @method static void nullOrInteger($value, $message = null, $propertyPath = null) Assert that value is a php integer or that the value is null.
107
 * @method static void nullOrIntegerish($value, $message = null, $propertyPath = null) Assert that value is a php integer'ish or that the value is null.
108
 * @method static void nullOrIsArray($value, $message = null, $propertyPath = null) Assert that value is an array or that the value is null.
109
 * @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.
110
 * @method static void nullOrIsCallable($value, $message = null, $propertyPath = null) Determines that the provided value is callable or that the value is null.
111
 * @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.
112
 * @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.
113
 * @method static void nullOrIsObject($value, $message = null, $propertyPath = null) Determines that the provided value is an object or that the value is null.
114
 * @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.
115
 * @method static void nullOrKeyExists($value, $key, $message = null, $propertyPath = null) Assert that key exists in an array or that the value is null.
116
 * @method static void nullOrKeyNotExists($value, $key, $message = null, $propertyPath = null) Assert that key does not exist in an array
117
 * @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.
118
 * @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.
119
 * @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.
120
 * @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.
121
 * @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.
122
 * @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.
123
 * @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.
124
 * @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.
125
 * @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.
126
 * @method static void nullOrNoContent($value, $message = null, $propertyPath = null) Assert that value is empty or that the value is null.
127
 * @method static void nullOrNotBlank($value, $message = null, $propertyPath = null) Assert that value is not blank or that the value is null.
128
 * @method static void nullOrNotEmpty($value, $message = null, $propertyPath = null) Assert that value is not empty or that the value is null.
129
 * @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.
130
 * @method static void nullOrNotEq($value1, $value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
131
 * @method static void nullOrNotInArray($value, $choices, $message = null, $propertyPath = null) Assert that value is not in array of choices or that the value is null.
132
 * @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.
133
 * @method static void nullOrNotNull($value, $message = null, $propertyPath = null) Assert that value is not null or that the value is null.
134
 * @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.
135
 * @method static void nullOrNumeric($value, $message = null, $propertyPath = null) Assert that value is numeric or that the value is null.
136
 * @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.
137
 * @method static void nullOrReadable($value, $message = null, $propertyPath = null) Assert that the value is something readable or that the value is null.
138
 * @method static void nullOrRegex($value, $pattern, $message = null, $propertyPath = null) Assert that value matches a regex or that the value is null.
139
 * @method static void nullOrSame($value, $value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===) or that the value is null.
140
 * @method static void nullOrScalar($value, $message = null, $propertyPath = null) Assert that value is a PHP scalar or that the value is null.
141
 * @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.
142
 * @method static void nullOrString($value, $message = null, $propertyPath = null) Assert that value is a string or that the value is null.
143
 * @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.
144
 * @method static void nullOrTrue($value, $message = null, $propertyPath = null) Assert that the value is boolean True or that the value is null.
145
 * @method static void nullOrUrl($value, $message = null, $propertyPath = null) Assert that value is an URL or that the value is null.
146
 * @method static void nullOrUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID or that the value is null.
147
 * @method static void nullOrWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable or that the value is null.
148
 */
149
class Assertion
150
{
151
    const INVALID_FLOAT             = 9;
152
    const INVALID_INTEGER           = 10;
153
    const INVALID_DIGIT             = 11;
154
    const INVALID_INTEGERISH        = 12;
155
    const INVALID_BOOLEAN           = 13;
156
    const VALUE_EMPTY               = 14;
157
    const VALUE_NULL                = 15;
158
    const INVALID_STRING            = 16;
159
    const INVALID_REGEX             = 17;
160
    const INVALID_MIN_LENGTH        = 18;
161
    const INVALID_MAX_LENGTH        = 19;
162
    const INVALID_STRING_START      = 20;
163
    const INVALID_STRING_CONTAINS   = 21;
164
    const INVALID_CHOICE            = 22;
165
    const INVALID_NUMERIC           = 23;
166
    const INVALID_ARRAY             = 24;
167
    const INVALID_KEY_EXISTS        = 26;
168
    const INVALID_NOT_BLANK         = 27;
169
    const INVALID_INSTANCE_OF       = 28;
170
    const INVALID_SUBCLASS_OF       = 29;
171
    const INVALID_RANGE             = 30;
172
    const INVALID_ALNUM             = 31;
173
    const INVALID_TRUE              = 32;
174
    const INVALID_EQ                = 33;
175
    const INVALID_SAME              = 34;
176
    const INVALID_MIN               = 35;
177
    const INVALID_MAX               = 36;
178
    const INVALID_LENGTH            = 37;
179
    const INVALID_FALSE             = 38;
180
    const INVALID_STRING_END        = 39;
181
    const INVALID_UUID              = 40;
182
    const INVALID_COUNT             = 41;
183
    const INVALID_NOT_EQ            = 42;
184
    const INVALID_NOT_SAME          = 43;
185
    const INVALID_TRAVERSABLE       = 44;
186
    const INVALID_ARRAY_ACCESSIBLE  = 45;
187
    const INVALID_KEY_ISSET         = 46;
188
    const INVALID_VALUE_IN_ARRAY    = 47;
189
    const INVALID_DIRECTORY         = 101;
190
    const INVALID_FILE              = 102;
191
    const INVALID_READABLE          = 103;
192
    const INVALID_WRITEABLE         = 104;
193
    const INVALID_CLASS             = 105;
194
    const INVALID_EMAIL             = 201;
195
    const INTERFACE_NOT_IMPLEMENTED = 202;
196
    const INVALID_URL               = 203;
197
    const INVALID_NOT_INSTANCE_OF   = 204;
198
    const VALUE_NOT_EMPTY           = 205;
199
    const INVALID_JSON_STRING       = 206;
200
    const INVALID_OBJECT            = 207;
201
    const INVALID_METHOD            = 208;
202
    const INVALID_SCALAR            = 209;
203
    const INVALID_LESS              = 210;
204
    const INVALID_LESS_OR_EQUAL     = 211;
205
    const INVALID_GREATER           = 212;
206
    const INVALID_GREATER_OR_EQUAL  = 213;
207
    const INVALID_DATE              = 214;
208
    const INVALID_CALLABLE          = 215;
209
    const INVALID_KEY_NOT_EXISTS    = 216;
210
211
    /**
212
     * Exception to throw when an assertion failed.
213
     *
214
     * @var string
215
     */
216
    static protected $exceptionClass = 'Assert\InvalidArgumentException';
217
218
    /**
219
     * Helper method that handles building the assertion failure exceptions.
220
     * They are returned from this method so that the stack trace still shows
221
     * the assertions method.
222
     */
223
    protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
224
    {
225
        $exceptionClass = static::$exceptionClass;
226
        return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
227
    }
228
229
    /**
230
     * Assert that two values are equal (using == ).
231
     *
232
     * @param mixed $value
233
     * @param mixed $value2
234
     * @param string|null $message
235
     * @param string|null $propertyPath
236
     * @return void
237
     * @throws \Assert\AssertionFailedException
238
     */
239
    public static function eq($value, $value2, $message = null, $propertyPath = null)
240
    {
241
        if ($value != $value2) {
242
            $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...
243
                $message ?: 'Value "%s" does not equal expected value "%s".',
244
                static::stringify($value),
245
                static::stringify($value2)
246
            );
247
248
            throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
249
        }
250
    }
251
252
    /**
253
     * Assert that two values are the same (using ===).
254
     *
255
     * @param mixed $value
256
     * @param mixed $value2
257
     * @param string|null $message
258
     * @param string|null $propertyPath
259
     * @return void
260
     * @throws \Assert\AssertionFailedException
261
     */
262
    public static function same($value, $value2, $message = null, $propertyPath = null)
263
    {
264
        if ($value !== $value2) {
265
            $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...
266
                $message ?: 'Value "%s" is not the same as expected value "%s".',
267
                static::stringify($value),
268
                static::stringify($value2)
269
            );
270
271
            throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
272
        }
273
    }
274
275
    /**
276
     * Assert that two values are not equal (using == ).
277
     *
278
     * @param mixed $value1
279
     * @param mixed $value2
280
     * @param string|null $message
281
     * @param string|null $propertyPath
282
     * @return void
283
     * @throws \Assert\AssertionFailedException
284
     */
285
    public static function notEq($value1, $value2, $message = null, $propertyPath = null)
286
    {
287
        if ($value1 == $value2) {
288
            $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...
289
                $message ?: 'Value "%s" is equal to expected value "%s".',
290
                static::stringify($value1),
291
                static::stringify($value2)
292
            );
293
            throw static::createException($value1, $message,static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
294
        }
295
    }
296
297
    /**
298
     * Assert that two values are not the same (using === ).
299
     *
300
     * @param mixed $value1
301
     * @param mixed $value2
302
     * @param string|null $message
303
     * @param string|null $propertyPath
304
     * @return void
305
     * @throws \Assert\AssertionFailedException
306
     */
307
    public static function notSame($value1, $value2, $message = null, $propertyPath = null)
308
    {
309
        if ($value1 === $value2) {
310
            $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...
311
                $message ?: 'Value "%s" is the same as expected value "%s".',
312
                static::stringify($value1),
313
                static::stringify($value2)
314
            );
315
            throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
316
        }
317
    }
318
319
    /**
320
     * Assert that value is not in array of choices.
321
     *
322
     * @param mixed $value
323
     * @param array $choices
324
     * @param string|null $message
325
     * @param string|null $propertyPath
326
     * @return void
327
     * @throws \Assert\AssertionFailedException
328
     */
329
    public static function notInArray($value, array $choices, $message = null, $propertyPath = null)
330
    {
331
        if (in_array($value, $choices) === true) {
332
            $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...
333
                $message ?: 'Value "%s" is in given "%s".',
334
                static::stringify($value),
335
                static::stringify($choices)
336
            );
337
            throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath);
338
        }
339
    }
340
341
    /**
342
     * Assert that value is a php integer.
343
     *
344
     * @param mixed $value
345
     * @param string|null $message
346
     * @param string|null $propertyPath
347
     * @return void
348
     * @throws \Assert\AssertionFailedException
349
     */
350
    public static function integer($value, $message = null, $propertyPath = null)
351
    {
352
        if ( ! is_int($value)) {
353
            $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...
354
                $message ?: 'Value "%s" is not an integer.',
355
                static::stringify($value)
356
            );
357
358
            throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
359
        }
360
    }
361
362
    /**
363
     * Assert that value is a php float.
364
     *
365
     * @param mixed $value
366
     * @param string|null $message
367
     * @param string|null $propertyPath
368
     * @return void
369
     * @throws \Assert\AssertionFailedException
370
     */
371
    public static function float($value, $message = null, $propertyPath = null)
372
    {
373
        if ( ! is_float($value)) {
374
            $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...
375
                $message ?: 'Value "%s" is not a float.',
376
                static::stringify($value)
377
            );
378
379
            throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
380
        }
381
    }
382
383
    /**
384
     * Validates if an integer or integerish is a digit.
385
     *
386
     * @param mixed $value
387
     * @param string|null $message
388
     * @param string|null $propertyPath
389
     * @return void
390
     * @throws \Assert\AssertionFailedException
391
     */
392
    public static function digit($value, $message = null, $propertyPath = null)
393
    {
394
        if ( ! ctype_digit((string)$value)) {
395
            $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...
396
                $message ?: 'Value "%s" is not a digit.',
397
                static::stringify($value)
398
            );
399
400
            throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
401
        }
402
    }
403
404
    /**
405
     * Assert that value is a php integer'ish.
406
     *
407
     * @param mixed $value
408
     * @param string|null $message
409
     * @param string|null $propertyPath
410
     * @return void
411
     * @throws \Assert\AssertionFailedException
412
     */
413
    public static function integerish($value, $message = null, $propertyPath = null)
414
    {
415
        if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
416
            $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...
417
                $message ?: 'Value "%s" is not an integer or a number castable to integer.',
418
                static::stringify($value)
419
            );
420
421
            throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
422
        }
423
    }
424
425
    /**
426
     * Assert that value is php boolean
427
     *
428
     * @param mixed $value
429
     * @param string|null $message
430
     * @param string|null $propertyPath
431
     * @return void
432
     * @throws \Assert\AssertionFailedException
433
     */
434
    public static function boolean($value, $message = null, $propertyPath = null)
435
    {
436
        if ( ! is_bool($value)) {
437
            $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...
438
                $message ?: 'Value "%s" is not a boolean.',
439
                static::stringify($value)
440
            );
441
442
            throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
443
        }
444
    }
445
446
    /**
447
     * Assert that value is a PHP scalar
448
     *
449
     * @param mixed $value
450
     * @param string|null $message
451
     * @param string|null $propertyPath
452
     * @return void
453
     * @throws \Assert\AssertionFailedException
454
     */
455
    public static function scalar($value, $message = null, $propertyPath = null)
456
    {
457
        if (!is_scalar($value)) {
458
            $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...
459
                $message ?: 'Value "%s" is not a scalar.',
460
                static::stringify($value)
461
            );
462
463
            throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
464
        }
465
    }
466
467
    /**
468
     * Assert that value is not empty
469
     *
470
     * @param mixed $value
471
     * @param string|null $message
472
     * @param string|null $propertyPath
473
     * @return void
474
     * @throws \Assert\AssertionFailedException
475
     */
476
    public static function notEmpty($value, $message = null, $propertyPath = null)
477
    {
478
        if (empty($value)) {
479
            $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...
480
                $message ?: 'Value "%s" is empty, but non empty value was expected.',
481
                static::stringify($value)
482
            );
483
484
            throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
485
        }
486
    }
487
488
    /**
489
     * Assert that value is empty
490
     *
491
     * @param mixed $value
492
     * @param string|null $message
493
     * @param string|null $propertyPath
494
     * @return void
495
     * @throws \Assert\AssertionFailedException
496
     */
497
    public static function noContent($value, $message = null, $propertyPath = null)
498
    {
499
        if (!empty($value)) {
500
            $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...
501
                $message ?: 'Value "%s" is not empty, but empty value was expected.',
502
                static::stringify($value)
503
            );
504
505
            throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
506
        }
507
    }
508
509
    /**
510
     * Assert that value is not null
511
     *
512
     * @param mixed $value
513
     * @param string|null $message
514
     * @param string|null $propertyPath
515
     * @return void
516
     * @throws \Assert\AssertionFailedException
517
     */
518
    public static function notNull($value, $message = null, $propertyPath = null)
519
    {
520
        if ($value === null) {
521
            $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...
522
                $message ?: 'Value "%s" is null, but non null value was expected.',
523
                static::stringify($value)
524
            );
525
526
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
527
        }
528
    }
529
530
    /**
531
     * Assert that value is a string
532
     *
533
     * @param mixed $value
534
     * @param string|null $message
535
     * @param string|null $propertyPath
536
     * @return void
537
     * @throws \Assert\AssertionFailedException
538
     */
539
    public static function string($value, $message = null, $propertyPath = null)
540
    {
541
        if ( ! is_string($value)) {
542
            $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...
543
                $message ?: 'Value "%s" expected to be string, type %s given.',
544
                static::stringify($value),
545
                gettype($value)
546
            );
547
548
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
549
        }
550
    }
551
552
    /**
553
     * Assert that value matches a regex
554
     *
555
     * @param mixed $value
556
     * @param string $pattern
557
     * @param string|null $message
558
     * @param string|null $propertyPath
559
     * @return void
560
     * @throws \Assert\AssertionFailedException
561
     */
562
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
563
    {
564
        static::string($value, $message, $propertyPath);
565
566
        if ( ! preg_match($pattern, $value)) {
567
            $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...
568
                $message ?: 'Value "%s" does not match expression.',
569
                static::stringify($value)
570
            );
571
572
            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...
573
        }
574
    }
575
576
    /**
577
     * Assert that string has a given length.
578
     *
579
     * @param mixed $value
580
     * @param int $length
581
     * @param string|null $message
582
     * @param string|null $propertyPath
583
     * @param string $encoding
584
     * @return void
585
     * @throws \Assert\AssertionFailedException
586
     */
587
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
588
    {
589
        static::string($value, $message, $propertyPath);
590
591
        if (mb_strlen($value, $encoding) !== $length) {
592
            $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...
593
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
594
                static::stringify($value),
595
                $length,
596
                mb_strlen($value, $encoding)
597
            );
598
599
            $constraints = array('length' => $length, 'encoding' => $encoding);
600
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
601
        }
602
    }
603
604
    /**
605
     * Assert that a string is at least $minLength chars long.
606
     *
607
     * @param mixed $value
608
     * @param int $minLength
609
     * @param string|null $message
610
     * @param string|null $propertyPath
611
     * @param string $encoding
612
     * @return void
613
     * @throws \Assert\AssertionFailedException
614
     */
615
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
616
    {
617
        static::string($value, $message, $propertyPath);
618
619
        if (mb_strlen($value, $encoding) < $minLength) {
620
            $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...
621
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
622
                static::stringify($value),
623
                $minLength,
624
                mb_strlen($value, $encoding)
625
            );
626
627
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
628
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
629
        }
630
    }
631
632
    /**
633
     * Assert that string value is not longer than $maxLength chars.
634
     *
635
     * @param mixed $value
636
     * @param integer $maxLength
637
     * @param string|null $message
638
     * @param string|null $propertyPath
639
     * @param string $encoding
640
     * @return void
641
     * @throws \Assert\AssertionFailedException
642
     */
643
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
644
    {
645
        static::string($value, $message, $propertyPath);
646
647
        if (mb_strlen($value, $encoding) > $maxLength) {
648
            $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...
649
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
650
                static::stringify($value),
651
                $maxLength,
652
                mb_strlen($value, $encoding)
653
            );
654
655
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
656
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
657
        }
658
    }
659
660
    /**
661
     * Assert that string length is between min,max lengths.
662
     *
663
     * @param mixed $value
664
     * @param integer $minLength
665
     * @param integer $maxLength
666
     * @param string|null $message
667
     * @param string|null $propertyPath
668
     * @param string $encoding
669
     * @return void
670
     * @throws \Assert\AssertionFailedException
671
     */
672
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
673
    {
674
        static::string($value, $message, $propertyPath);
675
676
        if (mb_strlen($value, $encoding) < $minLength) {
677
            $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...
678
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
679
                static::stringify($value),
680
                $minLength,
681
                mb_strlen($value, $encoding)
682
            );
683
684
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
685
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
686
        }
687
688
        if (mb_strlen($value, $encoding) > $maxLength) {
689
            $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...
690
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
691
                static::stringify($value),
692
                $maxLength,
693
                mb_strlen($value, $encoding)
694
            );
695
696
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
697
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
698
        }
699
    }
700
701
    /**
702
     * Assert that string starts with a sequence of chars.
703
     *
704
     * @param mixed $string
705
     * @param string $needle
706
     * @param string|null $message
707
     * @param string|null $propertyPath
708
     * @param string $encoding
709
     * @return void
710
     * @throws \Assert\AssertionFailedException
711
     */
712
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
713
    {
714
        static::string($string, $message, $propertyPath);
715
716
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
717
            $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...
718
                $message ?: 'Value "%s" does not start with "%s".',
719
                static::stringify($string),
720
                static::stringify($needle)
721
            );
722
723
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
724
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
725
        }
726
    }
727
728
    /**
729
     * Assert that string ends with a sequence of chars.
730
     *
731
     * @param mixed $string
732
     * @param string $needle
733
     * @param string|null $message
734
     * @param string|null $propertyPath
735
     * @param string $encoding
736
     * @return void
737
     * @throws \Assert\AssertionFailedException
738
     */
739
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
740
    {
741
        static::string($string, $message, $propertyPath);
742
743
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
744
745
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
746
            $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...
747
                $message ?: 'Value "%s" does not end with "%s".',
748
                static::stringify($string),
749
                static::stringify($needle)
750
            );
751
752
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
753
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
754
        }
755
    }
756
757
    /**
758
     * Assert that string contains a sequence of chars.
759
     *
760
     * @param mixed $string
761
     * @param string $needle
762
     * @param string|null $message
763
     * @param string|null $propertyPath
764
     * @param string $encoding
765
     * @return void
766
     * @throws \Assert\AssertionFailedException
767
     */
768
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
769
    {
770
        static::string($string, $message, $propertyPath);
771
772
        if (mb_strpos($string, $needle, null, $encoding) === false) {
773
            $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...
774
                $message ?: 'Value "%s" does not contain "%s".',
775
                static::stringify($string),
776
                static::stringify($needle)
777
            );
778
779
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
780
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
781
        }
782
    }
783
784
    /**
785
     * Assert that value is in array of choices.
786
     *
787
     * @param mixed $value
788
     * @param array $choices
789
     * @param string|null $message
790
     * @param string|null $propertyPath
791
     * @return void
792
     * @throws \Assert\AssertionFailedException
793
     */
794
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
795
    {
796
        if ( ! in_array($value, $choices, true)) {
797
            $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...
798
                $message ?: 'Value "%s" is not an element of the valid values: %s',
799
                static::stringify($value),
800
                implode(", ", array_map('Assert\Assertion::stringify', $choices))
801
            );
802
803
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
804
        }
805
    }
806
807
    /**
808
     * Alias of {@see choice()}
809
     *
810
     * @throws \Assert\AssertionFailedException
811
     */
812
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
813
    {
814
        static::choice($value, $choices, $message, $propertyPath);
815
    }
816
817
    /**
818
     * Assert that value is numeric.
819
     *
820
     * @param mixed $value
821
     * @param string|null $message
822
     * @param string|null $propertyPath
823
     * @return void
824
     * @throws \Assert\AssertionFailedException
825
     */
826
    public static function numeric($value, $message = null, $propertyPath = null)
827
    {
828
        if ( ! is_numeric($value)) {
829
            $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...
830
                $message ?: 'Value "%s" is not numeric.',
831
                static::stringify($value)
832
            );
833
834
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
835
        }
836
    }
837
838
    /**
839
     * Assert that value is an array.
840
     *
841
     * @param mixed $value
842
     * @param string|null $message
843
     * @param string|null $propertyPath
844
     * @return void
845
     * @throws \Assert\AssertionFailedException
846
     */
847
    public static function isArray($value, $message = null, $propertyPath = null)
848
    {
849
        if ( ! is_array($value)) {
850
            $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...
851
                $message ?: 'Value "%s" is not an array.',
852
                static::stringify($value)
853
            );
854
855
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
856
        }
857
    }
858
859
    /**
860
     * Assert that value is an array or a traversable object.
861
     *
862
     * @param mixed $value
863
     * @param string|null $message
864
     * @param string|null $propertyPath
865
     * @return void
866
     * @throws \Assert\AssertionFailedException
867
     */
868
    public static function isTraversable($value, $message = null, $propertyPath = null)
869
    {
870
        if ( ! is_array($value) && ! $value instanceof \Traversable) {
871
            $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...
872
                $message ?: 'Value "%s" is not an array and does not implement Traversable.',
873
                static::stringify($value)
874
            );
875
876
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
877
        }
878
    }
879
880
    /**
881
     * Assert that value is an array or an array-accessible object.
882
     *
883
     * @param mixed $value
884
     * @param string|null $message
885
     * @param string|null $propertyPath
886
     * @return void
887
     * @throws \Assert\AssertionFailedException
888
     */
889
    public static function isArrayAccessible($value, $message = null, $propertyPath = null)
890
    {
891
        if ( ! is_array($value) && ! $value instanceof \ArrayAccess) {
892
            $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...
893
                $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
894
                static::stringify($value)
895
            );
896
897
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
898
        }
899
    }
900
901
    /**
902
     * Assert that key exists in an array
903
     *
904
     * @param mixed $value
905
     * @param string|integer $key
906
     * @param string|null $message
907
     * @param string|null $propertyPath
908
     * @return void
909
     * @throws \Assert\AssertionFailedException
910
     */
911
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
912
    {
913
        static::isArray($value, $message, $propertyPath);
914
915
        if ( ! array_key_exists($key, $value)) {
916
            $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...
917
                $message ?: 'Array does not contain an element with key "%s"',
918
                static::stringify($key)
919
            );
920
921
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
922
        }
923
    }
924
925
    /**
926
     * Assert that key does not exist in an array
927
     *
928
     * @param mixed $value
929
     * @param string|integer $key
930
     * @param string|null $message
931
     * @param string|null $propertyPath
932
     * @return void
933
     * @throws \Assert\AssertionFailedException
934
     */
935
    public static function keyNotExists($value, $key, $message = null, $propertyPath = null)
936
    {
937
        static::isArray($value, $message, $propertyPath);
938
939
        if (array_key_exists($key, $value)) {
940
            $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...
941
                $message ?: 'Array contains an element with key "%s"',
942
                self::stringify($key)
943
            );
944
945
            throw static::createException($value, $message, static::INVALID_KEY_NOT_EXISTS, $propertyPath, array('key' => $key));
946
        }
947
    }
948
949
    /**
950
     * Assert that key exists in an array/array-accessible object using isset()
951
     *
952
     * @param mixed $value
953
     * @param string|integer $key
954
     * @param string|null $message
955
     * @param string|null $propertyPath
956
     * @return void
957
     * @throws \Assert\AssertionFailedException
958
     */
959
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
960
    {
961
        static::isArrayAccessible($value, $message, $propertyPath);
962
963
        if ( ! isset($value[$key])) {
964
            $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...
965
                $message ?: 'The element with key "%s" was not found',
966
                static::stringify($key)
967
            );
968
969
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
970
        }
971
    }
972
973
    /**
974
     * Assert that key exists in an array/array-accessible object and it's value is not empty.
975
     *
976
     * @param mixed $value
977
     * @param string|integer $key
978
     * @param string|null $message
979
     * @param string|null $propertyPath
980
     * @return void
981
     * @throws \Assert\AssertionFailedException
982
     */
983
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
984
    {
985
        static::keyIsset($value, $key, $message, $propertyPath);
986
        static::notEmpty($value[$key], $message, $propertyPath);
987
    }
988
989
    /**
990
     * Assert that value is not blank
991
     *
992
     * @param mixed $value
993
     * @param string|null $message
994
     * @param string|null $propertyPath
995
     * @return void
996
     * @throws \Assert\AssertionFailedException
997
     */
998
    public static function notBlank($value, $message = null, $propertyPath = null)
999
    {
1000
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
1001
            $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...
1002
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
1003
                static::stringify($value)
1004
            );
1005
1006
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
1007
        }
1008
    }
1009
1010
    /**
1011
     * Assert that value is instance of given class-name.
1012
     *
1013
     * @param mixed $value
1014
     * @param string $className
1015
     * @param string|null $message
1016
     * @param string|null $propertyPath
1017
     * @return void
1018
     * @throws \Assert\AssertionFailedException
1019
     */
1020
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
1021
    {
1022
        if ( ! ($value instanceof $className)) {
1023
            $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...
1024
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
1025
                static::stringify($value),
1026
                $className
1027
            );
1028
1029
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
1030
        }
1031
    }
1032
1033
    /**
1034
     * Assert that value is not instance of given class-name.
1035
     *
1036
     * @param mixed $value
1037
     * @param string $className
1038
     * @param string|null $message
1039
     * @param string|null $propertyPath
1040
     * @return void
1041
     * @throws \Assert\AssertionFailedException
1042
     */
1043
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
1044
    {
1045
        if ($value instanceof $className) {
1046
            $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...
1047
                $message ?: 'Class "%s" was not expected to be instanceof of "%s".',
1048
                static::stringify($value),
1049
                $className
1050
            );
1051
1052
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1053
        }
1054
    }
1055
1056
    /**
1057
     * Assert that value is subclass of given class-name.
1058
     *
1059
     * @param mixed $value
1060
     * @param string $className
1061
     * @param string|null $message
1062
     * @param string|null $propertyPath
1063
     * @return void
1064
     * @throws \Assert\AssertionFailedException
1065
     */
1066
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1067
    {
1068
        if ( ! is_subclass_of($value, $className)) {
1069
            $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...
1070
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1071
                static::stringify($value),
1072
                $className
1073
            );
1074
1075
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1076
        }
1077
    }
1078
1079
    /**
1080
     * Assert that value is in range of numbers.
1081
     *
1082
     * @param mixed $value
1083
     * @param integer $minValue
1084
     * @param integer $maxValue
1085
     * @param string|null $message
1086
     * @param string|null $propertyPath
1087
     * @return void
1088
     * @throws \Assert\AssertionFailedException
1089
     */
1090
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1091
    {
1092
        static::numeric($value, $message, $propertyPath);
1093
1094
        if ($value < $minValue || $value > $maxValue) {
1095
            $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...
1096
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1097
                static::stringify($value),
1098
                static::stringify($minValue),
1099
                static::stringify($maxValue)
1100
            );
1101
1102
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1103
        }
1104
    }
1105
1106
    /**
1107
     * Assert that a value is at least as big as a given limit
1108
     *
1109
     * @param mixed $value
1110
     * @param mixed $minValue
1111
     * @param string|null $message
1112
     * @param string|null $propertyPath
1113
     * @return void
1114
     * @throws \Assert\AssertionFailedException
1115
     */
1116
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1117
    {
1118
        static::numeric($value, $message, $propertyPath);
1119
1120
        if ($value < $minValue) {
1121
            $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...
1122
                $message ?: 'Number "%s" was expected to be at least "%d".',
1123
                static::stringify($value),
1124
                static::stringify($minValue)
1125
            );
1126
1127
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1128
        }
1129
    }
1130
1131
    /**
1132
     * Assert that a number is smaller as a given limit
1133
     *
1134
     * @param mixed $value
1135
     * @param mixed $maxValue
1136
     * @param string|null $message
1137
     * @param string|null $propertyPath
1138
     * @return void
1139
     * @throws \Assert\AssertionFailedException
1140
     */
1141
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1142
    {
1143
        static::numeric($value, $message, $propertyPath);
1144
1145
        if ($value > $maxValue) {
1146
            $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...
1147
                $message ?: 'Number "%s" was expected to be at most "%d".',
1148
                static::stringify($value),
1149
                static::stringify($maxValue)
1150
            );
1151
1152
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1153
        }
1154
    }
1155
1156
    /**
1157
     * Assert that a file exists
1158
     *
1159
     * @param string $value
1160
     * @param string|null $message
1161
     * @param string|null $propertyPath
1162
     * @return void
1163
     * @throws \Assert\AssertionFailedException
1164
     */
1165
    public static function file($value, $message = null, $propertyPath = null)
1166
    {
1167
        static::string($value, $message, $propertyPath);
1168
        static::notEmpty($value, $message, $propertyPath);
1169
1170
        if ( ! is_file($value)) {
1171
            $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...
1172
                $message ?: 'File "%s" was expected to exist.',
1173
                static::stringify($value)
1174
            );
1175
1176
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1177
        }
1178
    }
1179
1180
    /**
1181
     * Assert that a directory exists
1182
     *
1183
     * @param string $value
1184
     * @param string|null $message
1185
     * @param string|null $propertyPath
1186
     * @return void
1187
     * @throws \Assert\AssertionFailedException
1188
     */
1189
    public static function directory($value, $message = null, $propertyPath = null)
1190
    {
1191
        static::string($value, $message, $propertyPath);
1192
1193
        if ( ! is_dir($value)) {
1194
            $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...
1195
                $message ?: 'Path "%s" was expected to be a directory.',
1196
                static::stringify($value)
1197
            );
1198
1199
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1200
        }
1201
    }
1202
1203
    /**
1204
     * Assert that the value is something readable
1205
     *
1206
     * @param string $value
1207
     * @param string|null $message
1208
     * @param string|null $propertyPath
1209
     * @return void
1210
     * @throws \Assert\AssertionFailedException
1211
     */
1212
    public static function readable($value, $message = null, $propertyPath = null)
1213
    {
1214
        static::string($value, $message, $propertyPath);
1215
1216
        if ( ! is_readable($value)) {
1217
            $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...
1218
                $message ?: 'Path "%s" was expected to be readable.',
1219
                static::stringify($value)
1220
            );
1221
1222
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1223
        }
1224
    }
1225
1226
    /**
1227
     * Assert that the value is something writeable
1228
     *
1229
     * @param string $value
1230
     * @param string|null $message
1231
     * @param string|null $propertyPath
1232
     * @return void
1233
     * @throws \Assert\AssertionFailedException
1234
     */
1235
    public static function writeable($value, $message = null, $propertyPath = null)
1236
    {
1237
        static::string($value, $message, $propertyPath);
1238
1239
        if ( ! is_writeable($value)) {
1240
            $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...
1241
                $message ?: 'Path "%s" was expected to be writeable.',
1242
                static::stringify($value)
1243
            );
1244
1245
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1246
        }
1247
    }
1248
1249
    /**
1250
     * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL).
1251
     *
1252
     * @param mixed $value
1253
     * @param string|null $message
1254
     * @param string|null $propertyPath
1255
     * @return void
1256
     * @throws \Assert\AssertionFailedException
1257
     */
1258
    public static function email($value, $message = null, $propertyPath = null)
1259
    {
1260
        static::string($value, $message, $propertyPath);
1261
1262
        if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1263
            $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...
1264
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1265
                static::stringify($value)
1266
            );
1267
1268
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1269
        } else {
1270
            $host = substr($value, strpos($value, '@') + 1);
1271
1272
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1273
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1274
                $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...
1275
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1276
                    static::stringify($value)
1277
                );
1278
1279
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1280
            }
1281
        }
1282
    }
1283
1284
    /**
1285
     * Assert that value is an URL.
1286
     *
1287
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1288
     *
1289
     * @param mixed $value
1290
     * @param string|null $message
1291
     * @param string|null $propertyPath
1292
     * @return void
1293
     * @throws \Assert\AssertionFailedException
1294
     *
1295
     *
1296
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1297
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1298
     */
1299
    public static function url($value, $message = null, $propertyPath = null)
1300
    {
1301
        static::string($value, $message, $propertyPath);
1302
1303
        $protocols = array('http', 'https');
1304
1305
        $pattern = '~^
1306
            (%s)://                                 # protocol
1307
            (
1308
                ([\pL\pN\pS-]+\.)+[\pL]+                   # a domain name
1309
                    |                                     #  or
1310
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}      # a IP address
1311
                    |                                     #  or
1312
                \[
1313
                    (?:(?:(?:(?:(?:(?:(?:[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})))?::))))
1314
                \]  # a IPv6 address
1315
            )
1316
            (:[0-9]+)?                              # a port (optional)
1317
            (/?|/\S+)                               # a /, nothing or a / with something
1318
        $~ixu';
1319
1320
        $pattern = sprintf($pattern, implode('|', $protocols));
1321
1322
        if (!preg_match($pattern, $value)) {
1323
            $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...
1324
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1325
                static::stringify($value)
1326
            );
1327
1328
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1329
        }
1330
1331
    }
1332
1333
    /**
1334
     * Assert that value is alphanumeric.
1335
     *
1336
     * @param mixed $value
1337
     * @param string|null $message
1338
     * @param string|null $propertyPath
1339
     * @return void
1340
     * @throws \Assert\AssertionFailedException
1341
     */
1342
    public static function alnum($value, $message = null, $propertyPath = null)
1343
    {
1344
        try {
1345
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1346
        } catch(AssertionFailedException $e) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after CATCH keyword; 0 found
Loading history...
1347
            $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...
1348
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1349
                static::stringify($value)
1350
            );
1351
1352
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1353
        }
1354
    }
1355
1356
    /**
1357
     * Assert that the value is boolean True.
1358
     *
1359
     * @param mixed $value
1360
     * @param string|null $message
1361
     * @param string|null $propertyPath
1362
     * @return void
1363
     * @throws \Assert\AssertionFailedException
1364
     */
1365
    public static function true($value, $message = null, $propertyPath = null)
1366
    {
1367
        if ($value !== true) {
1368
            $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...
1369
                $message ?: 'Value "%s" is not TRUE.',
1370
                static::stringify($value)
1371
            );
1372
1373
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1374
        }
1375
    }
1376
1377
    /**
1378
     * Assert that the value is boolean False.
1379
     *
1380
     * @param mixed $value
1381
     * @param string|null $message
1382
     * @param string|null $propertyPath
1383
     * @return void
1384
     * @throws \Assert\AssertionFailedException
1385
     */
1386
    public static function false($value, $message = null, $propertyPath = null)
1387
    {
1388
        if ($value !== false) {
1389
            $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...
1390
                $message ?: 'Value "%s" is not FALSE.',
1391
                static::stringify($value)
1392
            );
1393
1394
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1395
        }
1396
    }
1397
1398
    /**
1399
     * Assert that the class exists.
1400
     *
1401
     * @param mixed $value
1402
     * @param string|null $message
1403
     * @param string|null $propertyPath
1404
     * @return void
1405
     * @throws \Assert\AssertionFailedException
1406
     */
1407
    public static function classExists($value, $message = null, $propertyPath = null)
1408
    {
1409
        if ( ! class_exists($value)) {
1410
            $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...
1411
                $message ?: 'Class "%s" does not exist.',
1412
                static::stringify($value)
1413
            );
1414
1415
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1416
        }
1417
    }
1418
1419
    /**
1420
     * Assert that the class implements the interface
1421
     *
1422
     * @param mixed $class
1423
     * @param string $interfaceName
1424
     * @param string|null $message
1425
     * @param string|null $propertyPath
1426
     * @return void
1427
     * @throws \Assert\AssertionFailedException
1428
     */
1429
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1430
    {
1431
        $reflection = new \ReflectionClass($class);
1432
        if ( ! $reflection->implementsInterface($interfaceName)) {
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 ?: 'Class "%s" does not implement interface "%s".',
1435
                static::stringify($class),
1436
                static::stringify($interfaceName)
1437
            );
1438
1439
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1440
        }
1441
    }
1442
1443
    /**
1444
     * Assert that the given string is a valid json string.
1445
     *
1446
     * NOTICE:
1447
     * Since this does a json_decode to determine its validity
1448
     * you probably should consider, when using the variable
1449
     * content afterwards, just to decode and check for yourself instead
1450
     * of using this assertion.
1451
     *
1452
     * @param mixed $value
1453
     * @param string|null $message
1454
     * @param string|null $propertyPath
1455
     * @return void
1456
     * @throws \Assert\AssertionFailedException
1457
     */
1458
    public static function isJsonString($value, $message = null, $propertyPath = null)
1459
    {
1460
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1461
            $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...
1462
                $message ?: 'Value "%s" is not a valid JSON string.',
1463
                static::stringify($value)
1464
            );
1465
1466
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1467
        }
1468
    }
1469
1470
    /**
1471
     * Assert that the given string is a valid UUID
1472
     *
1473
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1474
     *
1475
     * @param string $value
1476
     * @param string|null $message
1477
     * @param string|null $propertyPath
1478
     * @return void
1479
     * @throws \Assert\AssertionFailedException
1480
     */
1481
    public static function uuid($value, $message = null, $propertyPath = null)
1482
    {
1483
        $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...
1484
1485
        if ($value === '00000000-0000-0000-0000-000000000000') {
1486
            return;
1487
        }
1488
1489
        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)) {
1490
            $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...
1491
                $message ?: 'Value "%s" is not a valid UUID.',
1492
                static::stringify($value)
1493
            );
1494
1495
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1496
        }
1497
    }
1498
1499
    /**
1500
     * Assert that the count of countable is equal to count.
1501
     *
1502
     * @param array|\Countable $countable
1503
     * @param int              $count
1504
     * @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...
1505
     * @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...
1506
     * @return void
1507
     * @throws \Assert\AssertionFailedException
1508
     */
1509
    public static function count($countable, $count, $message = null, $propertyPath = null)
1510
    {
1511
        if ($count !== count($countable)) {
1512
            $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...
1513
                $message ?: 'List does not contain exactly "%d" elements.',
1514
                static::stringify($count)
1515
            );
1516
1517
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1518
        }
1519
    }
1520
1521
    /**
1522
     * static call handler to implement:
1523
     *  - "null or assertion" delegation
1524
     *  - "all" delegation
1525
     */
1526
    public static function __callStatic($method, $args)
1527
    {
1528
        if (strpos($method, "nullOr") === 0) {
1529
            if ( ! array_key_exists(0, $args)) {
1530
                throw new BadMethodCallException("Missing the first argument.");
1531
            }
1532
1533
            if ($args[0] === null) {
1534
                return;
1535
            }
1536
1537
            $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...
1538
1539
            return call_user_func_array(array(get_called_class(), $method), $args);
1540
        }
1541
1542
        if (strpos($method, "all") === 0) {
1543
            if ( ! array_key_exists(0, $args)) {
1544
                throw new BadMethodCallException("Missing the first argument.");
1545
            }
1546
1547
            static::isTraversable($args[0]);
1548
1549
            $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...
1550
            $values      = array_shift($args);
1551
            $calledClass = get_called_class();
1552
1553
            foreach ($values as $value) {
1554
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1555
            }
1556
1557
            return;
1558
        }
1559
1560
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1561
    }
1562
1563
    /**
1564
     * Determines if the values array has every choice as key and that this choice has content.
1565
     *
1566
     * @param array $values
1567
     * @param array $choices
1568
     * @param null  $message
1569
     * @param null  $propertyPath
1570
     */
1571
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1572
    {
1573
        self::notEmpty($values, $message, $propertyPath);
1574
1575
        foreach ($choices as $choice) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
1576
1577
            self::notEmptyKey($values, $choice, $message, $propertyPath);
1578
        }
1579
    }
1580
1581
    /**
1582
     * Determines that the named method is defined in the provided object.
1583
     *
1584
     * @param string $value
1585
     * @param mixed  $object
1586
     * @param null   $message
1587
     * @param null   $propertyPath
1588
     *
1589
     * @throws
1590
     */
1591
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1592
    {
1593
        self::isObject($object, $message, $propertyPath);
1594
1595
        if (!method_exists($object, $value)) {
1596
            $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...
1597
                $message ?: 'Expected "%s" does not exist in provided object.',
1598
                static::stringify($value)
1599
            );
1600
1601
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1602
        }
1603
    }
1604
1605
    /**
1606
     * Determines that the provided value is an object.
1607
     *
1608
     * @param mixed $value
1609
     * @param null  $message
1610
     * @param null  $propertyPath
1611
     */
1612
    public static function isObject($value, $message = null, $propertyPath = null)
1613
    {
1614
        if (!is_object($value)) {
1615
            $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...
1616
                $message ?: 'Provided "%s" is not a valid object.',
1617
                static::stringify($value)
1618
            );
1619
1620
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1621
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1622
        }
1623
    }
1624
1625
    /**
1626
     * Determines if the value is less than given limit.
1627
     *
1628
     * @param mixed $value
1629
     * @param mixed $limit
1630
     * @param null  $message
1631
     * @param null  $propertyPath
1632
     */
1633
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1634
    {
1635
        if ($value >= $limit) {
1636
            $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...
1637
                $message ?: 'Provided "%s" is not less than "%s".',
1638
                static::stringify($value),
1639
                static::stringify($limit)
1640
            );
1641
1642
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1643
        }
1644
    }
1645
1646
    /**
1647
     * Determines if the value is less or than given limit.
1648
     *
1649
     * @param mixed $value
1650
     * @param mixed $limit
1651
     * @param null  $message
1652
     * @param null  $propertyPath
1653
     */
1654
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1655
    {
1656
        if ($value > $limit) {
1657
            $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...
1658
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1659
                static::stringify($value),
1660
                static::stringify($limit)
1661
            );
1662
1663
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1664
        }
1665
    }
1666
1667
    /**
1668
     * Determines if the value is greater than given limit.
1669
     *
1670
     * @param mixed $value
1671
     * @param mixed $limit
1672
     * @param null  $message
1673
     * @param null  $propertyPath
1674
     */
1675
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1676
    {
1677
        if ($value <= $limit) {
1678
            $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...
1679
                $message ?: 'Provided "%s" is not greater than "%s".',
1680
                static::stringify($value),
1681
                static::stringify($limit)
1682
            );
1683
1684
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1685
        }
1686
    }
1687
1688
    /**
1689
     * Determines if the value is greater or equal than given limit.
1690
     *
1691
     * @param mixed $value
1692
     * @param mixed $limit
1693
     * @param null  $message
1694
     * @param null  $propertyPath
1695
     */
1696
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1697
    {
1698
        if ($value < $limit) {
1699
            $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...
1700
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1701
                static::stringify($value),
1702
                static::stringify($limit)
1703
            );
1704
1705
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1706
        }
1707
    }
1708
1709
    /**
1710
     * Assert that date is valid and corresponds to the given format.
1711
     *
1712
     * @param string      $value
1713
     * @param string      $format supports all of the options date(), except for the following:
1714
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
1715
     * @param string|null $message
1716
     * @param string|null $propertyPath
1717
     *
1718
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
1719
     */
1720
     public static function date($value, $format, $message = null, $propertyPath = null)
1721
     {
1722
         static::string($value, $message, $propertyPath);
1723
         static::string($format, $message, $propertyPath);
1724
1725
         $dateTime = \DateTime::createFromFormat($format, $value);
1726
1727
         if (false === $dateTime || $value !== $dateTime->format($format)) {
1728
             $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...
1729
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
1730
                 static::stringify($value),
1731
                 static::stringify($format)
1732
             );
1733
1734
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
1735
         }
1736
     }
1737
1738
    /**
1739
     * Determines that the provided value is callable.
1740
     *
1741
     * @param mixed $value
1742
     * @param null $message
1743
     * @param null $propertyPath
1744
     */
1745
    public static function isCallable($value, $message = null, $propertyPath = null)
1746
    {
1747
        if (!is_callable($value)) {
1748
            $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...
1749
                $message ?: 'Provided "%s" is not a callable.',
1750
                static::stringify($value)
1751
            );
1752
1753
            throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath);
1754
        }
1755
    }
1756
1757
    /**
1758
     * Make a string version of a value.
1759
     *
1760
     * @param mixed $value
1761
     * @return string
1762
     */
1763
    protected static function stringify($value)
1764
    {
1765
        if (is_bool($value)) {
1766
            return $value ? '<TRUE>' : '<FALSE>';
1767
        }
1768
1769
        if (is_scalar($value)) {
1770
            $val = (string)$value;
1771
1772
            if (strlen($val) > 100) {
1773
                $val = substr($val, 0, 97) . '...';
1774
            }
1775
1776
            return $val;
1777
        }
1778
1779
        if (is_array($value)) {
1780
            return '<ARRAY>';
1781
        }
1782
1783
        if (is_object($value)) {
1784
            return get_class($value);
1785
        }
1786
1787
        if (is_resource($value)) {
1788
            return '<RESOURCE>';
1789
        }
1790
1791
        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...
1792
            return '<NULL>';
1793
        }
1794
1795
        return 'unknown';
1796
    }
1797
}
1798
1799