Completed
Push — master ( d410b1...93d6af )
by Richard
02:03
created

Assertion::phpVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Assert
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this package in the file LICENSE.txt.
10
 * If you did not receive a copy of the license and are unable to
11
 * obtain it through the world-wide-web, please send an email
12
 * to [email protected] so I can send you a copy immediately.
13
 */
14
15
namespace Assert;
16
17
use BadMethodCallException;
18
19
/**
20
 * Assert library
21
 *
22
 * @author Benjamin Eberlei <[email protected]>
23
 *
24
 * @method static bool allAlnum(mixed $value, string $message = null, string $propertyPath = null) Assert that value is alphanumeric for all values.
25
 * @method static bool allBetween(mixed $value, mixed $lowerLimit, mixed $upperLimit, string $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values.
26
 * @method static bool allBetweenExclusive(mixed $value, mixed $lowerLimit, mixed $upperLimit, string $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values.
27
 * @method static bool allBetweenLength(mixed $value, integer $minLength, integer $maxLength, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min,max lengths for all values.
28
 * @method static bool allBoolean(mixed $value, string $message = null, string $propertyPath = null) Assert that value is php boolean for all values.
29
 * @method static bool allChoice(mixed $value, array $choices, string $message = null, string $propertyPath = null) Assert that value is in array of choices for all values.
30
 * @method static bool allChoicesNotEmpty(array $values, array $choices, string $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content for all values.
31
 * @method static bool allClassExists(mixed $value, string $message = null, string $propertyPath = null) Assert that the class exists for all values.
32
 * @method static bool allContains(mixed $string, string $needle, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars for all values.
33
 * @method static bool allCount(array|\Countable $countable, array|\Countable $count, string $message = null, string $propertyPath = null) Assert that the count of countable is equal to count for all values.
34
 * @method static bool allDate(string $value, string $format, string $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format for all values.
35
 * @method static bool allDefined(mixed $constant, string $message = null, string $propertyPath = null) Assert that a constant is defined for all values.
36
 * @method static bool allDigit(mixed $value, string $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit for all values.
37
 * @method static bool allDirectory(string $value, string $message = null, string $propertyPath = null) Assert that a directory exists for all values.
38
 * @method static bool allE164(string $value, string $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number for all values.
39
 * @method static bool allEmail(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL) for all values.
40
 * @method static bool allEndsWith(mixed $string, string $needle, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars for all values.
41
 * @method static bool allEq(mixed $value, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are equal (using == ) for all values.
42
 * @method static bool allExtensionLoaded(mixed $value, string $message = null, string $propertyPath = null) Assert that extension is loaded for all values.
43
 * @method static bool allFalse(mixed $value, string $message = null, string $propertyPath = null) Assert that the value is boolean False for all values.
44
 * @method static bool allFile(string $value, string $message = null, string $propertyPath = null) Assert that a file exists for all values.
45
 * @method static bool allFloat(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a php float for all values.
46
 * @method static bool allGreaterOrEqualThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit for all values.
47
 * @method static bool allGreaterThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is greater than given limit for all values.
48
 * @method static bool allImplementsInterface(mixed $class, string $interfaceName, string $message = null, string $propertyPath = null) Assert that the class implements the interface for all values.
49
 * @method static bool allInArray(mixed $value, array $choices, string $message = null, string $propertyPath = null) Alias of {@see choice()} for all values.
50
 * @method static bool allInteger(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a php integer for all values.
51
 * @method static bool allIntegerish(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a php integer'ish for all values.
52
 * @method static bool allInterfaceExists(mixed $value, string $message = null, string $propertyPath = null) Assert that the interface exists for all values.
53
 * @method static bool allIp(string $value, int $flag = null, string $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address for all values.
54
 * @method static bool allIpv4(string $value, int $flag = null, string $message = null, string $propertyPath = null) Assert that value is an IPv4 address for all values.
55
 * @method static bool allIpv6(string $value, int $flag = null, string $message = null, string $propertyPath = null) Assert that value is an IPv6 address for all values.
56
 * @method static bool allIsArray(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an array for all values.
57
 * @method static bool allIsArrayAccessible(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object for all values.
58
 * @method static bool allIsCallable(mixed $value, string $message = null, string $propertyPath = null) Determines that the provided value is callable for all values.
59
 * @method static bool allIsInstanceOf(mixed $value, string $className, string $message = null, string $propertyPath = null) Assert that value is instance of given class-name for all values.
60
 * @method static bool allIsJsonString(mixed $value, string $message = null, string $propertyPath = null) Assert that the given string is a valid json string for all values.
61
 * @method static bool allIsObject(mixed $value, string $message = null, string $propertyPath = null) Determines that the provided value is an object for all values.
62
 * @method static bool allIsTraversable(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an array or a traversable object for all values.
63
 * @method static bool allKeyExists(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key exists in an array for all values.
64
 * @method static bool allKeyIsset(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() for all values.
65
 * @method static bool allKeyNotExists(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key does not exist in an array for all values.
66
 * @method static bool allLength(mixed $value, int $length, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length for all values.
67
 * @method static bool allLessOrEqualThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is less or than given limit for all values.
68
 * @method static bool allLessThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is less than given limit for all values.
69
 * @method static bool allMax(mixed $value, mixed $maxValue, string $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit for all values.
70
 * @method static bool allMaxLength(mixed $value, integer $maxLength, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars for all values.
71
 * @method static bool allMethodExists(string $value, mixed $object, string $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object for all values.
72
 * @method static bool allMin(mixed $value, mixed $minValue, string $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit for all values.
73
 * @method static bool allMinLength(mixed $value, int $minLength, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long for all values.
74
 * @method static bool allNoContent(mixed $value, string $message = null, string $propertyPath = null) Assert that value is empty for all values.
75
 * @method static bool allNotBlank(mixed $value, string $message = null, string $propertyPath = null) Assert that value is not blank for all values.
76
 * @method static bool allNotEmpty(mixed $value, string $message = null, string $propertyPath = null) Assert that value is not empty for all values.
77
 * @method static bool allNotEmptyKey(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty for all values.
78
 * @method static bool allNotEq(mixed $value1, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are not equal (using == ) for all values.
79
 * @method static bool allNotInArray(mixed $value, array $choices, string $message = null, string $propertyPath = null) Assert that value is not in array of choices for all values.
80
 * @method static bool allNotIsInstanceOf(mixed $value, string $className, string $message = null, string $propertyPath = null) Assert that value is not instance of given class-name for all values.
81
 * @method static bool allNotNull(mixed $value, string $message = null, string $propertyPath = null) Assert that value is not null for all values.
82
 * @method static bool allNotSame(mixed $value1, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are not the same (using === ) for all values.
83
 * @method static bool allNull(mixed $value, string $message = null, string $propertyPath = null) Assert that value is null for all values.
84
 * @method static bool allNumeric(mixed $value, string $message = null, string $propertyPath = null) Assert that value is numeric for all values.
85
 * @method static bool allPhpVersion(string $operator, mixed $version, string $message = null, string $propertyPath = null) Assert that extension is loaded for all values.
86
 * @method static bool allRange(mixed $value, mixed $minValue, mixed $maxValue, string $message = null, string $propertyPath = null) Assert that value is in range of numbers for all values.
87
 * @method static bool allReadable(string $value, string $message = null, string $propertyPath = null) Assert that the value is something readable for all values.
88
 * @method static bool allRegex(mixed $value, string $pattern, string $message = null, string $propertyPath = null) Assert that value matches a regex for all values.
89
 * @method static bool allSame(mixed $value, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are the same (using ===) for all values.
90
 * @method static bool allSatisfy(mixed $value, callable $callback, string $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback for all values.
91
 * @method static bool allScalar(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a PHP scalar for all values.
92
 * @method static bool allStartsWith(mixed $string, string $needle, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars for all values.
93
 * @method static bool allString(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a string for all values.
94
 * @method static bool allSubclassOf(mixed $value, string $className, string $message = null, string $propertyPath = null) Assert that value is subclass of given class-name for all values.
95
 * @method static bool allTrue(mixed $value, string $message = null, string $propertyPath = null) Assert that the value is boolean True for all values.
96
 * @method static bool allUrl(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an URL for all values.
97
 * @method static bool allUuid(string $value, string $message = null, string $propertyPath = null) Assert that the given string is a valid UUID for all values.
98
 * @method static bool allVersion(string $version1, string $operator, string $version2, string $message = null, string $propertyPath = null) Assert that extension is loaded for all values.
99
 * @method static bool allWriteable(string $value, string $message = null, string $propertyPath = null) Assert that the value is something writeable for all values.
100
 * @method static bool nullOrAlnum(mixed $value, string $message = null, string $propertyPath = null) Assert that value is alphanumeric or that the value is null.
101
 * @method static bool nullOrBetween(mixed $value, mixed $lowerLimit, mixed $upperLimit, string $message = null, string $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit or that the value is null.
102
 * @method static bool nullOrBetweenExclusive(mixed $value, mixed $lowerLimit, mixed $upperLimit, string $message = null, string $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit or that the value is null.
103
 * @method static bool nullOrBetweenLength(mixed $value, integer $minLength, integer $maxLength, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string length is between min,max lengths or that the value is null.
104
 * @method static bool nullOrBoolean(mixed $value, string $message = null, string $propertyPath = null) Assert that value is php boolean or that the value is null.
105
 * @method static bool nullOrChoice(mixed $value, array $choices, string $message = null, string $propertyPath = null) Assert that value is in array of choices or that the value is null.
106
 * @method static bool nullOrChoicesNotEmpty(array $values, array $choices, string $message = null, string $propertyPath = null) Determines if the values array has every choice as key and that this choice has content or that the value is null.
107
 * @method static bool nullOrClassExists(mixed $value, string $message = null, string $propertyPath = null) Assert that the class exists or that the value is null.
108
 * @method static bool nullOrContains(mixed $string, string $needle, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string contains a sequence of chars or that the value is null.
109
 * @method static bool nullOrCount(array|\Countable $countable, array|\Countable $count, string $message = null, string $propertyPath = null) Assert that the count of countable is equal to count or that the value is null.
110
 * @method static bool nullOrDate(string $value, string $format, string $message = null, string $propertyPath = null) Assert that date is valid and corresponds to the given format or that the value is null.
111
 * @method static bool nullOrDefined(mixed $constant, string $message = null, string $propertyPath = null) Assert that a constant is defined or that the value is null.
112
 * @method static bool nullOrDigit(mixed $value, string $message = null, string $propertyPath = null) Validates if an integer or integerish is a digit or that the value is null.
113
 * @method static bool nullOrDirectory(string $value, string $message = null, string $propertyPath = null) Assert that a directory exists or that the value is null.
114
 * @method static bool nullOrE164(string $value, string $message = null, string $propertyPath = null) Assert that the given string is a valid E164 Phone Number or that the value is null.
115
 * @method static bool nullOrEmail(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL) or that the value is null.
116
 * @method static bool nullOrEndsWith(mixed $string, string $needle, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars or that the value is null.
117
 * @method static bool nullOrEq(mixed $value, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are equal (using == ) or that the value is null.
118
 * @method static bool nullOrExtensionLoaded(mixed $value, string $message = null, string $propertyPath = null) Assert that extension is loaded or that the value is null.
119
 * @method static bool nullOrFalse(mixed $value, string $message = null, string $propertyPath = null) Assert that the value is boolean False or that the value is null.
120
 * @method static bool nullOrFile(string $value, string $message = null, string $propertyPath = null) Assert that a file exists or that the value is null.
121
 * @method static bool nullOrFloat(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a php float or that the value is null.
122
 * @method static bool nullOrGreaterOrEqualThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is greater or equal than given limit or that the value is null.
123
 * @method static bool nullOrGreaterThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is greater than given limit or that the value is null.
124
 * @method static bool nullOrImplementsInterface(mixed $class, string $interfaceName, string $message = null, string $propertyPath = null) Assert that the class implements the interface or that the value is null.
125
 * @method static bool nullOrInArray(mixed $value, array $choices, string $message = null, string $propertyPath = null) Alias of {@see choice()} or that the value is null.
126
 * @method static bool nullOrInteger(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a php integer or that the value is null.
127
 * @method static bool nullOrIntegerish(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a php integer'ish or that the value is null.
128
 * @method static bool nullOrInterfaceExists(mixed $value, string $message = null, string $propertyPath = null) Assert that the interface exists or that the value is null.
129
 * @method static bool nullOrIp(string $value, int $flag = null, string $message = null, string $propertyPath = null) Assert that value is an IPv4 or IPv6 address or that the value is null.
130
 * @method static bool nullOrIpv4(string $value, int $flag = null, string $message = null, string $propertyPath = null) Assert that value is an IPv4 address or that the value is null.
131
 * @method static bool nullOrIpv6(string $value, int $flag = null, string $message = null, string $propertyPath = null) Assert that value is an IPv6 address or that the value is null.
132
 * @method static bool nullOrIsArray(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an array or that the value is null.
133
 * @method static bool nullOrIsArrayAccessible(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object or that the value is null.
134
 * @method static bool nullOrIsCallable(mixed $value, string $message = null, string $propertyPath = null) Determines that the provided value is callable or that the value is null.
135
 * @method static bool nullOrIsInstanceOf(mixed $value, string $className, string $message = null, string $propertyPath = null) Assert that value is instance of given class-name or that the value is null.
136
 * @method static bool nullOrIsJsonString(mixed $value, string $message = null, string $propertyPath = null) Assert that the given string is a valid json string or that the value is null.
137
 * @method static bool nullOrIsObject(mixed $value, string $message = null, string $propertyPath = null) Determines that the provided value is an object or that the value is null.
138
 * @method static bool nullOrIsTraversable(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an array or a traversable object or that the value is null.
139
 * @method static bool nullOrKeyExists(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key exists in an array or that the value is null.
140
 * @method static bool nullOrKeyIsset(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object using isset() or that the value is null.
141
 * @method static bool nullOrKeyNotExists(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key does not exist in an array or that the value is null.
142
 * @method static bool nullOrLength(mixed $value, int $length, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string has a given length or that the value is null.
143
 * @method static bool nullOrLessOrEqualThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is less or than given limit or that the value is null.
144
 * @method static bool nullOrLessThan(mixed $value, mixed $limit, string $message = null, string $propertyPath = null) Determines if the value is less than given limit or that the value is null.
145
 * @method static bool nullOrMax(mixed $value, mixed $maxValue, string $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit or that the value is null.
146
 * @method static bool nullOrMaxLength(mixed $value, integer $maxLength, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars or that the value is null.
147
 * @method static bool nullOrMethodExists(string $value, mixed $object, string $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object or that the value is null.
148
 * @method static bool nullOrMin(mixed $value, mixed $minValue, string $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit or that the value is null.
149
 * @method static bool nullOrMinLength(mixed $value, int $minLength, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long or that the value is null.
150
 * @method static bool nullOrNoContent(mixed $value, string $message = null, string $propertyPath = null) Assert that value is empty or that the value is null.
151
 * @method static bool nullOrNotBlank(mixed $value, string $message = null, string $propertyPath = null) Assert that value is not blank or that the value is null.
152
 * @method static bool nullOrNotEmpty(mixed $value, string $message = null, string $propertyPath = null) Assert that value is not empty or that the value is null.
153
 * @method static bool nullOrNotEmptyKey(mixed $value, string|integer $key, string $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty or that the value is null.
154
 * @method static bool nullOrNotEq(mixed $value1, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
155
 * @method static bool nullOrNotInArray(mixed $value, array $choices, string $message = null, string $propertyPath = null) Assert that value is not in array of choices or that the value is null.
156
 * @method static bool nullOrNotIsInstanceOf(mixed $value, string $className, string $message = null, string $propertyPath = null) Assert that value is not instance of given class-name or that the value is null.
157
 * @method static bool nullOrNotNull(mixed $value, string $message = null, string $propertyPath = null) Assert that value is not null or that the value is null.
158
 * @method static bool nullOrNotSame(mixed $value1, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are not the same (using === ) or that the value is null.
159
 * @method static bool nullOrNull(mixed $value, string $message = null, string $propertyPath = null) Assert that value is null or that the value is null.
160
 * @method static bool nullOrNumeric(mixed $value, string $message = null, string $propertyPath = null) Assert that value is numeric or that the value is null.
161
 * @method static bool nullOrPhpVersion(string $operator, mixed $version, string $message = null, string $propertyPath = null) Assert that extension is loaded or that the value is null.
162
 * @method static bool nullOrRange(mixed $value, mixed $minValue, mixed $maxValue, string $message = null, string $propertyPath = null) Assert that value is in range of numbers or that the value is null.
163
 * @method static bool nullOrReadable(string $value, string $message = null, string $propertyPath = null) Assert that the value is something readable or that the value is null.
164
 * @method static bool nullOrRegex(mixed $value, string $pattern, string $message = null, string $propertyPath = null) Assert that value matches a regex or that the value is null.
165
 * @method static bool nullOrSame(mixed $value, mixed $value2, string $message = null, string $propertyPath = null) Assert that two values are the same (using ===) or that the value is null.
166
 * @method static bool nullOrSatisfy(mixed $value, callable $callback, string $message = null, string $propertyPath = null) Assert that the provided value is valid according to a callback or that the value is null.
167
 * @method static bool nullOrScalar(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a PHP scalar or that the value is null.
168
 * @method static bool nullOrStartsWith(mixed $string, string $needle, string $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string starts with a sequence of chars or that the value is null.
169
 * @method static bool nullOrString(mixed $value, string $message = null, string $propertyPath = null) Assert that value is a string or that the value is null.
170
 * @method static bool nullOrSubclassOf(mixed $value, string $className, string $message = null, string $propertyPath = null) Assert that value is subclass of given class-name or that the value is null.
171
 * @method static bool nullOrTrue(mixed $value, string $message = null, string $propertyPath = null) Assert that the value is boolean True or that the value is null.
172
 * @method static bool nullOrUrl(mixed $value, string $message = null, string $propertyPath = null) Assert that value is an URL or that the value is null.
173
 * @method static bool nullOrUuid(string $value, string $message = null, string $propertyPath = null) Assert that the given string is a valid UUID or that the value is null.
174
 * @method static bool nullOrVersion(string $version1, string $operator, string $version2, string $message = null, string $propertyPath = null) Assert that extension is loaded or that the value is null.
175
 * @method static bool nullOrWriteable(string $value, string $message = null, string $propertyPath = null) Assert that the value is something writeable or that the value is null.
176
 */
