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