|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Action; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Result |
|
18
|
|
|
* |
|
19
|
|
|
* @package CakeDC\Api\Service\Action |
|
20
|
|
|
*/ |
|
21
|
|
|
class Result |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Response code |
|
26
|
|
|
* |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_code = 200; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Response data |
|
33
|
|
|
* |
|
34
|
|
|
* @var array|mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_data = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Response payload |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $_payload = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Exception structure |
|
47
|
|
|
* |
|
48
|
|
|
* @var Exception |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $_exception = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Result constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $data data to be delivered for the api |
|
56
|
|
|
* @param int $code code of the api request |
|
57
|
|
|
*/ |
|
58
|
60 |
|
public function __construct($data = null, $code = null) |
|
59
|
|
|
{ |
|
60
|
60 |
|
if ($data !== null) { |
|
61
|
|
|
$this->setData($data); |
|
62
|
|
|
} |
|
63
|
60 |
|
if ($code !== null) { |
|
64
|
|
|
$this->setCode($code); |
|
65
|
|
|
} |
|
66
|
60 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets a result data. |
|
70
|
|
|
* |
|
71
|
|
|
* @return array|mixed |
|
72
|
|
|
*/ |
|
73
|
56 |
|
public function getData() |
|
74
|
|
|
{ |
|
75
|
56 |
|
return $this->_data; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets a result data. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array|mixed $value data to be delivered for the api |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
56 |
|
public function setData($value) |
|
85
|
|
|
{ |
|
86
|
56 |
|
$this->_data = $value; |
|
87
|
|
|
|
|
88
|
56 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get and set result data. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array|mixed $value data to be delivered for the api |
|
95
|
|
|
* @deprecated 3.6.0 Use setData()/getData() instead. |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function data($value = null) |
|
99
|
|
|
{ |
|
100
|
|
|
deprecationWarning( |
|
101
|
|
|
'Result::data() is deprecated. ' . |
|
102
|
|
|
'Use Result::setData()/getData() instead.' |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
if ($value !== null) { |
|
106
|
|
|
return $this->setData($value); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->getData(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Gets a result code. |
|
114
|
|
|
* |
|
115
|
|
|
* @return int |
|
116
|
|
|
*/ |
|
117
|
60 |
|
public function getCode() |
|
118
|
|
|
{ |
|
119
|
60 |
|
return $this->_code; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Sets a result code. |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $value code to be delivered for the api |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
59 |
|
public function setCode($value) |
|
129
|
|
|
{ |
|
130
|
59 |
|
$this->_code = $value; |
|
131
|
|
|
|
|
132
|
59 |
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Code api method. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int $value code of the api request |
|
139
|
|
|
* @deprecated 3.6.0 Use setCode()/getCode() instead. |
|
140
|
|
|
* @return int |
|
141
|
|
|
*/ |
|
142
|
|
|
public function code($value = null) |
|
143
|
|
|
{ |
|
144
|
|
|
deprecationWarning( |
|
145
|
|
|
'Result::code() is deprecated. ' . |
|
146
|
|
|
'Use Result::setCode()/getCode() instead.' |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
if ($value !== null) { |
|
150
|
|
|
return $this->setCode($value); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $this->getCode(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Gets a result exception. |
|
158
|
|
|
* |
|
159
|
|
|
* @return Exception |
|
160
|
|
|
*/ |
|
161
|
56 |
|
public function getException() |
|
162
|
|
|
{ |
|
163
|
56 |
|
return $this->_exception; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Sets a result exception. |
|
168
|
|
|
* |
|
169
|
|
|
* @param Exception $value exception to be delivered for the api |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
8 |
|
public function setException($value) |
|
173
|
|
|
{ |
|
174
|
8 |
|
$this->_exception = $value; |
|
175
|
|
|
|
|
176
|
8 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Exception api method. |
|
181
|
|
|
* |
|
182
|
|
|
* @param Exception $value exception of the api request |
|
183
|
|
|
* @deprecated 3.6.0 Use setException()/getException() instead. |
|
184
|
|
|
* @return Exception |
|
185
|
|
|
*/ |
|
186
|
|
|
public function exception($value = null) |
|
187
|
|
|
{ |
|
188
|
|
|
deprecationWarning( |
|
189
|
|
|
'Result::exception() is deprecated. ' . |
|
190
|
|
|
'Use Result::setException()/getException() instead.' |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
|
|
if ($value !== null) { |
|
194
|
|
|
return $this->setException($value); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->getException(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Appends value to Payload. |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $key the key to be used in the payload |
|
204
|
|
|
* @param mixed $value value to be used as payload |
|
205
|
|
|
* @return void |
|
206
|
|
|
*/ |
|
207
|
30 |
|
public function appendPayload($key, $value) |
|
208
|
|
|
{ |
|
209
|
30 |
|
$this->_payload[$key] = $value; |
|
210
|
30 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Gets a result payload. |
|
214
|
|
|
* |
|
215
|
|
|
* @return array|mixed Payload |
|
216
|
|
|
*/ |
|
217
|
54 |
|
public function getPayload($key = null) |
|
218
|
|
|
{ |
|
219
|
54 |
|
if ($key === null) { |
|
220
|
54 |
|
return $this->_payload; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
if (isset($this->_payload[$key])) { |
|
224
|
|
|
return $this->_payload[$key]; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return null; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Sets a result payload. |
|
232
|
|
|
* |
|
233
|
|
|
* @param mixed $value payload to be delivered for the api |
|
234
|
|
|
* @return $this |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setPayload($value) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->_payload = $value; |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
return $this; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Payload api method. |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $value payload of the api request |
|
|
|
|
|
|
247
|
|
|
* @deprecated 3.6.0 Use getPayload() instead. |
|
248
|
|
|
* @return mixed |
|
249
|
|
|
*/ |
|
250
|
|
|
public function payload($key = null) |
|
251
|
|
|
{ |
|
252
|
|
|
deprecationWarning( |
|
253
|
|
|
'Result::payload() is deprecated. ' . |
|
254
|
|
|
'Use Result::getPayload() instead.' |
|
255
|
|
|
); |
|
256
|
|
|
|
|
257
|
|
|
return $this->getPayload($key); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* To array transformation. |
|
262
|
|
|
* |
|
263
|
|
|
* @return array |
|
264
|
|
|
*/ |
|
265
|
|
|
public function toArray() |
|
266
|
|
|
{ |
|
267
|
|
|
$info = [ |
|
268
|
|
|
'code' => $this->_code, |
|
269
|
|
|
'data' => $this->_data, |
|
270
|
|
|
'payload' => $this->_payload, |
|
271
|
|
|
]; |
|
272
|
|
|
if ($this->_exception !== null) { |
|
273
|
|
|
$info['exception'] = $this->_exception->getMessage(); |
|
274
|
|
|
$info['exceptionStack'] = $this->_exception->getTraceAsString(); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return $info; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Returns an array that can be used to describe the internal state of this |
|
282
|
|
|
* object. |
|
283
|
|
|
* |
|
284
|
|
|
* @return array |
|
285
|
|
|
*/ |
|
286
|
|
|
public function __debugInfo() |
|
287
|
|
|
{ |
|
288
|
|
|
return $this->toArray(); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.