177
class Assertion
178
{
179
    const INVALID_FLOAT             = 9;
180
    const INVALID_INTEGER           = 10;
181
    const INVALID_DIGIT             = 11;
182
    const INVALID_INTEGERISH        = 12;
183
    const INVALID_BOOLEAN           = 13;
184
    const VALUE_EMPTY               = 14;
185
    const VALUE_NULL                = 15;
186
    const VALUE_NOT_NULL            = 25;
187
    const INVALID_STRING            = 16;
188
    const INVALID_REGEX             = 17;
189
    const INVALID_MIN_LENGTH        = 18;
190
    const INVALID_MAX_LENGTH        = 19;
191
    const INVALID_STRING_START      = 20;
192
    const INVALID_STRING_CONTAINS   = 21;
193
    const INVALID_CHOICE            = 22;
194
    const INVALID_NUMERIC           = 23;
195
    const INVALID_ARRAY             = 24;
196
    const INVALID_KEY_EXISTS        = 26;
197
    const INVALID_NOT_BLANK         = 27;
198
    const INVALID_INSTANCE_OF       = 28;
199
    const INVALID_SUBCLASS_OF       = 29;
200
    const INVALID_RANGE             = 30;
201
    const INVALID_ALNUM             = 31;
202
    const INVALID_TRUE              = 32;
203
    const INVALID_EQ                = 33;
204
    const INVALID_SAME              = 34;
205
    const INVALID_MIN               = 35;
206
    const INVALID_MAX               = 36;
207
    const INVALID_LENGTH            = 37;
208
    const INVALID_FALSE             = 38;
209
    const INVALID_STRING_END        = 39;
210
    const INVALID_UUID              = 40;
211
    const INVALID_COUNT             = 41;
212
    const INVALID_NOT_EQ            = 42;
213
    const INVALID_NOT_SAME          = 43;
214
    const INVALID_TRAVERSABLE       = 44;
215
    const INVALID_ARRAY_ACCESSIBLE  = 45;
216
    const INVALID_KEY_ISSET         = 46;
217
    const INVALID_VALUE_IN_ARRAY    = 47;
218
    const INVALID_E164              = 48;
219
    const INVALID_DIRECTORY         = 101;
220
    const INVALID_FILE              = 102;
221
    const INVALID_READABLE          = 103;
222
    const INVALID_WRITEABLE         = 104;
223
    const INVALID_CLASS             = 105;
224
    const INVALID_INTERFACE         = 106;
225
    const INVALID_EMAIL             = 201;
226
    const INTERFACE_NOT_IMPLEMENTED = 202;
227
    const INVALID_URL               = 203;
228
    const INVALID_NOT_INSTANCE_OF   = 204;
229
    const VALUE_NOT_EMPTY           = 205;
230
    const INVALID_JSON_STRING       = 206;
231
    const INVALID_OBJECT            = 207;
232
    const INVALID_METHOD            = 208;
233
    const INVALID_SCALAR            = 209;
234
    const INVALID_LESS              = 210;
235
    const INVALID_LESS_OR_EQUAL     = 211;
236
    const INVALID_GREATER           = 212;
237
    const INVALID_GREATER_OR_EQUAL  = 213;
238
    const INVALID_DATE              = 214;
239
    const INVALID_CALLABLE          = 215;
240
    const INVALID_KEY_NOT_EXISTS    = 216;
241
    const INVALID_SATISFY           = 217;
242
    const INVALID_IP                = 218;
243
    const INVALID_BETWEEN           = 219;
244
    const INVALID_BETWEEN_EXCLUSIVE = 220;
245
    const INVALID_EXTENSION         = 222;
246
    const INVALID_CONSTANT          = 221;
247
    const INVALID_VERSION           = 223;
248
249
    /**
250
     * Exception to throw when an assertion failed.
251
     *
252
     * @var string
253
     */
254
    protected static $exceptionClass = 'Assert\InvalidArgumentException';
255
256
    /**
257
     * Helper method that handles building the assertion failure exceptions.
258
     * They are returned from this method so that the stack trace still shows
259
     * the assertions method.
260
     *
261
     * @param mixed $value
262
     * @param string $message
263
     * @param int $code
264
     * @param string|null $propertyPath
265
     * @param array $constraints
266
     *
267
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
268
     */
269
    protected static function createException($value, $message, $code, $propertyPath, array $constraints = array())
270
    {
271
        $exceptionClass = static::$exceptionClass;
272
        return new $exceptionClass($message, $code, $propertyPath, $value, $constraints);
273
    }
274
275
    /**
276
     * Assert that two values are equal (using == ).
277
     *
278
     * @param mixed $value
279
     * @param mixed $value2
280
     * @param string|null $message
281
     * @param string|null $propertyPath
282
     * @return bool
283
     * @throws \Assert\AssertionFailedException
284
     */
285
    public static function eq($value, $value2, $message = null, $propertyPath = null)
286
    {
287
        if ($value != $value2) {
288
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
289
                $message ?: 'Value "%s" does not equal expected value "%s".',
290
                static::stringify($value),
291
                static::stringify($value2)
292
            );
293
294
            throw static::createException($value, $message, static::INVALID_EQ, $propertyPath, array('expected' => $value2));
295
        }
296
297
        return true;
298
    }
