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

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