-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathAssertionChain.php
More file actions
184 lines (164 loc) · 7.88 KB
/
AssertionChain.php
File metadata and controls
184 lines (164 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Assert
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
*
* to kontakt@beberlei.de so I can send you a copy immediately.
*/
namespace Assert;
use ReflectionClass;
/**
* Chaining builder for assertions
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*
* METHODSTART
* @method \Assert\AssertionChain eq($value2, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain same($value2, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain notEq($value2, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain notSame($value2, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain integer($message = null, $propertyPath = null)
* @method \Assert\AssertionChain float($message = null, $propertyPath = null)
* @method \Assert\AssertionChain digit($message = null, $propertyPath = null)
* @method \Assert\AssertionChain date($message = null, $propertyPath = null)
* @method \Assert\AssertionChain integerish($message = null, $propertyPath = null)
* @method \Assert\AssertionChain boolean($message = null, $propertyPath = null)
* @method \Assert\AssertionChain scalar($message = null, $propertyPath = null)
* @method \Assert\AssertionChain notEmpty($message = null, $propertyPath = null)
* @method \Assert\AssertionChain noContent($message = null, $propertyPath = null)
* @method \Assert\AssertionChain notNull($message = null, $propertyPath = null)
* @method \Assert\AssertionChain string($message = null, $propertyPath = null)
* @method \Assert\AssertionChain regex($pattern, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain length($length, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain minLength($minLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain maxLength($maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain startsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain endsWith($needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain contains($needle, $message = null, $propertyPath = null, $encoding = "utf8")
* @method \Assert\AssertionChain choice($choices, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain inArray($choices, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain numeric($message = null, $propertyPath = null)
* @method \Assert\AssertionChain isArray($message = null, $propertyPath = null)
* @method \Assert\AssertionChain keyExists($key, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain keysExist($keys, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain propertyExists($key, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain propertiesExist($keys, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain notEmptyKey($key, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain notBlank($message = null, $propertyPath = null)
* @method \Assert\AssertionChain isCallable($message = null, $propertyPath = null)
* @method \Assert\AssertionChain isInstanceOf($className, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain notIsInstanceOf($className, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain subclassOf($className, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain range($minValue, $maxValue, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain min($minValue, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain max($maxValue, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain file($message = null, $propertyPath = null)
* @method \Assert\AssertionChain directory($message = null, $propertyPath = null)
* @method \Assert\AssertionChain readable($message = null, $propertyPath = null)
* @method \Assert\AssertionChain writeable($message = null, $propertyPath = null)
* @method \Assert\AssertionChain email($message = null, $propertyPath = null)
* @method \Assert\AssertionChain url($message = null, $propertyPath = null)
* @method \Assert\AssertionChain alnum($message = null, $propertyPath = null)
* @method \Assert\AssertionChain true($message = null, $propertyPath = null)
* @method \Assert\AssertionChain false($message = null, $propertyPath = null)
* @method \Assert\AssertionChain classExists($message = null, $propertyPath = null)
* @method \Assert\AssertionChain implementsInterface($interfaceName, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain isJsonString($message = null, $propertyPath = null)
* @method \Assert\AssertionChain uuid($message = null, $propertyPath = null)
* @method \Assert\AssertionChain count($count, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain choicesNotEmpty($choices, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain methodExists($object, $message = null, $propertyPath = null)
* @method \Assert\AssertionChain isObject($message = null, $propertyPath = null)
* @method \Assert\AssertionChain utf8($message = null, $propertyPath = null)
* METHODEND
*/
class AssertionChain
{
private $value;
private $defaultMessage;
private $defaultPropertyPath;
/**
* Return each assertion as always valid.
*
* @var bool
*/
private $alwaysValid = false;
/**
* Perform assertion on every element of array or traversable.
*
* @var bool
*/
private $all = false;
public function __construct($value, $defaultMessage = null, $defaultPropertyPath = null)
{
$this->value = $value;
$this->defaultMessage = $defaultMessage;
$this->defaultPropertyPath = $defaultPropertyPath;
}
/**
* Call assertion on the current value in the chain.
*
* @param string $method
* @param array $args
*
* @return \Assert\AssertionChain
*/
public function __call($methodName, $args)
{
if ($this->alwaysValid === true) {
return $this;
}
if (!method_exists('Assert\Assertion', $methodName)) {
throw new \RuntimeException("Assertion '" . $methodName . "' does not exist.");
}
$reflClass = new ReflectionClass('Assert\Assertion');
$method = $reflClass->getMethod($methodName);
array_unshift($args, $this->value);
$params = $method->getParameters();
foreach ($params as $idx => $param) {
if (isset($args[$idx])) {
continue;
}
if ($param->getName() == 'message') {
$args[$idx] = $this->defaultMessage;
}
if ($param->getName() == 'propertyPath') {
$args[$idx] = $this->defaultPropertyPath;
}
}
if ($this->all) {
$methodName = 'all' . $methodName;
}
call_user_func_array(array('Assert\Assertion', $methodName), $args);
return $this;
}
/**
* Switch chain into validation mode for an array of values.
*
* @return \Assert\AssertionChain
*/
public function all()
{
$this->all = true;
return $this;
}
/**
* Switch chain into mode allowing nulls, ignoring further assertions.
*
* @return \Assert\AssertionChain
*/
public function nullOr()
{
if ($this->value === null) {
$this->alwaysValid = true;
}
return $this;
}
}