Completed
Pull Request — master (#126)
by
unknown
01:55
created

Assertion::notEmptyKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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