299
300
    /**
301
     * Assert that two values are the same (using ===).
302
     *
303
     * @param mixed $value
304
     * @param mixed $value2
305
     * @param string|null $message
306
     * @param string|null $propertyPath
307
     * @return bool
308
     * @throws \Assert\AssertionFailedException
309
     */
310
    public static function same($value, $value2, $message = null, $propertyPath = null)
311
    {
312
        if ($value !== $value2) {
313
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
314
                $message ?: 'Value "%s" is not the same as expected value "%s".',
315
                static::stringify($value),
316
                static::stringify($value2)
317
            );
318
319
            throw static::createException($value, $message, static::INVALID_SAME, $propertyPath, array('expected' => $value2));
320
        }
321
322
        return true;
323
    }
324
325
    /**
326
     * Assert that two values are not equal (using == ).
327
     *
328
     * @param mixed $value1
329
     * @param mixed $value2
330
     * @param string|null $message
331
     * @param string|null $propertyPath
332
     * @return bool
333
     * @throws \Assert\AssertionFailedException
334
     */
335
    public static function notEq($value1, $value2, $message = null, $propertyPath = null)
336
    {
337
        if ($value1 == $value2) {
338
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
339
                $message ?: 'Value "%s" is equal to expected value "%s".',
340
                static::stringify($value1),
341
                static::stringify($value2)
342
            );
343
            throw static::createException($value1, $message, static::INVALID_NOT_EQ, $propertyPath, array('expected' => $value2));
344
        }
345
346
        return true;
347
    }
348
349
    /**
350
     * Assert that two values are not the same (using === ).
351
     *
352
     * @param mixed $value1
353
     * @param mixed $value2
354
     * @param string|null $message
355
     * @param string|null $propertyPath
356
     * @return bool
357
     * @throws \Assert\AssertionFailedException
358
     */
359
    public static function notSame($value1, $value2, $message = null, $propertyPath = null)
360
    {
361
        if ($value1 === $value2) {
362
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
363
                $message ?: 'Value "%s" is the same as expected value "%s".',
364
                static::stringify($value1),
365
                static::stringify($value2)
366
            );
367
            throw static::createException($value1, $message, static::INVALID_NOT_SAME, $propertyPath, array('expected' => $value2));
368
        }
369
370
        return true;
371
    }
372
373
    /**
374
     * Assert that value is not in array of choices.
375
     *
376
     * @param mixed $value
377
     * @param array $choices
378
     * @param string|null $message
379
     * @param string|null $propertyPath
380
     * @return bool
381
     * @throws \Assert\AssertionFailedException
382
     */
383
    public static function notInArray($value, array $choices, $message = null, $propertyPath = null)
384
    {
385
        if (in_array($value, $choices) === true) {
386
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
387
                $message ?: 'Value "%s" is in given "%s".',
388
                static::stringify($value),
389
                static::stringify($choices)
390
            );
391
            throw static::createException($value, $message, static::INVALID_VALUE_IN_ARRAY, $propertyPath);
392
        }
393
394
        return true;
395
    }
396
397
    /**
398
     * Assert that value is a php integer.
399
     *
400
     * @param mixed $value
401
     * @param string|null $message
402
     * @param string|null $propertyPath
403
     * @return bool
404
     * @throws \Assert\AssertionFailedException
405
     */
406
    public static function integer($value, $message = null, $propertyPath = null)
407
    {
408
        if (! is_int($value)) {
409
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
410
                $message ?: 'Value "%s" is not an integer.',
411
                static::stringify($value)
412
            );
413
414
            throw static::createException($value, $message, static::INVALID_INTEGER, $propertyPath);
415
        }
416
417
        return true;
418
    }
419
420
    /**
421
     * Assert that value is a php float.
422
     *
423
     * @param mixed $value
424
     * @param string|null $message
425
     * @param string|null $propertyPath
426
     * @return bool
427
     * @throws \Assert\AssertionFailedException
428
     */
429
    public static function float($value, $message = null, $propertyPath = null)
430
    {
431
        if (! is_float($value)) {
432
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
433
                $message ?: 'Value "%s" is not a float.',
434
                static::stringify($value)
435
            );
436
437
            throw static::createException($value, $message, static::INVALID_FLOAT, $propertyPath);
438
        }
439
440
        return true;
441
    }
442
443
    /**
444
     * Validates if an integer or integerish is a digit.
445
     *
446
     * @param mixed $value
447
     * @param string|null $message
448
     * @param string|null $propertyPath
449
     * @return bool
450
     * @throws \Assert\AssertionFailedException
451
     */
452
    public static function digit($value, $message = null, $propertyPath = null)
453
    {
454
        if (! ctype_digit((string)$value)) {
455
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
456
                $message ?: 'Value "%s" is not a digit.',
457
                static::stringify($value)
458
            );
459
460
            throw static::createException($value, $message, static::INVALID_DIGIT, $propertyPath);
461
        }
462
463
        return true;
464
    }
465
466
    /**
467
     * Assert that value is a php integer'ish.
468
     *
469
     * @param mixed $value
470
     * @param string|null $message
471
     * @param string|null $propertyPath
472
     * @return bool
473
     * @throws \Assert\AssertionFailedException
474
     */
475
    public static function integerish($value, $message = null, $propertyPath = null)
476
    {
477
        if (is_object($value) || strval(intval($value)) != $value || is_bool($value) || is_null($value)) {
478
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
479
                $message ?: 'Value "%s" is not an integer or a number castable to integer.',
480
                static::stringify($value)
481
            );
482
483
            throw static::createException($value, $message, static::INVALID_INTEGERISH, $propertyPath);
484
        }
485
486
        return true;
487
    }
488
489
    /**
490
     * Assert that value is php boolean
491
     *
492
     * @param mixed $value
493
     * @param string|null $message
494
     * @param string|null $propertyPath
495
     * @return bool
496
     * @throws \Assert\AssertionFailedException
497
     */
498
    public static function boolean($value, $message = null, $propertyPath = null)
499
    {
500
        if (! is_bool($value)) {
501
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
502
                $message ?: 'Value "%s" is not a boolean.',
503
                static::stringify($value)
504
            );
505
506
            throw static::createException($value, $message, static::INVALID_BOOLEAN, $propertyPath);
507
        }
508
509
        return true;
510
    }
511
512
    /**
513
     * Assert that value is a PHP scalar
514
     *
515
     * @param mixed $value
516
     * @param string|null $message
517
     * @param string|null $propertyPath
518
     * @return bool
519
     * @throws \Assert\AssertionFailedException
520
     */
521
    public static function scalar($value, $message = null, $propertyPath = null)
522
    {
523
        if (!is_scalar($value)) {
524
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
525
                $message ?: 'Value "%s" is not a scalar.',
526
                static::stringify($value)
527
            );
528
529
            throw static::createException($value, $message, static::INVALID_SCALAR, $propertyPath);
530
        }
531
532
        return true;
533
    }
534
535
    /**
536
     * Assert that value is not empty
537
     *
538
     * @param mixed $value
539
     * @param string|null $message
540
     * @param string|null $propertyPath
541
     * @return bool
542
     * @throws \Assert\AssertionFailedException
543
     */
544
    public static function notEmpty($value, $message = null, $propertyPath = null)
545
    {
546
        if (empty($value)) {
547
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
548
                $message ?: 'Value "%s" is empty, but non empty value was expected.',
549
                static::stringify($value)
550
            );
551
552
            throw static::createException($value, $message, static::VALUE_EMPTY, $propertyPath);
553
        }
554
555
        return true;
556
    }
557
558
    /**
559
     * Assert that value is empty
560
     *
561
     * @param mixed $value
562
     * @param string|null $message
563
     * @param string|null $propertyPath
564
     * @return bool
565
     * @throws \Assert\AssertionFailedException
566
     */
567
    public static function noContent($value, $message = null, $propertyPath = null)
568
    {
569
        if (!empty($value)) {
570
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
571
                $message ?: 'Value "%s" is not empty, but empty value was expected.',
572
                static::stringify($value)
573
            );
574
575
            throw static::createException($value, $message, static::VALUE_NOT_EMPTY, $propertyPath);
576
        }
