1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Routlt\Description\ResultDescriptor |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/routlt |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Routlt\Description; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Lang\Reflection\AnnotationInterface; |
24
|
|
|
use AppserverIo\Configuration\Interfaces\NodeInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Descriptor implementation for a action result. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link http://github.com/appserver-io/routlt |
33
|
|
|
* @link http://www.appserver.io |
34
|
|
|
*/ |
35
|
|
|
class ResultDescriptor implements ResultDescriptorInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The arction result name. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $name; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The action result type. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $type; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The action result value. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $result; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The HTTP response code that has to be send. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $code = 200; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the action result name. |
68
|
|
|
* |
69
|
|
|
* @param string $name The action result name |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function setName($name) |
74
|
|
|
{ |
75
|
|
|
$this->name = $name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the action result name. |
80
|
|
|
* |
81
|
|
|
* @return string The action result name |
82
|
|
|
*/ |
83
|
|
|
public function getName() |
84
|
|
|
{ |
85
|
|
|
return $this->name; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets the action result type. |
90
|
|
|
* |
91
|
|
|
* @param string $type The action result type |
92
|
|
|
* |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function setType($type) |
96
|
|
|
{ |
97
|
|
|
$this->type = $type; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the action result type. |
102
|
|
|
* |
103
|
|
|
* @return string The action result type |
104
|
|
|
*/ |
105
|
|
|
public function getType() |
106
|
|
|
{ |
107
|
|
|
return $this->type; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the action result value. |
112
|
|
|
* |
113
|
|
|
* @param string $result The action result value |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function setResult($result) |
118
|
|
|
{ |
119
|
|
|
$this->result = $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the action result value. |
124
|
|
|
* |
125
|
|
|
* @return string The action result value |
126
|
|
|
*/ |
127
|
|
|
public function getResult() |
128
|
|
|
{ |
129
|
|
|
return $this->result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sets the HTTP response code that has to be send. |
134
|
|
|
* |
135
|
|
|
* @param integer $code The HTTP response code |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function setCode($code) |
140
|
|
|
{ |
141
|
|
|
$this->code = $code; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the HTTP response code that has to be send. |
146
|
|
|
* |
147
|
|
|
* @return integer The HTTP response code |
148
|
|
|
*/ |
149
|
|
|
public function getCode() |
150
|
|
|
{ |
151
|
|
|
return $this->code; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns a new descriptor instance. |
156
|
|
|
* |
157
|
|
|
* @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The descriptor instance |
158
|
|
|
*/ |
159
|
|
|
public static function newDescriptorInstance() |
160
|
|
|
{ |
161
|
|
|
return new ResultDescriptor(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Initializes the result configuration instance from the passed reflection annotation instance. |
166
|
|
|
* |
167
|
|
|
* @param \AppserverIo\Lang\Reflection\AnnotationInterface $reflectionAnnotation The reflection annotation with the result configuration |
168
|
|
|
* |
169
|
|
|
* @return \AppserverIo\Routlt\Description\ResultDescriptorInterface The initialized descriptor |
170
|
|
|
*/ |
171
|
|
|
public function fromReflectionAnnotation(AnnotationInterface $reflectionAnnotation) |
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
// initialize the annotation instance |
175
|
|
|
$annotationInstance = $reflectionAnnotation->newInstance( |
176
|
|
|
$reflectionAnnotation->getAnnotationName(), |
177
|
|
|
$reflectionAnnotation->getValues() |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
// initialize the descriptor properties from the annotation values |
181
|
|
|
$this->setName($annotationInstance->getName()); |
182
|
|
|
$this->setType($annotationInstance->getType()); |
183
|
|
|
$this->setResult($annotationInstance->getResult()); |
184
|
|
|
|
185
|
|
|
// set the HTTP response code if given |
186
|
|
|
if ($code = $annotationInstance->getCode()) { |
187
|
|
|
$this->setCode($code); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// return the instance |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Initializes a action configuration instance from the passed deployment descriptor node. |
196
|
|
|
* |
197
|
|
|
* @param \SimpleXmlElement $node The deployment node with the action configuration |
198
|
|
|
* |
199
|
|
|
* @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor |
200
|
|
|
*/ |
201
|
|
|
public function fromDeploymentDescriptor(\SimpleXmlElement $node) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Initializes a action configuration instance from the passed configuration node. |
207
|
|
|
* |
208
|
|
|
* @param \AppserverIo\Configuration\Interfaces\NodeInterface $node The configuration node with the action configuration |
209
|
|
|
* |
210
|
|
|
* @return \AppserverIo\Routlt\Description\ActionDescriptorInterface The initialized descriptor |
211
|
|
|
*/ |
212
|
|
|
public function fromConfiguration(NodeInterface $node) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Merges the passed configuration into this one. Configuration values |
218
|
|
|
* of the passed configuration will overwrite the this one. |
219
|
|
|
* |
220
|
|
|
* @param \AppserverIo\Routlt\Description\ResultDescriptorInterface $resultDescriptor The configuration to merge |
221
|
|
|
* |
222
|
|
|
* @return void |
223
|
|
|
* @throws \AppserverIo\Routlt\Description\DescriptorException Is thrown if the passed descriptor has a different method name |
224
|
|
|
*/ |
225
|
|
|
public function merge(ResultDescriptorInterface $resultDescriptor) |
226
|
|
|
{ |
227
|
|
|
|
228
|
|
|
// check if the classes are equal |
229
|
|
|
if ($this->getName() !== $resultDescriptor->getName()) { |
230
|
|
|
throw new DescriptorException( |
231
|
|
|
sprintf('You try to merge a result configuration for % with %s', $resultDescriptor->getName(), $this->getName()) |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// merge the type |
236
|
|
|
if ($type = $resultDescriptor->getType()) { |
237
|
|
|
$this->setType($type); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// merge the result |
241
|
|
|
if ($result = $resultDescriptor->getResult()) { |
242
|
|
|
$this->setResult($result); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// merge the code |
246
|
|
|
if ($code = $resultDescriptor->getCode()) { |
247
|
|
|
$this->setCode($code); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.