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