577
578
        return true;
579
    }
580
581
    /**
582
     * Assert that value is null
583
     *
584
     * @param mixed $value
585
     * @param string|null $message
586
     * @param string|null $propertyPath
587
     * @return bool
588
     * @throws \Assert\AssertionFailedException
589
     */
590
    public static function null($value, $message = null, $propertyPath = null)
591
    {
592
        if ($value !== null) {
593
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
594
                $message ?: 'Value "%s" is not null, but null value was expected.',
595
                static::stringify($value)
596
            );
597
598
            throw static::createException($value, $message, static::VALUE_NOT_NULL, $propertyPath);
599
        }
600
601
        return true;
602
    }
603
604
    /**
605
     * Assert that value is not null
606
     *
607
     * @param mixed $value
608
     * @param string|null $message
609
     * @param string|null $propertyPath
610
     * @return bool
611
     * @throws \Assert\AssertionFailedException
612
     */
613
    public static function notNull($value, $message = null, $propertyPath = null)
614
    {
615
        if ($value === null) {
616
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
617
                $message ?: 'Value "%s" is null, but non null value was expected.',
618
                static::stringify($value)
619
            );
620
621
            throw static::createException($value, $message, static::VALUE_NULL, $propertyPath);
622
        }
623
624
        return true;
625
    }
626
627
    /**
628
     * Assert that value is a string
629
     *
630
     * @param mixed $value
631
     * @param string|null $message
632
     * @param string|null $propertyPath
633
     * @return bool
634
     * @throws \Assert\AssertionFailedException
635
     */
636
    public static function string($value, $message = null, $propertyPath = null)
637
    {
638
        if (! is_string($value)) {
639
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
640
                $message ?: 'Value "%s" expected to be string, type %s given.',
641
                static::stringify($value),
642
                gettype($value)
643
            );
644
645
            throw static::createException($value, $message, static::INVALID_STRING, $propertyPath);
646
        }
647
648
        return true;
649
    }
650
651
    /**
652
     * Assert that value matches a regex
653
     *
654
     * @param mixed $value
655
     * @param string $pattern
656
     * @param string|null $message
657
     * @param string|null $propertyPath
658
     * @return bool
659
     * @throws \Assert\AssertionFailedException
660
     */
661
    public static function regex($value, $pattern, $message = null, $propertyPath = null)
662
    {
663
        static::string($value, $message, $propertyPath);
664
665
        if (! preg_match($pattern, $value)) {
666
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
667
                $message ?: 'Value "%s" does not match expression.',
668
                static::stringify($value)
669
            );
670
671
            throw static::createException($value, $message, static::INVALID_REGEX, $propertyPath, array('pattern' => $pattern));
672
        }
673
674
        return true;
675
    }
676
677
    /**
678
     * Assert that string has a given length.
679
     *
680
     * @param mixed $value
681
     * @param int $length
682
     * @param string|null $message
683
     * @param string|null $propertyPath
684
     * @param string $encoding
685
     * @return bool
686
     * @throws \Assert\AssertionFailedException
687
     */
688
    public static function length($value, $length, $message = null, $propertyPath = null, $encoding = 'utf8')
689
    {
690
        static::string($value, $message, $propertyPath);
691
692
        if (mb_strlen($value, $encoding) !== $length) {
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" has to be %d exactly characters long, but length is %d.',
695
                static::stringify($value),
696
                $length,
697
                mb_strlen($value, $encoding)
698
            );
699
700
            $constraints = array('length' => $length, 'encoding' => $encoding);
701
            throw static::createException($value, $message, static::INVALID_LENGTH, $propertyPath, $constraints);
702
        }
703
704
        return true;
705
    }
706
707
    /**
708
     * Assert that a string is at least $minLength chars long.
709
     *
710
     * @param mixed $value
711
     * @param int $minLength
712
     * @param string|null $message
713
     * @param string|null $propertyPath
714
     * @param string $encoding
715
     * @return bool
716
     * @throws \Assert\AssertionFailedException
717
     */
718
    public static function minLength($value, $minLength, $message = null, $propertyPath = null, $encoding = 'utf8')
719
    {
720
        static::string($value, $message, $propertyPath);
721
722
        if (mb_strlen($value, $encoding) < $minLength) {
723
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
724
                $message ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
725
                static::stringify($value),
726
                $minLength,
727
                mb_strlen($value, $encoding)
728
            );
729
730
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
731
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
732
        }
733
734
        return true;
735
    }
736
737
    /**
738
     * Assert that string value is not longer than $maxLength chars.
739
     *
740
     * @param mixed $value
741
     * @param integer $maxLength
742
     * @param string|null $message
743
     * @param string|null $propertyPath
744
     * @param string $encoding
745
     * @return bool
746
     * @throws \Assert\AssertionFailedException
747
     */
748
    public static function maxLength($value, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
749
    {
750
        static::string($value, $message, $propertyPath);
751
752
        if (mb_strlen($value, $encoding) > $maxLength) {
753
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
754
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
755
                static::stringify($value),
756
                $maxLength,
757
                mb_strlen($value, $encoding)
758
            );
759
760
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
761
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
762
        }
763
764
        return true;
765
    }
766
767
    /**
768
     * Assert that string length is between min,max lengths.
769
     *
770
     * @param mixed $value
771
     * @param integer $minLength
772
     * @param integer $maxLength
773
     * @param string|null $message
774
     * @param string|null $propertyPath
775
     * @param string $encoding
776
     * @return bool
777
     * @throws \Assert\AssertionFailedException
778
     */
779
    public static function betweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = 'utf8')
780
    {
781
        static::string($value, $message, $propertyPath);
782
783
        if (mb_strlen($value, $encoding) < $minLength) {
784
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
785
                $message ?: 'Value "%s" is too short, it should have at least %d characters, but only has %d characters.',
786
                static::stringify($value),
787
                $minLength,
788
                mb_strlen($value, $encoding)
789
            );
790
791
            $constraints = array('min_length' => $minLength, 'encoding' => $encoding);
792
            throw static::createException($value, $message, static::INVALID_MIN_LENGTH, $propertyPath, $constraints);
793
        }
794
795
        if (mb_strlen($value, $encoding) > $maxLength) {
796
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
797
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
798
                static::stringify($value),
799
                $maxLength,
800
                mb_strlen($value, $encoding)
801
            );
802
803
            $constraints = array('max_length' => $maxLength, 'encoding' => $encoding);
804
            throw static::createException($value, $message, static::INVALID_MAX_LENGTH, $propertyPath, $constraints);
805
        }
806
807
        return true;
808
    }
809
810
    /**
811
     * Assert that string starts with a sequence of chars.
812
     *
813
     * @param mixed $string
814
     * @param string $needle
815
     * @param string|null $message
816
     * @param string|null $propertyPath
817
     * @param string $encoding
818
     * @return bool
819
     * @throws \Assert\AssertionFailedException
820
     */
821
    public static function startsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
822
    {
823
        static::string($string, $message, $propertyPath);
824
825
        if (mb_strpos($string, $needle, null, $encoding) !== 0) {
826
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
827
                $message ?: 'Value "%s" does not start with "%s".',
828
                static::stringify($string),
829
                static::stringify($needle)
830
            );
831
832
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
833
            throw static::createException($string, $message, static::INVALID_STRING_START, $propertyPath, $constraints);
834
        }
835
836
        return true;
837
    }
838
839
    /**
840
     * Assert that string ends with a sequence of chars.
841
     *
842
     * @param mixed $string
843
     * @param string $needle
844
     * @param string|null $message
845
     * @param string|null $propertyPath
846
     * @param string $encoding
847
     * @return bool
848
     * @throws \Assert\AssertionFailedException
849
     */
850
    public static function endsWith($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
851
    {
852
        static::string($string, $message, $propertyPath);
853
854
        $stringPosition = mb_strlen($string, $encoding) - mb_strlen($needle, $encoding);
855
856
        if (mb_strripos($string, $needle, null, $encoding) !== $stringPosition) {
857
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
858
                $message ?: 'Value "%s" does not end with "%s".',
859
                static::stringify($string),
860
                static::stringify($needle)
861
            );
862
863
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
864
            throw static::createException($string, $message, static::INVALID_STRING_END, $propertyPath, $constraints);
865
        }
866
867
        return true;
868
    }
869
870
    /**
871
     * Assert that string contains a sequence of chars.
872
     *
873
     * @param mixed $string
874
     * @param string $needle
875
     * @param string|null $message
876
     * @param string|null $propertyPath
877
     * @param string $encoding
878
     * @return bool
879
     * @throws \Assert\AssertionFailedException
880
     */
881
    public static function contains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
882
    {
883
        static::string($string, $message, $propertyPath);
884
885
        if (mb_strpos($string, $needle, null, $encoding) === false) {
886
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
887
                $message ?: 'Value "%s" does not contain "%s".',
888
                static::stringify($string),
889
                static::stringify($needle)
890
            );
891
892
            $constraints = array('needle' => $needle, 'encoding' => $encoding);
893
            throw static::createException($string, $message, static::INVALID_STRING_CONTAINS, $propertyPath, $constraints);
894
        }
895
896
        return true;
897
    }
898
899
    /**
900
     * Assert that value is in array of choices.
901
     *
902
     * @param mixed $value
903
     * @param array $choices
904
     * @param string|null $message
905
     * @param string|null $propertyPath
906
     * @return bool
907
     * @throws \Assert\AssertionFailedException
908
     */
909
    public static function choice($value, array $choices, $message = null, $propertyPath = null)
910
    {
911
        if (! in_array($value, $choices, true)) {
912
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
913
                $message ?: 'Value "%s" is not an element of the valid values: %s',
914
                static::stringify($value),
915
                implode(", ", array_map(array(get_called_class(), 'stringify'), $choices))
916
            );
917
918
            throw static::createException($value, $message, static::INVALID_CHOICE, $propertyPath, array('choices' => $choices));
919
        }
920
921
        return true;
922
    }
923
924
    /**
925
     * Alias of {@see choice()}
926
     *
927
     * @param mixed $value
928
     * @param array $choices
929
     * @param string|null $message
930
     * @param string|null $propertyPath
931
     * @return bool
932
     */
933
    public static function inArray($value, array $choices, $message = null, $propertyPath = null)
934
    {
935
        return static::choice($value, $choices, $message, $propertyPath);
936
    }
937
938
    /**
939
     * Assert that value is numeric.
940
     *
941
     * @param mixed $value
942
     * @param string|null $message
943
     * @param string|null $propertyPath
944
     * @return bool
945
     * @throws \Assert\AssertionFailedException
946
     */
