Completed
Pull Request — master (#179)
by Andreas
03:28
created

Assertion::betweenExclusive()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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