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 ReflectionClass; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Chaining builder for assertions |
21
|
|
|
* |
22
|
|
|
* @author Benjamin Eberlei <[email protected]> |
23
|
|
|
* |
24
|
|
|
* METHODSTART |
25
|
|
|
* @method \Assert\AssertionChain eq($value2, $message = null, $propertyPath = null) |
26
|
|
|
* @method \Assert\AssertionChain same($value2, $message = null, $propertyPath = null) |
27
|
|
|
* @method \Assert\AssertionChain notEq($value2, $message = null, $propertyPath = null) |
28
|
|
|
* @method \Assert\AssertionChain notSame($value2, $message = null, $propertyPath = null) |
29
|
|
|
* @method \Assert\AssertionChain integer($message = null, $propertyPath = null) |
30
|
|
|
* @method \Assert\AssertionChain float($message = null, $propertyPath = null) |
31
|
|
|
* @method \Assert\AssertionChain digit($message = null, $propertyPath = null) |
32
|
|
|
* @method \Assert\AssertionChain integerish($message = null, $propertyPath = null) |
33
|
|
|
* @method \Assert\AssertionChain boolean($message = null, $propertyPath = null) |
34
|
|
|
* @method \Assert\AssertionChain scalar($message = null, $propertyPath = null) |
35
|
|
|
* @method \Assert\AssertionChain notEmpty($message = null, $propertyPath = null) |
36
|
|
|
* @method \Assert\AssertionChain noContent($message = null, $propertyPath = null) |
37
|
|
|
* @method \Assert\AssertionChain notNull($message = null, $propertyPath = null) |
38
|
|
|
* @method \Assert\AssertionChain string($message = null, $propertyPath = null) |
39
|
|
|
* @method \Assert\AssertionChain regex($pattern, $message = null, $propertyPath = null) |
40
|
|
|
* @method \Assert\AssertionChain length($length, $message = null, $propertyPath = null, $encoding = "utf8") |
41
|
|
|
* @method \Assert\AssertionChain minLength($minLength, $message = null, $propertyPath = null, $encoding = "utf8") |
42
|
|
|
* @method \Assert\AssertionChain maxLength($maxLength, $message = null, $propertyPath = null, $encoding = "utf8") |
43
|
|
|
* @method \Assert\AssertionChain betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") |
44
|
|
|
* @method \Assert\AssertionChain startsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") |
45
|
|
|
* @method \Assert\AssertionChain endsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8") |
46
|
|
|
* @method \Assert\AssertionChain contains($needle, $message = null, $propertyPath = null, $encoding = "utf8") |
47
|
|
|
* @method \Assert\AssertionChain choice($choices, $message = null, $propertyPath = null) |
48
|
|
|
* @method \Assert\AssertionChain inArray($choices, $message = null, $propertyPath = null) |
49
|
|
|
* @method \Assert\AssertionChain numeric($message = null, $propertyPath = null) |
50
|
|
|
* @method \Assert\AssertionChain isArray($message = null, $propertyPath = null) |
51
|
|
|
* @method \Assert\AssertionChain isTraversable($message = null, $propertyPath = null) |
52
|
|
|
* @method \Assert\AssertionChain isArrayAccessible($message = null, $propertyPath = null) |
53
|
|
|
* @method \Assert\AssertionChain keyExists($key, $message = null, $propertyPath = null) |
54
|
|
|
* @method \Assert\AssertionChain keyIsset($key, $message = null, $propertyPath = null) |
55
|
|
|
* @method \Assert\AssertionChain notEmptyKey($key, $message = null, $propertyPath = null) |
56
|
|
|
* @method \Assert\AssertionChain notBlank($message = null, $propertyPath = null) |
57
|
|
|
* @method \Assert\AssertionChain isInstanceOf($className, $message = null, $propertyPath = null) |
58
|
|
|
* @method \Assert\AssertionChain notIsInstanceOf($className, $message = null, $propertyPath = null) |
59
|
|
|
* @method \Assert\AssertionChain subclassOf($className, $message = null, $propertyPath = null) |
60
|
|
|
* @method \Assert\AssertionChain range($minValue, $maxValue, $message = null, $propertyPath = null) |
61
|
|
|
* @method \Assert\AssertionChain min($minValue, $message = null, $propertyPath = null) |
62
|
|
|
* @method \Assert\AssertionChain max($maxValue, $message = null, $propertyPath = null) |
63
|
|
|
* @method \Assert\AssertionChain file($message = null, $propertyPath = null) |
64
|
|
|
* @method \Assert\AssertionChain directory($message = null, $propertyPath = null) |
65
|
|
|
* @method \Assert\AssertionChain readable($message = null, $propertyPath = null) |
66
|
|
|
* @method \Assert\AssertionChain writeable($message = null, $propertyPath = null) |
67
|
|
|
* @method \Assert\AssertionChain email($message = null, $propertyPath = null) |
68
|
|
|
* @method \Assert\AssertionChain url($message = null, $propertyPath = null) |
69
|
|
|
* @method \Assert\AssertionChain alnum($message = null, $propertyPath = null) |
70
|
|
|
* @method \Assert\AssertionChain true($message = null, $propertyPath = null) |
71
|
|
|
* @method \Assert\AssertionChain false($message = null, $propertyPath = null) |
72
|
|
|
* @method \Assert\AssertionChain classExists($message = null, $propertyPath = null) |
73
|
|
|
* @method \Assert\AssertionChain implementsInterface($interfaceName, $message = null, $propertyPath = null) |
74
|
|
|
* @method \Assert\AssertionChain isJsonString($message = null, $propertyPath = null) |
75
|
|
|
* @method \Assert\AssertionChain uuid($message = null, $propertyPath = null) |
76
|
|
|
* @method \Assert\AssertionChain count($count, $message = null, $propertyPath = null) |
77
|
|
|
* @method \Assert\AssertionChain choicesNotEmpty($choices, $message = null, $propertyPath = null) |
78
|
|
|
* @method \Assert\AssertionChain methodExists($object, $message = null, $propertyPath = null) |
79
|
|
|
* @method \Assert\AssertionChain isObject($message = null, $propertyPath = null) |
80
|
|
|
* @method \Assert\AssertionChain lessThan($limit, $message = null, $propertyPath = null) |
81
|
|
|
* @method \Assert\AssertionChain lessOrEqualThan($limit, $message = null, $propertyPath = null) |
82
|
|
|
* @method \Assert\AssertionChain greaterThan($limit, $message = null, $propertyPath = null) |
83
|
|
|
* @method \Assert\AssertionChain greaterOrEqualThan($limit, $message = null, $propertyPath = null) |
84
|
|
|
* @method \Assert\AssertionChain date($format, $message = null, $propertyPath = null) |
85
|
|
|
* METHODEND |
86
|
|
|
*/ |
87
|
|
|
class AssertionChain |
88
|
|
|
{ |
89
|
|
|
private $value; |
90
|
|
|
private $defaultMessage; |
91
|
|
|
private $defaultPropertyPath; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return each assertion as always valid. |
95
|
|
|
* |
96
|
|
|
* @var bool |
97
|
|
|
*/ |
98
|
|
|
private $alwaysValid = false; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Perform assertion on every element of array or traversable. |
102
|
|
|
* |
103
|
|
|
* @var bool |
104
|
|
|
*/ |
105
|
|
|
private $all = false; |
106
|
|
|
|
107
|
|
|
public function __construct($value, $defaultMessage = null, $defaultPropertyPath = null) |
108
|
|
|
{ |
109
|
|
|
$this->value = $value; |
110
|
|
|
$this->defaultMessage = $defaultMessage; |
111
|
|
|
$this->defaultPropertyPath = $defaultPropertyPath; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Call assertion on the current value in the chain. |
116
|
|
|
* |
117
|
|
|
* @param string $methodName |
118
|
|
|
* @param array $args |
119
|
|
|
* |
120
|
|
|
* @return \Assert\AssertionChain |
121
|
|
|
*/ |
122
|
|
|
public function __call($methodName, $args) |
123
|
|
|
{ |
124
|
|
|
if ($this->alwaysValid === true) { |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!method_exists('Assert\Assertion', $methodName)) { |
129
|
|
|
throw new \RuntimeException("Assertion '" . $methodName . "' does not exist."); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$reflClass = new ReflectionClass('Assert\Assertion'); |
133
|
|
|
$method = $reflClass->getMethod($methodName); |
134
|
|
|
|
135
|
|
|
array_unshift($args, $this->value); |
136
|
|
|
$params = $method->getParameters(); |
137
|
|
|
|
138
|
|
|
foreach ($params as $idx => $param) { |
139
|
|
|
if (isset($args[$idx])) { |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($param->getName() == 'message') { |
144
|
|
|
$args[$idx] = $this->defaultMessage; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($param->getName() == 'propertyPath') { |
148
|
|
|
$args[$idx] = $this->defaultPropertyPath; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($this->all) { |
153
|
|
|
$methodName = 'all' . $methodName; |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
call_user_func_array(array('Assert\Assertion', $methodName), $args); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Switch chain into validation mode for an array of values. |
163
|
|
|
* |
164
|
|
|
* @return \Assert\AssertionChain |
165
|
|
|
*/ |
166
|
|
|
public function all() |
167
|
|
|
{ |
168
|
|
|
$this->all = true; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Switch chain into mode allowing nulls, ignoring further assertions. |
175
|
|
|
* |
176
|
|
|
* @return \Assert\AssertionChain |
177
|
|
|
*/ |
178
|
|
|
public function nullOr() |
179
|
|
|
{ |
180
|
|
|
if ($this->value === null) { |
181
|
|
|
$this->alwaysValid = true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|