1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PerfectOblivion\Payload; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Traversable; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use PerfectOblivion\Payload\Contracts\PayloadContract; |
11
|
|
|
|
12
|
|
|
class Payload implements PayloadContract, ArrayAccess, JsonSerializable, Jsonable, Arrayable |
13
|
|
|
{ |
14
|
|
|
/** @var int */ |
15
|
|
|
private $status; |
16
|
|
|
|
17
|
|
|
/** @var mixed */ |
18
|
|
|
private $output; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
private $messages = []; |
22
|
|
|
|
23
|
|
|
/** @var string|null */ |
24
|
|
|
private $outputWrapper = null; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $messagesWrapper = 'messages'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initialize a Payload object. |
31
|
|
|
*/ |
32
|
|
|
public static function instance(): PayloadContract |
33
|
|
|
{ |
34
|
|
|
return new static; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the Payload status. |
39
|
|
|
* |
40
|
|
|
* @param int $status |
41
|
|
|
*/ |
42
|
|
|
public function setStatus(int $status): PayloadContract |
43
|
|
|
{ |
44
|
|
|
$instance = clone $this; |
45
|
|
|
$instance->status = $status; |
46
|
|
|
|
47
|
|
|
return $instance; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the status of the payload. |
52
|
|
|
*/ |
53
|
|
|
public function getStatus(): int |
54
|
|
|
{ |
55
|
|
|
return $this->status; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the Payload messages. |
60
|
|
|
* |
61
|
|
|
* @param array $output |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function setMessages(array $messages): PayloadContract |
64
|
|
|
{ |
65
|
|
|
$instance = clone $this; |
66
|
|
|
$instance->messages = $messages; |
67
|
|
|
|
68
|
|
|
return $instance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get messages array from the payload. |
73
|
|
|
*/ |
74
|
|
|
public function getMessages(): array |
75
|
|
|
{ |
76
|
|
|
return $this->messages; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the Payload output. |
81
|
|
|
* |
82
|
|
|
* @param mixed $output |
83
|
|
|
* @param string|null $wrapper |
84
|
|
|
*/ |
85
|
|
|
public function setOutput($output, ? string $wrapper = null): PayloadContract |
86
|
|
|
{ |
87
|
|
|
if ($wrapper) { |
|
|
|
|
88
|
|
|
$this->outputWrapper = $wrapper; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$instance = clone $this; |
92
|
|
|
$instance->output = $output; |
93
|
|
|
|
94
|
|
|
return $instance; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the Payload output. |
99
|
|
|
*/ |
100
|
|
|
public function getOutput(): array |
101
|
|
|
{ |
102
|
|
|
return $this->getArrayableItems($this->output); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the raw Payload output. |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function getRawOutput() |
111
|
|
|
{ |
112
|
|
|
return $this->output; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieve the Payload output and wrap it. |
117
|
|
|
* Use the outputWrapper if it is set, otherwise use 'data'. |
118
|
|
|
*/ |
119
|
|
|
public function getwrappedOutput(): array |
120
|
|
|
{ |
121
|
|
|
return $this->outputWrapper ? [$this->outputWrapper => $this->output] : ['data' => $this->output]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the wrapper for the output. |
126
|
|
|
*/ |
127
|
|
|
public function getOutputWrapper(): string |
128
|
|
|
{ |
129
|
|
|
return $this->outputWrapper; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the wrapper for the messages. |
134
|
|
|
*/ |
135
|
|
|
public function getMessagesWrapper(): string |
136
|
|
|
{ |
137
|
|
|
return $this->messagesWrapper; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return all of the components of the payload in array format. |
142
|
|
|
*/ |
143
|
|
|
public function all(): array |
144
|
|
|
{ |
145
|
|
|
$all = ['output' => $this->getOutput()]; |
146
|
|
|
if ($this->messages) { |
|
|
|
|
147
|
|
|
$all = array_merge($all, ['messages' => $this->getMessages()]); |
148
|
|
|
} |
149
|
|
|
if ($this->status) { |
150
|
|
|
$all = array_merge($all, ['status' => $this->getStatus()]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $all; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Convert the Payload output to an array. |
158
|
|
|
*/ |
159
|
|
|
public function getArrayableItems(): array |
160
|
|
|
{ |
161
|
|
|
if (is_array($this->output)) { |
162
|
|
|
return $this->output; |
163
|
|
|
} elseif ($this->output instanceof Arrayable) { |
164
|
|
|
return $this->output->toArray(); |
165
|
|
|
} elseif ($this->output instanceof Jsonable) { |
166
|
|
|
return json_decode($this->output->toJson(), true); |
167
|
|
|
} elseif ($this->output instanceof JsonSerializable) { |
|
|
|
|
168
|
|
|
return $this->output->jsonSerialize(); |
169
|
|
|
} elseif ($this->output instanceof Traversable) { |
170
|
|
|
return iterator_to_array($this->output); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return (array) $this->output; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Dynamically retrieve attributes on the OutputItem. |
178
|
|
|
* |
179
|
|
|
* @param string $key |
180
|
|
|
* |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
public function __get($key) |
184
|
|
|
{ |
185
|
|
|
return $this->output[$key]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Convert the Payload instance to JSON. |
190
|
|
|
* |
191
|
|
|
* @param int $options |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public function toJson($options = 0) |
196
|
|
|
{ |
197
|
|
|
return json_encode($this->jsonSerialize(), $options); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Convert the Payload into something JSON serializable. |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
|
public function jsonSerialize() |
206
|
|
|
{ |
207
|
|
|
return $this->toArray(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Convert the Payload instance to an array. |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function toArray() |
216
|
|
|
{ |
217
|
|
|
$output = $this->outputWrapper || $this->messages ? $this->getWrappedOutput() : $this->getOutput(); |
|
|
|
|
218
|
|
|
$messages = $this->messages ? [$this->messagesWrapper => $this->messages] : $this->messages; |
219
|
|
|
|
220
|
|
|
return $this->messages ? array_merge($output, $messages) : $output; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Determine if the given attribute exists. |
225
|
|
|
* |
226
|
|
|
* @param mixed $offset |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public function offsetExists($offset) |
231
|
|
|
{ |
232
|
|
|
return isset($this->$offset); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get the value for a given offset. |
237
|
|
|
* |
238
|
|
|
* @param mixed $offset |
239
|
|
|
* |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
|
|
public function offsetGet($offset) |
243
|
|
|
{ |
244
|
|
|
return $this->$offset; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Set the value for a given offset. |
249
|
|
|
* |
250
|
|
|
* @param mixed $offset |
251
|
|
|
* @param mixed $value |
252
|
|
|
*/ |
253
|
|
|
public function offsetSet($offset, $value) |
254
|
|
|
{ |
255
|
|
|
$this->$offset = $value; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Unset the value for a given offset. |
260
|
|
|
* |
261
|
|
|
* @param mixed $offset |
262
|
|
|
*/ |
263
|
|
|
public function offsetUnset($offset) |
264
|
|
|
{ |
265
|
|
|
unset($this->$offset); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Determine if an attribute exists on the Payload. |
270
|
|
|
* |
271
|
|
|
* @param string $key |
272
|
|
|
* |
273
|
|
|
* @return bool |
274
|
|
|
*/ |
275
|
|
|
public function __isset($key) |
276
|
|
|
{ |
277
|
|
|
if (! $this->output) { |
278
|
|
|
return false; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return isset($this->output[$key]); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Unset an attribute on the Payload. |
286
|
|
|
* |
287
|
|
|
* @param string $key |
288
|
|
|
*/ |
289
|
|
|
public function __unset($key) |
290
|
|
|
{ |
291
|
|
|
if (! $this->output) { |
292
|
|
|
return; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
unset($this->output[$key]); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Convert the Payload to its string representation. |
300
|
|
|
* |
301
|
|
|
* @return string |
302
|
|
|
*/ |
303
|
|
|
public function __toString() |
304
|
|
|
{ |
305
|
|
|
return $this->toJson(); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.