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