1 | <?php |
||
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) |
|
67 | |||
68 | /** |
||
69 | * Gets a result data. |
||
70 | * |
||
71 | * @return array|mixed |
||
72 | */ |
||
73 | 56 | public function getData() |
|
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) |
|
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) |
||
111 | |||
112 | /** |
||
113 | * Gets a result code. |
||
114 | * |
||
115 | * @return int |
||
116 | */ |
||
117 | 60 | public function getCode() |
|
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) |
|
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) |
||
155 | |||
156 | /** |
||
157 | * Gets a result exception. |
||
158 | * |
||
159 | * @return Exception |
||
160 | */ |
||
161 | 56 | public function getException() |
|
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) |
|
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) |
||
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) |
|
211 | |||
212 | /** |
||
213 | * Gets a result payload. |
||
214 | * |
||
215 | * @return array|mixed Payload |
||
216 | */ |
||
217 | 54 | public function getPayload($key = null) |
|
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) |
||
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) |
||
259 | |||
260 | /** |
||
261 | * To array transformation. |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public function toArray() |
||
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() |
||
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.