1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Assert |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this package in the file LICENSE.txt. |
9
|
|
|
* If you did not receive a copy of the license and are unable to |
10
|
|
|
* obtain it through the world-wide-web, please send an email |
11
|
|
|
* |
12
|
|
|
* to [email protected] so I can send you a copy immediately. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Assert; |
16
|
|
|
|
17
|
|
|
use LogicException; |
18
|
|
|
use ReflectionClass; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Chaining builder for assertions |
22
|
|
|
* |
23
|
|
|
* @author Benjamin Eberlei <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @method AssertionChain alnum($message = null, $propertyPath = null) Assert that value is alphanumeric. |
26
|
|
|
* @method AssertionChain between($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit. |
27
|
|
|
* @method AssertionChain betweenExclusive($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit. |
28
|
|
|
* @method AssertionChain betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths. |
29
|
|
|
* @method AssertionChain boolean($message = null, $propertyPath = null) Assert that value is php boolean. |
30
|
|
|
* @method AssertionChain choice($choices, $message = null, $propertyPath = null) Assert that value is in array of choices. |
31
|
|
|
* @method AssertionChain choicesNotEmpty($choices, $message = null, $propertyPath = null) Determines if the values array has every choice as key and that this choice has content. |
32
|
|
|
* @method AssertionChain classExists($message = null, $propertyPath = null) Assert that the class exists. |
33
|
|
|
* @method AssertionChain contains($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string contains a sequence of chars. |
34
|
|
|
* @method AssertionChain count($count, $message = null, $propertyPath = null) Assert that the count of countable is equal to count. |
35
|
|
|
* @method AssertionChain date($format, $message = null, $propertyPath = null) Assert that date is valid and corresponds to the given format. |
36
|
|
|
* @method AssertionChain digit($message = null, $propertyPath = null) Validates if an integer or integerish is a digit. |
37
|
|
|
* @method AssertionChain directory($message = null, $propertyPath = null) Assert that a directory exists. |
38
|
|
|
* @method AssertionChain e164($message = null, $propertyPath = null) Assert that the given string is a valid E164 Phone Number. |
39
|
|
|
* @method AssertionChain email($message = null, $propertyPath = null) Assert that value is an email adress (using input_filter/FILTER_VALIDATE_EMAIL). |
40
|
|
|
* @method AssertionChain endsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string ends with a sequence of chars. |
41
|
|
|
* @method AssertionChain eq($value2, $message = null, $propertyPath = null) Assert that two values are equal (using == ). |
42
|
|
|
* @method AssertionChain false($message = null, $propertyPath = null) Assert that the value is boolean False. |
43
|
|
|
* @method AssertionChain file($message = null, $propertyPath = null) Assert that a file exists. |
44
|
|
|
* @method AssertionChain float($message = null, $propertyPath = null) Assert that value is a php float. |
45
|
|
|
* @method AssertionChain greaterOrEqualThan($limit, $message = null, $propertyPath = null) Determines if the value is greater or equal than given limit. |
46
|
|
|
* @method AssertionChain greaterThan($limit, $message = null, $propertyPath = null) Determines if the value is greater than given limit. |
47
|
|
|
* @method AssertionChain implementsInterface($interfaceName, $message = null, $propertyPath = null) Assert that the class implements the interface. |
48
|
|
|
* @method AssertionChain inArray($choices, $message = null, $propertyPath = null) Alias of {@see choice()}. |
49
|
|
|
* @method AssertionChain integer($message = null, $propertyPath = null) Assert that value is a php integer. |
50
|
|
|
* @method AssertionChain integerish($message = null, $propertyPath = null) Assert that value is a php integer'ish. |
51
|
|
|
* @method AssertionChain interfaceExists($message = null, $propertyPath = null) Assert that the interface exists. |
52
|
|
|
* @method AssertionChain ip($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 or IPv6 address. |
53
|
|
|
* @method AssertionChain ipv4($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv4 address. |
54
|
|
|
* @method AssertionChain ipv6($flag = null, $message = null, $propertyPath = null) Assert that value is an IPv6 address. |
55
|
|
|
* @method AssertionChain isArray($message = null, $propertyPath = null) Assert that value is an array. |
56
|
|
|
* @method AssertionChain isArrayAccessible($message = null, $propertyPath = null) Assert that value is an array or an array-accessible object. |
57
|
|
|
* @method AssertionChain isCallable($message = null, $propertyPath = null) Determines that the provided value is callable. |
58
|
|
|
* @method AssertionChain isInstanceOf($className, $message = null, $propertyPath = null) Assert that value is instance of given class-name. |
59
|
|
|
* @method AssertionChain isJsonString($message = null, $propertyPath = null) Assert that the given string is a valid json string. |
60
|
|
|
* @method AssertionChain isObject($message = null, $propertyPath = null) Determines that the provided value is an object. |
61
|
|
|
* @method AssertionChain isTraversable($message = null, $propertyPath = null) Assert that value is an array or a traversable object. |
62
|
|
|
* @method AssertionChain keyExists($key, $message = null, $propertyPath = null) Assert that key exists in an array. |
63
|
|
|
* @method AssertionChain keyIsset($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object using isset(). |
64
|
|
|
* @method AssertionChain keyNotExists($key, $message = null, $propertyPath = null) Assert that key does not exist in an array. |
65
|
|
|
* @method AssertionChain length($length, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string has a given length. |
66
|
|
|
* @method AssertionChain lessOrEqualThan($limit, $message = null, $propertyPath = null) Determines if the value is less or than given limit. |
67
|
|
|
* @method AssertionChain lessThan($limit, $message = null, $propertyPath = null) Determines if the value is less than given limit. |
68
|
|
|
* @method AssertionChain max($maxValue, $message = null, $propertyPath = null) Assert that a number is smaller as a given limit. |
69
|
|
|
* @method AssertionChain maxLength($maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string value is not longer than $maxLength chars. |
70
|
|
|
* @method AssertionChain methodExists($object, $message = null, $propertyPath = null) Determines that the named method is defined in the provided object. |
71
|
|
|
* @method AssertionChain min($minValue, $message = null, $propertyPath = null) Assert that a value is at least as big as a given limit. |
72
|
|
|
* @method AssertionChain minLength($minLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that a string is at least $minLength chars long. |
73
|
|
|
* @method AssertionChain noContent($message = null, $propertyPath = null) Assert that value is empty. |
74
|
|
|
* @method AssertionChain notBlank($message = null, $propertyPath = null) Assert that value is not blank. |
75
|
|
|
* @method AssertionChain notEmpty($message = null, $propertyPath = null) Assert that value is not empty. |
76
|
|
|
* @method AssertionChain notEmptyKey($key, $message = null, $propertyPath = null) Assert that key exists in an array/array-accessible object and it's value is not empty. |
77
|
|
|
* @method AssertionChain notEq($value2, $message = null, $propertyPath = null) Assert that two values are not equal (using == ). |
78
|
|
|
* @method AssertionChain notInArray($choices, $message = null, $propertyPath = null) Assert that value is not in array of choices. |
79
|
|
|
* @method AssertionChain notIsInstanceOf($className, $message = null, $propertyPath = null) Assert that value is not instance of given class-name. |
80
|
|
|
* @method AssertionChain notNull($message = null, $propertyPath = null) Assert that value is not null. |
81
|
|
|
* @method AssertionChain notSame($value2, $message = null, $propertyPath = null) Assert that two values are not the same (using === ). |
82
|
|
|
* @method AssertionChain null($message = null, $propertyPath = null) Assert that value is null. |
83
|
|
|
* @method AssertionChain numeric($message = null, $propertyPath = null) Assert that value is numeric. |
84
|
|
|
* @method AssertionChain range($minValue, $maxValue, $message = null, $propertyPath = null) Assert that value is in range of numbers. |
85
|
|
|
* @method AssertionChain readable($message = null, $propertyPath = null) Assert that the value is something readable. |
86
|
|
|
* @method AssertionChain regex($pattern, $message = null, $propertyPath = null) Assert that value matches a regex. |
87
|
|
|
* @method AssertionChain same($value2, $message = null, $propertyPath = null) Assert that two values are the same (using ===). |
88
|
|
|
* @method AssertionChain satisfy($callback, $message = null, $propertyPath = null) Assert that the provided value is valid according to a callback. |
89
|
|
|
* @method AssertionChain scalar($message = null, $propertyPath = null) Assert that value is a PHP scalar. |
90
|
|
|
* @method AssertionChain startsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string starts with a sequence of chars. |
91
|
|
|
* @method AssertionChain string($message = null, $propertyPath = null) Assert that value is a string. |
92
|
|
|
* @method AssertionChain subclassOf($className, $message = null, $propertyPath = null) Assert that value is subclass of given class-name. |
93
|
|
|
* @method AssertionChain true($message = null, $propertyPath = null) Assert that the value is boolean True. |
94
|
|
|
* @method AssertionChain url($message = null, $propertyPath = null) Assert that value is an URL. |
95
|
|
|
* @method AssertionChain uuid($message = null, $propertyPath = null) Assert that the given string is a valid UUID. |
96
|
|
|
* @method AssertionChain writeable($message = null, $propertyPath = null) Assert that the value is something writeable. |
97
|
|
|
*/ |
98
|
|
|
class AssertionChain |
99
|
|
|
{ |
100
|
|
|
private $value; |
101
|
|
|
private $defaultMessage; |
102
|
|
|
private $defaultPropertyPath; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return each assertion as always valid. |
106
|
|
|
* |
107
|
|
|
* @var bool |
108
|
|
|
*/ |
109
|
|
|
private $alwaysValid = false; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Perform assertion on every element of array or traversable. |
113
|
|
|
* |
114
|
|
|
* @var bool |
115
|
|
|
*/ |
116
|
|
|
private $all = false; |
117
|
|
|
|
118
|
|
|
/** @var string|Assertion Class to use for assertion calls */ |
119
|
|
|
private $assertionClassName = 'Assert\Assertion'; |
120
|
|
|
|
121
|
|
|
public function __construct($value, $defaultMessage = null, $defaultPropertyPath = null) |
122
|
|
|
{ |
123
|
|
|
$this->value = $value; |
124
|
|
|
$this->defaultMessage = $defaultMessage; |
125
|
|
|
$this->defaultPropertyPath = $defaultPropertyPath; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Call assertion on the current value in the chain. |
130
|
|
|
* |
131
|
|
|
* @param string $methodName |
132
|
|
|
* @param array $args |
133
|
|
|
* |
134
|
|
|
* @return \Assert\AssertionChain |
135
|
|
|
*/ |
136
|
|
|
public function __call($methodName, $args) |
137
|
|
|
{ |
138
|
|
|
if ($this->alwaysValid === true) { |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (!method_exists($this->assertionClassName, $methodName)) { |
143
|
|
|
throw new \RuntimeException("Assertion '" . $methodName . "' does not exist."); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$reflClass = new ReflectionClass($this->assertionClassName); |
147
|
|
|
$method = $reflClass->getMethod($methodName); |
148
|
|
|
|
149
|
|
|
array_unshift($args, $this->value); |
150
|
|
|
$params = $method->getParameters(); |
151
|
|
|
|
152
|
|
|
foreach ($params as $idx => $param) { |
153
|
|
|
if (isset($args[$idx])) { |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($param->getName() == 'message') { |
158
|
|
|
$args[$idx] = $this->defaultMessage; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($param->getName() == 'propertyPath') { |
162
|
|
|
$args[$idx] = $this->defaultPropertyPath; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($this->all) { |
167
|
|
|
$methodName = 'all' . $methodName; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
call_user_func_array(array($this->assertionClassName, $methodName), $args); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Switch chain into validation mode for an array of values. |
177
|
|
|
* |
178
|
|
|
* @return \Assert\AssertionChain |
179
|
|
|
*/ |
180
|
|
|
public function all() |
181
|
|
|
{ |
182
|
|
|
$this->all = true; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Switch chain into mode allowing nulls, ignoring further assertions. |
189
|
|
|
* |
190
|
|
|
* @return \Assert\AssertionChain |
191
|
|
|
*/ |
192
|
|
|
public function nullOr() |
193
|
|
|
{ |
194
|
|
|
if ($this->value === null) { |
195
|
|
|
$this->alwaysValid = true; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $className |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
|
|
public function setAssertionClassName($className) |
206
|
|
|
{ |
207
|
|
|
if (!is_string($className)) { |
208
|
|
|
throw new LogicException('Exception class name must be passed as a string'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if ($className !== 'Assert\Assertion' && !is_subclass_of($className, 'Assert\Assertion')) { |
212
|
|
|
throw new LogicException($className . ' is not (a subclass of) Assert\Assertion'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->assertionClassName = $className; |
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|