|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrightComponents\Common\Payloads; |
|
4
|
|
|
|
|
5
|
|
|
use Traversable; |
|
6
|
|
|
use JsonSerializable; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
10
|
|
|
use BrightComponents\Common\Payloads\Contracts\PayloadContract; |
|
11
|
|
|
|
|
12
|
|
|
class Payload implements PayloadContract |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $status; |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
private $input = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $output = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
private $messages = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $outputWrapper = 'data'; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $inputWrapper = 'input'; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $messagesWrapper = 'messages'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a copy of the payload with the status. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $status |
|
39
|
|
|
* |
|
40
|
|
|
* @return \BrightComponents\Common\Payloads\Contracts\PayloadContract |
|
41
|
|
|
*/ |
|
42
|
|
|
public function withStatus($status) |
|
43
|
|
|
{ |
|
44
|
|
|
$copy = clone $this; |
|
45
|
|
|
$copy->status = $status; |
|
46
|
|
|
|
|
47
|
|
|
return $copy; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the status of the payload. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getStatus() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->status; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Create a copy of the payload with input array. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $input |
|
64
|
|
|
* @param bool $wrap |
|
65
|
|
|
* |
|
66
|
|
|
* @return \BrightComponents\Common\Payloads\Contracts\PayloadContract |
|
67
|
|
|
*/ |
|
68
|
|
|
public function withInput(array $input, bool $wrap = true) |
|
69
|
|
|
{ |
|
70
|
|
|
$copy = clone $this; |
|
71
|
|
|
$copy->input = $wrap && $this->inputWrapper ? [$this->inputWrapper => $input] : $input; |
|
72
|
|
|
|
|
73
|
|
|
return $copy; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get input array from the payload. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getInput() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->input; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create a copy of the payload with output. |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $output |
|
90
|
|
|
* @param bool $wrap |
|
91
|
|
|
* |
|
92
|
|
|
* @return \BrightComponents\Common\Payloads\Contracts\PayloadContract |
|
93
|
|
|
*/ |
|
94
|
|
|
public function withOutput($output, bool $wrap = true) |
|
95
|
|
|
{ |
|
96
|
|
|
$copy = clone $this; |
|
97
|
|
|
$copy->output = $wrap && $this->outputWrapper ? [$this->outputWrapper => $this->getArrayableItems($output)] : $this->getArrayableItems($output); |
|
98
|
|
|
|
|
99
|
|
|
return $copy; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get output array from the payload. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getOutput() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->output; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Create a copy of the payload with messages array. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $output |
|
|
|
|
|
|
116
|
|
|
* @param bool $wrap |
|
117
|
|
|
* |
|
118
|
|
|
* @return \BrightComponents\Common\Payloads\Contracts\PayloadContract |
|
119
|
|
|
*/ |
|
120
|
|
|
public function withMessages(array $messages, bool $wrap = true) |
|
121
|
|
|
{ |
|
122
|
|
|
$copy = clone $this; |
|
123
|
|
|
$copy->messages = $wrap && $this->messagesWrapper ? [$this->messagesWrapper => $messages] : $messages; |
|
124
|
|
|
|
|
125
|
|
|
return $copy; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get messages array from the payload. |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getMessages() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->messages; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set a wrapper for payload output. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $wrapper |
|
142
|
|
|
* |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setOutputWrapper(string $wrapper) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->outputWrapper = $wrapper; |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Set a wrapper for payload output. Alias for setWrapper. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $wrapper |
|
156
|
|
|
* |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function wrapOutput(string $wrapper) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->setOutputWrapper($wrapper); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Set a wrapper for payload output. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $wrapper |
|
168
|
|
|
* |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setInputWrapper(string $wrapper) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->inputWrapper = $wrapper; |
|
174
|
|
|
|
|
175
|
|
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Set a wrapper for payload output. Alias for setWrapper. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $wrapper |
|
182
|
|
|
* |
|
183
|
|
|
* @return $this |
|
184
|
|
|
*/ |
|
185
|
|
|
public function wrapInput(string $wrapper) |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->setInputWrapper($wrapper); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Set a wrapper for payload output. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $wrapper |
|
194
|
|
|
* |
|
195
|
|
|
* @return $this |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setMessagesWrapper(string $wrapper) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->messagesWrapper = $wrapper; |
|
200
|
|
|
|
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Set a wrapper for payload output. Alias for setWrapper. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $wrapper |
|
208
|
|
|
* |
|
209
|
|
|
* @return $this |
|
210
|
|
|
*/ |
|
211
|
|
|
public function wrapMessages(string $wrapper) |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->setMessagesWrapper($wrapper); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get the arrayable items. |
|
218
|
|
|
* |
|
219
|
|
|
* @param mixed $input |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getArrayableItems($input) |
|
224
|
|
|
{ |
|
225
|
|
|
if (is_array($input)) { |
|
226
|
|
|
return $input; |
|
227
|
|
|
} elseif ($input instanceof Collection) { |
|
228
|
|
|
return $input->all(); |
|
229
|
|
|
} elseif ($input instanceof Arrayable) { |
|
230
|
|
|
return $input->toArray(); |
|
231
|
|
|
} elseif ($input instanceof Jsonable) { |
|
232
|
|
|
return json_decode($input->toJson(), true); |
|
233
|
|
|
} elseif ($input instanceof JsonSerializable) { |
|
|
|
|
|
|
234
|
|
|
return $input->jsonSerialize(); |
|
235
|
|
|
} elseif ($input instanceof Traversable) { |
|
236
|
|
|
return iterator_to_array($input); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return (array) $input; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Handle dynamic property getters for a payload. |
|
244
|
|
|
* |
|
245
|
|
|
* @param mixed $property |
|
246
|
|
|
* |
|
247
|
|
|
* @return mixed |
|
248
|
|
|
*/ |
|
249
|
|
|
public function __get($property) |
|
250
|
|
|
{ |
|
251
|
|
|
if ($this->outputWrapper) { |
|
252
|
|
|
return isset($this->getOutput()[$this->outputWrapper][$property]) ? $this->getOutput()[$this->outputWrapper][$property] : null; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
return isset($this->getOutput()[$property]) ? $this->getOutput()[$property] : null; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.