947
    public static function numeric($value, $message = null, $propertyPath = null)
948
    {
949
        if (! is_numeric($value)) {
950
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
951
                $message ?: 'Value "%s" is not numeric.',
952
                static::stringify($value)
953
            );
954
955
            throw static::createException($value, $message, static::INVALID_NUMERIC, $propertyPath);
956
        }
957
958
        return true;
959
    }
960
961
    /**
962
     * Assert that value is an array.
963
     *
964
     * @param mixed $value
965
     * @param string|null $message
966
     * @param string|null $propertyPath
967
     * @return bool
968
     * @throws \Assert\AssertionFailedException
969
     */
970
    public static function isArray($value, $message = null, $propertyPath = null)
971
    {
972
        if (! is_array($value)) {
973
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
974
                $message ?: 'Value "%s" is not an array.',
975
                static::stringify($value)
976
            );
977
978
            throw static::createException($value, $message, static::INVALID_ARRAY, $propertyPath);
979
        }
980
981
        return true;
982
    }
983
984
    /**
985
     * Assert that value is an array or a traversable object.
986
     *
987
     * @param mixed $value
988
     * @param string|null $message
989
     * @param string|null $propertyPath
990
     * @return bool
991
     * @throws \Assert\AssertionFailedException
992
     */
993
    public static function isTraversable($value, $message = null, $propertyPath = null)
994
    {
995
        if (! is_array($value) && ! $value instanceof \Traversable) {
996
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
997
                $message ?: 'Value "%s" is not an array and does not implement Traversable.',
998
                static::stringify($value)
999
            );
1000
1001
            throw static::createException($value, $message, static::INVALID_TRAVERSABLE, $propertyPath);
1002
        }
1003
1004
        return true;
1005
    }
1006
1007
    /**
1008
     * Assert that value is an array or an array-accessible object.
1009
     *
1010
     * @param mixed $value
1011
     * @param string|null $message
1012
     * @param string|null $propertyPath
1013
     * @return bool
1014
     * @throws \Assert\AssertionFailedException
1015
     */
1016
    public static function isArrayAccessible($value, $message = null, $propertyPath = null)
1017
    {
1018
        if (! is_array($value) && ! $value instanceof \ArrayAccess) {
1019
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1020
                $message ?: 'Value "%s" is not an array and does not implement ArrayAccess.',
1021
                static::stringify($value)
1022
            );
1023
1024
            throw static::createException($value, $message, static::INVALID_ARRAY_ACCESSIBLE, $propertyPath);
1025
        }
1026
1027
        return true;
1028
    }
1029
1030
    /**
1031
     * Assert that key exists in an array
1032
     *
1033
     * @param mixed $value
1034
     * @param string|integer $key
1035
     * @param string|null $message
1036
     * @param string|null $propertyPath
1037
     * @return bool
1038
     * @throws \Assert\AssertionFailedException
1039
     */
1040
    public static function keyExists($value, $key, $message = null, $propertyPath = null)
1041
    {
1042
        static::isArray($value, $message, $propertyPath);
1043
1044
        if (! array_key_exists($key, $value)) {
1045
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1046
                $message ?: 'Array does not contain an element with key "%s"',
1047
                static::stringify($key)
1048
            );
1049
1050
            throw static::createException($value, $message, static::INVALID_KEY_EXISTS, $propertyPath, array('key' => $key));
1051
        }
1052
1053
        return true;
1054
    }
1055
1056
    /**
1057
     * Assert that key does not exist in an array
1058
     *
1059
     * @param mixed $value
1060
     * @param string|integer $key
1061
     * @param string|null $message
1062
     * @param string|null $propertyPath
1063
     * @return bool
1064
     * @throws \Assert\AssertionFailedException
1065
     */
1066
    public static function keyNotExists($value, $key, $message = null, $propertyPath = null)
1067
    {
1068
        static::isArray($value, $message, $propertyPath);
1069
1070
        if (array_key_exists($key, $value)) {
1071
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1072
                $message ?: 'Array contains an element with key "%s"',
1073
                static::stringify($key)
1074
            );
1075
1076
            throw static::createException($value, $message, static::INVALID_KEY_NOT_EXISTS, $propertyPath, array('key' => $key));
1077
        }
1078
1079
        return true;
1080
    }
1081
1082
    /**
1083
     * Assert that key exists in an array/array-accessible object using isset()
1084
     *
1085
     * @param mixed $value
1086
     * @param string|integer $key
1087
     * @param string|null $message
1088
     * @param string|null $propertyPath
1089
     * @return bool
1090
     * @throws \Assert\AssertionFailedException
1091
     */
1092
    public static function keyIsset($value, $key, $message = null, $propertyPath = null)
1093
    {
1094
        static::isArrayAccessible($value, $message, $propertyPath);
1095
1096
        if (! isset($value[$key])) {
1097
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1098
                $message ?: 'The element with key "%s" was not found',
1099
                static::stringify($key)
1100
            );
1101
1102
            throw static::createException($value, $message, static::INVALID_KEY_ISSET, $propertyPath, array('key' => $key));
1103
        }
1104
1105
        return true;
1106
    }
1107
1108
    /**
1109
     * Assert that key exists in an array/array-accessible object and its value is not empty.
1110
     *
1111
     * @param mixed $value
1112
     * @param string|integer $key
1113
     * @param string|null $message
1114
     * @param string|null $propertyPath
1115
     * @return bool
1116
     * @throws \Assert\AssertionFailedException
1117
     */
1118
    public static function notEmptyKey($value, $key, $message = null, $propertyPath = null)
1119
    {
1120
        static::keyIsset($value, $key, $message, $propertyPath);
1121
        static::notEmpty($value[$key], $message, $propertyPath);
1122
1123
        return true;
1124
    }
1125
1126
    /**
1127
     * Assert that value is not blank
1128
     *
1129
     * @param mixed $value
1130
     * @param string|null $message
1131
     * @param string|null $propertyPath
1132
     * @return bool
1133
     * @throws \Assert\AssertionFailedException
1134
     */
1135
    public static function notBlank($value, $message = null, $propertyPath = null)
1136
    {
1137
        if (false === $value || (empty($value) && '0' != $value) || (is_string($value) && '' === trim($value))) {
1138
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1139
                $message ?: 'Value "%s" is blank, but was expected to contain a value.',
1140
                static::stringify($value)
1141
            );
1142
1143
            throw static::createException($value, $message, static::INVALID_NOT_BLANK, $propertyPath);
1144
        }
1145
1146
        return true;
1147
    }
1148
1149
    /**
1150
     * Assert that value is instance of given class-name.
1151
     *
1152
     * @param mixed $value
1153
     * @param string $className
1154
     * @param string|null $message
1155
     * @param string|null $propertyPath
1156
     * @return bool
1157
     * @throws \Assert\AssertionFailedException
1158
     */
1159
    public static function isInstanceOf($value, $className, $message = null, $propertyPath = null)
1160
    {
1161
        if (! ($value instanceof $className)) {
1162
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1163
                $message ?: 'Class "%s" was expected to be instanceof of "%s" but is not.',
1164
                static::stringify($value),
1165
                $className
1166
            );
1167
1168
            throw static::createException($value, $message, static::INVALID_INSTANCE_OF, $propertyPath, array('class' => $className));
1169
        }
1170
1171
        return true;
1172
    }
1173
1174
    /**
1175
     * Assert that value is not instance of given class-name.
1176
     *
1177
     * @param mixed $value
1178
     * @param string $className
1179
     * @param string|null $message
1180
     * @param string|null $propertyPath
1181
     * @return bool
1182
     * @throws \Assert\AssertionFailedException
1183
     */
1184
    public static function notIsInstanceOf($value, $className, $message = null, $propertyPath = null)
1185
    {
1186
        if ($value instanceof $className) {
1187
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1188
                $message ?: 'Class "%s" was not expected to be instanceof of "%s".',
1189
                static::stringify($value),
1190
                $className
1191
            );
1192
1193
            throw static::createException($value, $message, static::INVALID_NOT_INSTANCE_OF, $propertyPath, array('class' => $className));
1194
        }
1195
1196
        return true;
1197
    }
1198
1199
    /**
1200
     * Assert that value is subclass of given class-name.
1201
     *
1202
     * @param mixed $value
1203
     * @param string $className
1204
     * @param string|null $message
1205
     * @param string|null $propertyPath
1206
     * @return bool
1207
     * @throws \Assert\AssertionFailedException
1208
     */
1209
    public static function subclassOf($value, $className, $message = null, $propertyPath = null)
1210
    {
1211
        if (! is_subclass_of($value, $className)) {
1212
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1213
                $message ?: 'Class "%s" was expected to be subclass of "%s".',
1214
                static::stringify($value),
1215
                $className
1216
            );
1217
1218
            throw static::createException($value, $message, static::INVALID_SUBCLASS_OF, $propertyPath, array('class' => $className));
1219
        }
1220
1221
        return true;
1222
    }
1223
1224
    /**
1225
     * Assert that value is in range of numbers.
1226
     *
1227
     * @param mixed $value
1228
     * @param mixed $minValue
1229
     * @param mixed $maxValue
1230
     * @param string|null $message
1231
     * @param string|null $propertyPath
1232
     * @return bool
1233
     * @throws \Assert\AssertionFailedException
1234
     */
1235
    public static function range($value, $minValue, $maxValue, $message = null, $propertyPath = null)
1236
    {
1237
        static::numeric($value, $message, $propertyPath);
1238
1239
        if ($value < $minValue || $value > $maxValue) {
1240
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1241
                $message ?: 'Number "%s" was expected to be at least "%d" and at most "%d".',
1242
                static::stringify($value),
1243
                static::stringify($minValue),
1244
                static::stringify($maxValue)
1245
            );
1246
1247
            throw static::createException($value, $message, static::INVALID_RANGE, $propertyPath, array('min' => $minValue, 'max' => $maxValue));
1248
        }
1249
1250
        return true;
1251
    }
1252
1253
    /**
1254
     * Assert that a value is at least as big as a given limit
1255
     *
1256
     * @param mixed $value
1257
     * @param mixed $minValue
1258
     * @param string|null $message
1259
     * @param string|null $propertyPath
1260
     * @return bool
1261
     * @throws \Assert\AssertionFailedException
1262
     */
1263
    public static function min($value, $minValue, $message = null, $propertyPath = null)
1264
    {
1265
        static::numeric($value, $message, $propertyPath);
1266
1267
        if ($value < $minValue) {
1268
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1269
                $message ?: 'Number "%s" was expected to be at least "%s".',
1270
                static::stringify($value),
1271
                static::stringify($minValue)
1272
            );
1273
1274
            throw static::createException($value, $message, static::INVALID_MIN, $propertyPath, array('min' => $minValue));
1275
        }
