Completed
Pull Request — master (#169)
by Woody
03:57 queued 01:26
created

Assertion::satisfy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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