-
Notifications
You must be signed in to change notification settings - Fork 0
/
Method.php
executable file
·265 lines (236 loc) · 6.43 KB
/
Method.php
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace NickLewis\Mapping\Services;
use InvalidArgumentException;
use NickLewis\Mapping\Models\ObjectInterface;
use NickLewis\Mapping\Models\String;
use NickLewis\Mapping\Models\StringInterface;
use NickLewis\Mapping\Services\Lexicon\Method as LexiconMethod;
class Method {
const RETURN_INT = 'int';
const RETURN_BOOLEAN = 'boolean';
const RETURN_DOUBLE = 'double';
const RETURN_STRING = 'string';
const RETURN_DATE = 'date';
const RETURN_DATETIME = 'datetime';
const RETURN_MIXED = 'mixed';
const RETURN_MAP = 'map';
/** @var string */
private $name;
/** @var string */
private $returnType;
/** @var callable */
private $handler;
/** @var string */
private $description;
/** @var bool */
private $returnTypeMappable = false;
/** @var ParameterInterface[] */
private $parameters = [];
/** @type bool */
private $hasParameterGrouping = false;
/**
* Getter
* @return boolean
*/
private function isHasParameterGrouping() {
return $this->hasParameterGrouping;
}
/**
* Setter
* @param boolean $hasParameterGrouping
* @return Method
*/
private function setHasParameterGrouping($hasParameterGrouping) {
$this->hasParameterGrouping = $hasParameterGrouping;
return $this;
}
/**
* Getter
* @return ParameterInterface[]
*/
public function getParameters() {
return $this->parameters;
}
/**
* Setter
* @param ParameterInterface[] $parameters
*/
private function setParameters(array $parameters) {
$this->parameters = $parameters;
}
/**
* addParameter
*
* @param ParameterInterface $parameter
*
* @return void
*/
public function addParameter(ParameterInterface $parameter) {
if($this->isHasParameterGrouping()) {
throw new InvalidArgumentException('You cannot add a parameter after you have added a parameterGrouping');
}
if($parameter instanceof ParameterGrouping) {
$this->setHasParameterGrouping(true);
}
$parameters = $this->getParameters();
$parameters[] = $parameter;
$this->setParameters($parameters);
}
/**
* Getter
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* Setter
* @param string $description
* @return $this
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Getter
* @return callable
*/
private function getHandler() {
return $this->handler;
}
/**
* handle
* @param LexiconMethod[] $parameters
* @return mixed
*/
public function handle(array $parameters=[]) {
$handler = $this->getHandler();
$parameters = $this->validateParameters($parameters);
if(empty($parameters)) {
return $handler();
} else {
return call_user_func_array($handler, $parameters);
}
}
/**
* validParameters
* @param LexiconMethod[][] $parameters
* @return array
* @throws CatchableException
*/
private function validateParameters(array $parameters=[]) {
$currentParamKey = 0;
foreach($this->getParameters() as $mappingParameter) {
if($mappingParameter instanceof ParameterGrouping) {
while($currentParamKey<sizeOf($parameters)) {
foreach ($mappingParameter->getParameters() as $subMappingParameter) {
if (array_key_exists($currentParamKey, $parameters)) {
$parameters[$currentParamKey] = $subMappingParameter->validate($parameters[$currentParamKey]);
} elseif ($subMappingParameter->isRequired()) {
throw new CatchableException('Missing Required Parameter (' . $subMappingParameter->getDescription() . ')' . "\n" . $this->__toString());
}
$currentParamKey++;
}
}
} elseif($mappingParameter instanceof Parameter) {
if (array_key_exists($currentParamKey, $parameters)) {
$parameters[$currentParamKey] = $mappingParameter->validate($parameters[$currentParamKey]);
} elseif ($mappingParameter->isRequired()) {
throw new CatchableException('Missing Required Parameter (' . $mappingParameter->getDescription() . ')' . "\n" . $this->__toString());
}
}
$currentParamKey++;
}
if($currentParamKey<sizeOf($parameters)) {
throw new CatchableException('Too many parameters passed in'."\n".$this->__toString());
}
return $parameters;
}
/**
* __toString
* @return string
*/
public function __toString() {
$returnVar = $this->getName().': '.$this->getDescription()."\nParameters: ";
if(empty($this->getParameters())) {
$returnVar .= 'No Parameters';
} else {
foreach($this->getParameters() as $parameter) {
$returnVar .= "\n".$parameter->__toString();
}
}
return $returnVar;
}
/**
* Setter
* @param callable $handler
* @return $this
*/
public function setHandler(callable $handler) {
$this->handler = $handler;
return $this;
}
/**
* Getter
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Setter
* @param string $name
* @return $this
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Getter
* @return string
*/
public function getReturnType() {
return $this->returnType;
}
/**
* getter
* @return boolean
*/
public function isReturnTypeMappable() {
return $this->returnTypeMappable;
}
/**
* Setter
* @param boolean $returnTypeMappable
*/
private function setReturnTypeMappable($returnTypeMappable) {
$this->returnTypeMappable = $returnTypeMappable;
}
/**
* Setter
* @param string $returnType
* @throws \Exception Invalid Return type
* @return $this
*/
public function setReturnType($returnType) {
if(substr($returnType, -2)=='[]') {
$compareReturnType = substr($returnType, 0, -2);
} else {
$compareReturnType = $returnType;
}
if(!in_array($compareReturnType, array(self::RETURN_BOOLEAN, self::RETURN_DOUBLE, self::RETURN_INT, self::RETURN_STRING, self::RETURN_DATE, self::RETURN_DATETIME, self::RETURN_MIXED, self::RETURN_MAP))) {
$class = $compareReturnType;
if(!class_exists($class)) {
throw new \Exception('Invalid Return Type: '.$returnType);
}
if(!(in_array(ObjectInterface::class, class_implements($class)))) {
throw new \Exception('Invalid Return Type: '.$returnType.', does not implement Object Interface: '.$class);
}
$this->setReturnTypeMappable(true);
}
$this->returnType = $returnType;
return $this;
}
}