1276
1277
        return true;
1278
    }
1279
1280
    /**
1281
     * Assert that a number is smaller as a given limit
1282
     *
1283
     * @param mixed $value
1284
     * @param mixed $maxValue
1285
     * @param string|null $message
1286
     * @param string|null $propertyPath
1287
     * @return bool
1288
     * @throws \Assert\AssertionFailedException
1289
     */
1290
    public static function max($value, $maxValue, $message = null, $propertyPath = null)
1291
    {
1292
        static::numeric($value, $message, $propertyPath);
1293
1294
        if ($value > $maxValue) {
1295
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1296
                $message ?: 'Number "%s" was expected to be at most "%s".',
1297
                static::stringify($value),
1298
                static::stringify($maxValue)
1299
            );
1300
1301
            throw static::createException($value, $message, static::INVALID_MAX, $propertyPath, array('max' => $maxValue));
1302
        }
1303
1304
        return true;
1305
    }
1306
1307
    /**
1308
     * Assert that a file exists
1309
     *
1310
     * @param string $value
1311
     * @param string|null $message
1312
     * @param string|null $propertyPath
1313
     * @return bool
1314
     * @throws \Assert\AssertionFailedException
1315
     */
1316
    public static function file($value, $message = null, $propertyPath = null)
1317
    {
1318
        static::string($value, $message, $propertyPath);
1319
        static::notEmpty($value, $message, $propertyPath);
1320
1321
        if (! is_file($value)) {
1322
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1323
                $message ?: 'File "%s" was expected to exist.',
1324
                static::stringify($value)
1325
            );
1326
1327
            throw static::createException($value, $message, static::INVALID_FILE, $propertyPath);
1328
        }
1329
1330
        return true;
1331
    }
1332
1333
    /**
1334
     * Assert that a directory exists
1335
     *
1336
     * @param string $value
1337
     * @param string|null $message
1338
     * @param string|null $propertyPath
1339
     * @return bool
1340
     * @throws \Assert\AssertionFailedException
1341
     */
1342
    public static function directory($value, $message = null, $propertyPath = null)
1343
    {
1344
        static::string($value, $message, $propertyPath);
1345
1346
        if (! is_dir($value)) {
1347
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1348
                $message ?: 'Path "%s" was expected to be a directory.',
1349
                static::stringify($value)
1350
            );
1351
1352
            throw static::createException($value, $message, static::INVALID_DIRECTORY, $propertyPath);
1353
        }
1354
1355
        return true;
1356
    }
1357
1358
    /**
1359
     * Assert that the value is something readable
1360
     *
1361
     * @param string $value
1362
     * @param string|null $message
1363
     * @param string|null $propertyPath
1364
     * @return bool
1365
     * @throws \Assert\AssertionFailedException
1366
     */
1367
    public static function readable($value, $message = null, $propertyPath = null)
1368
    {
1369
        static::string($value, $message, $propertyPath);
1370
1371
        if (! is_readable($value)) {
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 ?: 'Path "%s" was expected to be readable.',
1374
                static::stringify($value)
1375
            );
1376
1377
            throw static::createException($value, $message, static::INVALID_READABLE, $propertyPath);
1378
        }
1379
1380
        return true;
1381
    }
1382
1383
    /**
1384
     * Assert that the value is something writeable
1385
     *
1386
     * @param string $value
1387
     * @param string|null $message
1388
     * @param string|null $propertyPath
1389
     * @return bool
1390
     * @throws \Assert\AssertionFailedException
1391
     */
1392
    public static function writeable($value, $message = null, $propertyPath = null)
1393
    {
1394
        static::string($value, $message, $propertyPath);
1395
1396
        if (! is_writeable($value)) {
1397
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1398
                $message ?: 'Path "%s" was expected to be writeable.',
1399
                static::stringify($value)
1400
            );
1401
1402
            throw static::createException($value, $message, static::INVALID_WRITEABLE, $propertyPath);
1403
        }
1404
1405
        return true;
1406
    }
1407
1408
    /**
1409
     * Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL).
1410
     *
1411
     * @param mixed $value
1412
     * @param string|null $message
1413
     * @param string|null $propertyPath
1414
     * @return bool
1415
     * @throws \Assert\AssertionFailedException
1416
     */
1417
    public static function email($value, $message = null, $propertyPath = null)
1418
    {
1419
        static::string($value, $message, $propertyPath);
1420
1421
        if (! filter_var($value, FILTER_VALIDATE_EMAIL)) {
1422
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1423
                $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1424
                static::stringify($value)
1425
            );
1426
1427
            throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1428
        } else {
1429
            $host = substr($value, strpos($value, '@') + 1);
1430
1431
            // Likely not a FQDN, bug in PHP FILTER_VALIDATE_EMAIL prior to PHP 5.3.3
1432
            if (version_compare(PHP_VERSION, '5.3.3', '<') && strpos($host, '.') === false) {
1433
                $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1434
                    $message ?: 'Value "%s" was expected to be a valid e-mail address.',
1435
                    static::stringify($value)
1436
                );
1437
1438
                throw static::createException($value, $message, static::INVALID_EMAIL, $propertyPath);
1439
            }
1440
        }
1441
1442
        return true;
1443
    }
1444
1445
    /**
1446
     * Assert that value is an URL.
1447
     *
1448
     * This code snipped was taken from the Symfony project and modified to the special demands of this method.
1449
     *
1450
     * @param mixed $value
1451
     * @param string|null $message
1452
     * @param string|null $propertyPath
1453
     * @return bool
1454
     * @throws \Assert\AssertionFailedException
1455
     *
1456
     *
1457
     * @link https://github.com/symfony/Validator/blob/master/Constraints/UrlValidator.php
1458
     * @link https://github.com/symfony/Validator/blob/master/Constraints/Url.php
1459
     */
1460
    public static function url($value, $message = null, $propertyPath = null)
1461
    {
1462
        static::string($value, $message, $propertyPath);
1463
1464
        $protocols = array('http', 'https');
1465
1466
        $pattern = '~^
1467
            (%s)://                                 # protocol
1468
            (([\pL\pN-]+:)?([\pL\pN-]+)@)?          # basic auth
1469
            (
1470
                ([\pL\pN\pS-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
1471
                    |                                                 # or
1472
                \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}                    # an IP address
1473
                    |                                                 # or
1474
                \[
1475
                    (?:(?:(?:(?:(?:(?:(?:[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})))?::))))
1476
                \]  # an IPv6 address
1477
            )
1478
            (:[0-9]+)?                              # a port (optional)
1479
            (/?|/\S+|\?\S*|\#\S*)                   # a /, nothing, a / with something, a query or a fragment
1480
        $~ixu';
1481
1482
        $pattern = sprintf($pattern, implode('|', $protocols));
1483
1484
        if (!preg_match($pattern, $value)) {
1485
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1486
                $message ?: 'Value "%s" was expected to be a valid URL starting with http or https',
1487
                static::stringify($value)
1488
            );
1489
1490
            throw static::createException($value, $message, static::INVALID_URL, $propertyPath);
1491
        }
1492
1493
        return true;
1494
    }
1495
1496
    /**
1497
     * Assert that value is alphanumeric.
1498
     *
1499
     * @param mixed $value
1500
     * @param string|null $message
1501
     * @param string|null $propertyPath
1502
     * @return bool
1503
     * @throws \Assert\AssertionFailedException
1504
     */
1505
    public static function alnum($value, $message = null, $propertyPath = null)
1506
    {
1507
        try {
1508
            static::regex($value, '(^([a-zA-Z]{1}[a-zA-Z0-9]*)$)', $message, $propertyPath);
1509
        } catch (AssertionFailedException $e) {
1510
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1511
                $message ?: 'Value "%s" is not alphanumeric, starting with letters and containing only letters and numbers.',
1512
                static::stringify($value)
1513
            );
1514
1515
            throw static::createException($value, $message, static::INVALID_ALNUM, $propertyPath);
1516
        }
1517
1518
        return true;
1519
    }
1520
1521
    /**
1522
     * Assert that the value is boolean True.
1523
     *
1524
     * @param mixed $value
1525
     * @param string|null $message
1526
     * @param string|null $propertyPath
1527
     * @return bool
1528
     * @throws \Assert\AssertionFailedException
1529
     */
1530
    public static function true($value, $message = null, $propertyPath = null)
1531
    {
1532
        if ($value !== true) {
1533
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1534
                $message ?: 'Value "%s" is not TRUE.',
1535
                static::stringify($value)
1536
            );
1537
1538
            throw static::createException($value, $message, static::INVALID_TRUE, $propertyPath);
1539
        }
1540
1541
        return true;
1542
    }
1543
1544
    /**
1545
     * Assert that the value is boolean False.
1546
     *
1547
     * @param mixed $value
1548
     * @param string|null $message
1549
     * @param string|null $propertyPath
1550
     * @return bool
1551
     * @throws \Assert\AssertionFailedException
1552
     */
1553
    public static function false($value, $message = null, $propertyPath = null)
1554
    {
1555
        if ($value !== false) {
1556
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1557
                $message ?: 'Value "%s" is not FALSE.',
1558
                static::stringify($value)
1559
            );
1560
1561
            throw static::createException($value, $message, static::INVALID_FALSE, $propertyPath);
1562
        }
1563
1564
        return true;
1565
    }
1566
1567
    /**
1568
     * Assert that the class exists.
1569
     *
1570
     * @param mixed $value
1571
     * @param string|null $message
1572
     * @param string|null $propertyPath
1573
     * @return bool
1574
     * @throws \Assert\AssertionFailedException
1575
     */
1576
    public static function classExists($value, $message = null, $propertyPath = null)
1577
    {
1578
        if (! class_exists($value)) {
1579
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1580
                $message ?: 'Class "%s" does not exist.',
1581
                static::stringify($value)
1582
            );
1583
1584
            throw static::createException($value, $message, static::INVALID_CLASS, $propertyPath);
1585
        }
1586
1587
        return true;
1588
    }
1589
1590
    /**
1591
     * Assert that the interface exists.
1592
     *
1593
     * @param mixed $value
1594
     * @param string|null $message
1595
     * @param string|null $propertyPath
1596
     * @return bool
1597
     * @throws \Assert\AssertionFailedException
1598
     */
1599
    public static function interfaceExists($value, $message = null, $propertyPath = null)
1600
    {
1601
        if (! interface_exists($value)) {
1602
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1603
                $message ?: 'Interface "%s" does not exist.',
1604
                static::stringify($value)
1605
            );
1606
1607
            throw static::createException($value, $message, static::INVALID_INTERFACE, $propertyPath);
1608
        }
