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