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