1609
1610
        return true;
1611
    }
1612
1613
    /**
1614
     * Assert that the class implements the interface
1615
     *
1616
     * @param mixed $class
1617
     * @param string $interfaceName
1618
     * @param string|null $message
1619
     * @param string|null $propertyPath
1620
     * @return bool
1621
     * @throws \Assert\AssertionFailedException
1622
     */
1623
    public static function implementsInterface($class, $interfaceName, $message = null, $propertyPath = null)
1624
    {
1625
        $reflection = new \ReflectionClass($class);
1626
        if (! $reflection->implementsInterface($interfaceName)) {
1627
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1628
                $message ?: 'Class "%s" does not implement interface "%s".',
1629
                static::stringify($class),
1630
                static::stringify($interfaceName)
1631
            );
1632
1633
            throw static::createException($class, $message, static::INTERFACE_NOT_IMPLEMENTED, $propertyPath, array('interface' => $interfaceName));
1634
        }
1635
1636
        return true;
1637
    }
1638
1639
    /**
1640
     * Assert that the given string is a valid json string.
1641
     *
1642
     * NOTICE:
1643
     * Since this does a json_decode to determine its validity
1644
     * you probably should consider, when using the variable
1645
     * content afterwards, just to decode and check for yourself instead
1646
     * of using this assertion.
1647
     *
1648
     * @param mixed $value
1649
     * @param string|null $message
1650
     * @param string|null $propertyPath
1651
     * @return bool
1652
     * @throws \Assert\AssertionFailedException
1653
     */
1654
    public static function isJsonString($value, $message = null, $propertyPath = null)
1655
    {
1656
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
1657
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1658
                $message ?: 'Value "%s" is not a valid JSON string.',
1659
                static::stringify($value)
1660
            );
1661
1662
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
1663
        }
1664
1665
        return true;
1666
    }
1667
1668
    /**
1669
     * Assert that the given string is a valid UUID
1670
     *
1671
     * Uses code from {@link https://github.com/ramsey/uuid} that is MIT licensed.
1672
     *
1673
     * @param string $value
1674
     * @param string|null $message
1675
     * @param string|null $propertyPath
1676
     * @return bool
1677
     * @throws \Assert\AssertionFailedException
1678
     */
1679
    public static function uuid($value, $message = null, $propertyPath = null)
1680
    {
1681
        $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...
1682
1683
        if ($value === '00000000-0000-0000-0000-000000000000') {
1684
            return true;
1685
        }
1686
1687
        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)) {
1688
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1689
                $message ?: 'Value "%s" is not a valid UUID.',
1690
                static::stringify($value)
1691
            );
1692
1693
            throw static::createException($value, $message, static::INVALID_UUID, $propertyPath);
1694
        }
1695
1696
        return true;
1697
    }
1698
1699
    /**
1700
     * Assert that the given string is a valid E164 Phone Number
1701
     *
1702
     * @link https://en.wikipedia.org/wiki/E.164
1703
     *
1704
     * @param string $value
1705
     * @param string|null $message
1706
     * @param string|null $propertyPath
1707
     * @return bool
1708
     * @throws \Assert\AssertionFailedException
1709
     */
1710
    public static function e164($value, $message = null, $propertyPath = null)
1711
    {
1712
        if (!preg_match('/^\+?[1-9]\d{1,14}$/', $value)) {
1713
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1714
                $message ?: 'Value "%s" is not a valid E164.',
1715
                static::stringify($value)
1716
            );
1717
1718
            throw static::createException($value, $message, static::INVALID_E164, $propertyPath);
1719
        }
1720
1721
        return true;
1722
    }
1723
1724
    /**
1725
     * Assert that the count of countable is equal to count.
1726
     *
1727
     * @param array|\Countable $countable
1728
     * @param int              $count
1729
     * @param string|null           $message
1730
     * @param string|null           $propertyPath
1731
     * @return bool
1732
     * @throws \Assert\AssertionFailedException
1733
     */
1734
    public static function count($countable, $count, $message = null, $propertyPath = null)
1735
    {
1736
        if ($count !== count($countable)) {
1737
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1738
                $message ?: 'List does not contain exactly "%d" elements.',
1739
                static::stringify($count)
1740
            );
1741
1742
            throw static::createException($countable, $message, static::INVALID_COUNT, $propertyPath, array('count' => $count));
1743
        }
1744
1745
        return true;
1746
    }
1747
1748
    /**
1749
     * static call handler to implement:
1750
     *  - "null or assertion" delegation
1751
     *  - "all" delegation
1752
     *
1753
     * @param string $method
1754
     * @param array $args
1755
     *
1756
     * @return bool|mixed
1757
     */
1758
    public static function __callStatic($method, $args)
1759
    {
1760
        if (strpos($method, "nullOr") === 0) {
1761
            if (! array_key_exists(0, $args)) {
1762
                throw new BadMethodCallException("Missing the first argument.");
1763
            }
1764
1765
            if ($args[0] === null) {
1766
                return true;
1767
            }
1768
1769
            $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...
1770
1771
            return call_user_func_array(array(get_called_class(), $method), $args);
1772
        }
1773
1774
        if (strpos($method, "all") === 0) {
1775
            if (! array_key_exists(0, $args)) {
1776
                throw new BadMethodCallException("Missing the first argument.");
1777
            }
1778
1779
            static::isTraversable($args[0]);
1780
1781
            $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...
1782
            $values      = array_shift($args);
1783
            $calledClass = get_called_class();
1784
1785
            foreach ($values as $value) {
1786
                call_user_func_array(array($calledClass, $method), array_merge(array($value), $args));
1787
            }
1788
1789
            return true;
1790
        }
1791
1792
        throw new BadMethodCallException("No assertion Assertion#" . $method . " exists.");
1793
    }
1794
1795
    /**
1796
     * Determines if the values array has every choice as key and that this choice has content.
1797
     *
1798
     * @param array $values
1799
     * @param array $choices
1800
     * @param string|null $message
1801
     * @param string|null $propertyPath
1802
     * @return bool
1803
     */
1804
    public static function choicesNotEmpty(array $values, array $choices, $message = null, $propertyPath = null)
1805
    {
1806
        static::notEmpty($values, $message, $propertyPath);
1807
1808
        foreach ($choices as $choice) {
1809
            static::notEmptyKey($values, $choice, $message, $propertyPath);
1810
        }
1811
1812
        return true;
1813
    }
1814
1815
    /**
1816
     * Determines that the named method is defined in the provided object.
1817
     *
1818
     * @param string $value
1819
     * @param mixed  $object
1820
     * @param string|null $message
1821
     * @param string|null $propertyPath
1822
     * @return bool
1823
     * @throws
1824
     */
1825
    public static function methodExists($value, $object, $message = null, $propertyPath = null)
1826
    {
1827
        static::isObject($object, $message, $propertyPath);
1828
1829
        if (!method_exists($object, $value)) {
1830
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1831
                $message ?: 'Expected "%s" does not exist in provided object.',
1832
                static::stringify($value)
1833
            );
1834
1835
            throw static::createException($value, $message, static::INVALID_METHOD, $propertyPath);
1836
        }
1837
1838
        return true;
1839
    }
1840
1841
    /**
1842
     * Determines that the provided value is an object.
1843
     *
1844
     * @param mixed $value
1845
     * @param string|null $message
1846
     * @param string|null $propertyPath
1847
     * @return bool
1848
     */
1849
    public static function isObject($value, $message = null, $propertyPath = null)
1850
    {
1851
        if (!is_object($value)) {
1852
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1853
                $message ?: 'Provided "%s" is not a valid object.',
1854
                static::stringify($value)
1855
            );
1856
1857
            throw static::createException($value, $message, static::INVALID_OBJECT, $propertyPath);
1858
        }
1859
1860
        return true;
1861
    }
1862
1863
    /**
1864
     * Determines if the value is less than given limit.
1865
     *
1866
     * @param mixed $value
1867
     * @param mixed $limit
1868
     * @param string|null $message
1869
     * @param string|null $propertyPath
1870
     * @return bool
1871
     */
1872
    public static function lessThan($value, $limit, $message = null, $propertyPath = null)
1873
    {
1874
        if ($value >= $limit) {
1875
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1876
                $message ?: 'Provided "%s" is not less than "%s".',
1877
                static::stringify($value),
1878
                static::stringify($limit)
1879
            );
1880
1881
            throw static::createException($value, $message, static::INVALID_LESS, $propertyPath);
1882
        }
1883
1884
        return true;
1885
    }
1886
1887
    /**
1888
     * Determines if the value is less or than given limit.
1889
     *
1890
     * @param mixed $value
1891
     * @param mixed $limit
1892
     * @param string|null $message
1893
     * @param string|null $propertyPath
1894
     * @return bool
1895
     */
1896
    public static function lessOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1897
    {
1898
        if ($value > $limit) {
1899
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1900
                $message ?: 'Provided "%s" is not less or equal than "%s".',
1901
                static::stringify($value),
1902
                static::stringify($limit)
1903
            );
1904
1905
            throw static::createException($value, $message, static::INVALID_LESS_OR_EQUAL, $propertyPath);
1906
        }
1907
1908
        return true;
1909
    }
1910
1911
    /**
1912
     * Determines if the value is greater than given limit.
1913
     *
1914
     * @param mixed $value
1915
     * @param mixed $limit
1916
     * @param string|null $message
1917
     * @param string|null $propertyPath
1918
     * @return bool
1919
     */
1920
    public static function greaterThan($value, $limit, $message = null, $propertyPath = null)
1921
    {
1922
        if ($value <= $limit) {
1923
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1924
                $message ?: 'Provided "%s" is not greater than "%s".',
1925
                static::stringify($value),
1926
                static::stringify($limit)
1927
            );
1928
1929
            throw static::createException($value, $message, static::INVALID_GREATER, $propertyPath);
1930
        }
1931
1932
        return true;
1933
    }
1934
1935
    /**
1936
     * Determines if the value is greater or equal than given limit.
1937
     *
1938
     * @param mixed $value
1939
     * @param mixed $limit
1940
     * @param string|null $message
1941
     * @param string|null $propertyPath
1942
     * @return bool
1943
     */
1944
    public static function greaterOrEqualThan($value, $limit, $message = null, $propertyPath = null)
1945
    {
1946
        if ($value < $limit) {
1947
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1948
                $message ?: 'Provided "%s" is not greater or equal than "%s".',
1949
                static::stringify($value),
1950
                static::stringify($limit)
1951
            );
1952
1953
            throw static::createException($value, $message, static::INVALID_GREATER_OR_EQUAL, $propertyPath);
1954
        }
