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