1955
1956
        return true;
1957
    }
1958
1959
    /**
1960
     * Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
1961
     *
1962
     * @param mixed $value
1963
     * @param mixed $lowerLimit
1964
     * @param mixed $upperLimit
1965
     * @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...
1966
     * @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...
1967
     * @return bool
1968
     */
1969
    public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
1970
    {
1971
        if ($lowerLimit > $value || $value > $upperLimit) {
1972
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1973
                $message ?: 'Provided "%s" is neither greater than or equal to "%s" nor less than or equal to "%s".',
1974
                static::stringify($value),
1975
                static::stringify($lowerLimit),
1976
                static::stringify($upperLimit)
1977
            );
1978
1979
            throw static::createException($value, $message, static::INVALID_BETWEEN, $propertyPath);
1980
        }
1981
1982
        return true;
1983
    }
1984
1985
    /**
1986
     * Assert that a value is greater than a lower limit, and less than an upper limit.
1987
     *
1988
     * @param mixed $value
1989
     * @param mixed $lowerLimit
1990
     * @param mixed $upperLimit
1991
     * @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...
1992
     * @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...
1993
     * @return bool
1994
     */
1995
    public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
1996
    {
1997
        if ($lowerLimit >= $value || $value >= $upperLimit) {
1998
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
1999
                $message ?: 'Provided "%s" is neither greater than "%s" nor less than "%s".',
2000
                static::stringify($value),
2001
                static::stringify($lowerLimit),
2002
                static::stringify($upperLimit)
2003
            );
2004
2005
            throw static::createException($value, $message, static::INVALID_BETWEEN_EXCLUSIVE, $propertyPath);
2006
        }
2007
2008
        return true;
2009
    }
2010
2011
    /**
2012
     * Assert that extension is loaded.
2013
     *
2014
     * @param mixed $value
2015
     * @param string|null $message
2016
     * @param string|null $propertyPath
2017
     * @return bool
2018
     * @throws \Assert\AssertionFailedException
2019
     */
2020
    public static function extensionLoaded($value, $message = null, $propertyPath = null)
2021
    {
2022
        if (! extension_loaded($value)) {
2023
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2024
                $message ?: 'Extension "%s" is required.',
2025
                static::stringify($value)
2026
            );
2027
2028
            throw static::createException($value, $message, static::INVALID_EXTENSION, $propertyPath);
2029
        }
2030
2031
        return true;
2032
    }
2033
2034
    /**
2035
     * Assert comparison of two versions.
2036
     *
2037
     * @param string $version1
2038
     * @param string $operator
2039
     * @param string $version2
2040
     * @param string|null $message
2041
     * @param string|null $propertyPath
2042
     * @return bool
2043
     * @throws \Assert\AssertionFailedException
2044
     */
2045
    public static function version($version1, $operator, $version2, $message = null, $propertyPath = null)
2046
    {
2047
        static::notEmpty($operator, 'versionCompare operator is required and cannot be empty.');
2048
2049
        if (version_compare($version1, $version2, $operator) !== true) {
2050
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2051
                $message ?: 'Version "%s" is not "%s" version "%s".',
2052
                static::stringify($version1),
2053
                static::stringify($operator),
2054
                static::stringify($version2)
2055
            );
2056
2057
            throw static::createException($version1, $message, static::INVALID_VERSION, $propertyPath);
2058
        }
2059
2060
        return true;
2061
    }
2062
2063
    /**
2064
     * Assert on PHP version.
2065
     *
2066
     * @param string $operator
2067
     * @param mixed $version
2068
     * @param string|null $message
2069
     * @param string|null $propertyPath
2070
     * @return bool
2071
     * @throws \Assert\AssertionFailedException
2072
     */
2073
    public static function phpVersion($operator, $version, $message = null, $propertyPath = null)
2074
    {
2075
        static::defined('PHP_VERSION');
2076
2077
        return static::version(PHP_VERSION, $operator, $version, $message, $propertyPath);
2078
    }
2079
2080
    /**
2081
     * Assert that date is valid and corresponds to the given format.
2082
     *
2083
     * @param string      $value
2084
     * @param string      $format supports all of the options date(), except for the following:
2085
     *                            N, w, W, t, L, o, B, a, A, g, h, I, O, P, Z, c, r.
2086
     * @param string|null $message
2087
     * @param string|null $propertyPath
2088
     * @return bool
2089
     *
2090
     * @link http://php.net/manual/function.date.php#refsect1-function.date-parameters
2091
     */
2092
     public static function date($value, $format, $message = null, $propertyPath = null)
2093
     {
2094
         static::string($value, $message, $propertyPath);
2095
         static::string($format, $message, $propertyPath);
2096
2097
         $dateTime = \DateTime::createFromFormat($format, $value);
2098
2099
         if (false === $dateTime || $value !== $dateTime->format($format)) {
2100
             $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2101
                 $message ?: 'Date "%s" is invalid or does not match format "%s".',
2102
                 static::stringify($value),
2103
                 static::stringify($format)
2104
             );
2105
2106
             throw static::createException($value, $message, static::INVALID_DATE, $propertyPath, array('format' => $format));
2107
         }
2108
2109
         return true;
2110
     }
2111
2112
    /**
2113
     * Determines that the provided value is callable.
2114
     *
2115
     * @param mixed $value
2116
     * @param string|null $message
2117
     * @param string|null $propertyPath
2118
     * @return bool
2119
     */
2120
    public static function isCallable($value, $message = null, $propertyPath = null)
2121
    {
2122
        if (!is_callable($value)) {
2123
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2124
                $message ?: 'Provided "%s" is not a callable.',
2125
                static::stringify($value)
2126
            );
2127
2128
            throw static::createException($value, $message, static::INVALID_CALLABLE, $propertyPath);
2129
        }
2130
2131
        return true;
2132
    }
2133
2134
    /**
2135
     * Assert that the provided value is valid according to a callback.
2136
     *
2137
     * If the callback returns `false` the assertion will fail.
2138
     *
2139
     * @param mixed $value
2140
     * @param callable $callback
2141
     * @param string|null $message
2142
     * @param string|null $propertyPath
2143
     * @return bool
2144
     */
2145
    public static function satisfy($value, $callback, $message = null, $propertyPath = null)
2146
    {
2147
        static::isCallable($callback);
2148
2149
        if (call_user_func($callback, $value) === false) {
2150
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2151
                $message ?: 'Provided "%s" is invalid according to custom rule.',
2152
                static::stringify($value)
2153
            );
2154
2155
            throw static::createException($value, $message, static::INVALID_SATISFY, $propertyPath);
2156
        }
2157
2158
        return true;
2159
    }
2160
2161
    /**
2162
     * Assert that value is an IPv4 or IPv6 address
2163
     * (using input_filter/FILTER_VALIDATE_IP).
2164
     *
2165
     * @param string      $value
2166
     * @param null|int    $flag
2167
     * @param string|null $message
2168
     * @param string|null $propertyPath
2169
     * @return bool
2170
     *
2171
     * @link http://php.net/manual/filter.filters.flags.php
2172
     */
2173
    public static function ip($value, $flag = null, $message = null, $propertyPath = null)
2174
    {
2175
        static::string($value, $message, $propertyPath);
2176
        if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
2177
            $message = sprintf(
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2178
                $message ?: 'Value "%s" was expected to be a valid IP address.',
2179
                static::stringify($value)
2180
            );
2181
            throw static::createException($value, $message, static::INVALID_IP, $propertyPath);
2182
        }
2183
2184
        return true;
2185
    }
2186
2187
    /**
2188
     * Assert that value is an IPv4 address
2189
     * (using input_filter/FILTER_VALIDATE_IP).
2190
     *
2191
     * @param string      $value
2192
     * @param null|int    $flag
2193
     * @param string|null $message
2194
     * @param string|null $propertyPath
2195
     * @return bool
2196
     *
2197
     * @link http://php.net/manual/filter.filters.flags.php
2198
     */
2199
    public static function ipv4($value, $flag = null, $message = null, $propertyPath = null)
2200
    {
2201
        static::ip($value, $flag | FILTER_FLAG_IPV4, $message ?: 'Value "%s" was expected to be a valid IPv4 address.', $propertyPath);
2202
2203
        return true;
2204
    }
2205
2206
    /**
2207
     * Assert that value is an IPv6 address
2208
     * (using input_filter/FILTER_VALIDATE_IP).
2209
     *
2210
     * @param string      $value
2211
     * @param null|int    $flag
2212
     * @param string|null $message
2213
     * @param string|null $propertyPath
2214
     * @return bool
2215
     *
2216
     * @link http://php.net/manual/filter.filters.flags.php
2217
     */
2218
    public static function ipv6($value, $flag = null, $message = null, $propertyPath = null)
2219
    {
2220
        static::ip($value, $flag | FILTER_FLAG_IPV6, $message ?: 'Value "%s" was expected to be a valid IPv6 address.', $propertyPath);
2221
2222
        return true;
2223
    }
2224
2225
    /**
2226
     * Make a string version of a value.
2227
     *
2228
     * @param mixed $value
2229
     * @return string
2230
     */
2231
    protected static function stringify($value)
2232
    {
2233
        if (is_bool($value)) {
2234
            return $value ? '<TRUE>' : '<FALSE>';
2235
        }
2236
2237
        if (is_scalar($value)) {
2238
            $val = (string)$value;
2239
2240
            if (strlen($val) > 100) {
2241
                $val = substr($val, 0, 97) . '...';
2242
            }
2243
2244
            return $val;
2245
        }
2246
2247
        if (is_array($value)) {
2248
            return '<ARRAY>';
2249
        }
2250
2251
        if (is_object($value)) {
2252
            return get_class($value);
2253
        }
2254
2255
        if (is_resource($value)) {
2256
            return get_resource_type($value);
2257
        }
2258
2259
        if ($value === null) {
2260
            return '<NULL>';
2261
        }
2262
2263
        return gettype($value);
2264
    }
2265
2266
    /**
2267
     * Assert that a constant is defined.
2268
     *
2269
     * @param mixed $constant
2270
     * @param string|null $message
2271
     * @param string|null $propertyPath
2272
     * @return bool
2273
     * @throws \Assert\AssertionFailedException
2274
     */
2275
    public static function defined($constant, $message = null, $propertyPath = null)
2276
    {
2277
        if (!defined($constant)) {
2278
            $message = sprintf($message ?: 'Value "%s" expected to be a defined constant.', $constant);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
2279
2280
            throw static::createException($constant, $message, static::INVALID_CONSTANT, $propertyPath);
2281
        }
2282
2283
        return true;
2284
    }
2285
}
2286