|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Transformers; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use JsonSerializable; |
|
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
8
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ResponseApiDataAbstract implements ArrayAccess, Arrayable, Jsonable, JsonSerializable |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Illuminate\Support\Collection |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $data; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ResponseApiDataAbstract constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $data |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($data = []) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->setData($data); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Determine if an item exists at an offset. |
|
29
|
|
|
* |
|
30
|
|
|
* @param mixed $key |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function offsetExists($key) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getData()->offsetExists($key); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get Custom Data. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Support\Collection |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function getData() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->data; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set Custom Data. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $data |
|
53
|
|
|
* |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setData($data) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->data = collect($data); |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get an item at a given offset. |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $key |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function offsetGet($key) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->{$key}; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the item at a given offset. |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $key |
|
79
|
|
|
* @param mixed $value |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function offsetSet($key, $value) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->getData()->offsetSet($key, $value); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Unset the item at a given offset. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $key |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function offsetUnset($key) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->getData()->offsetUnset($key); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get value by name from data collection. |
|
102
|
|
|
* |
|
103
|
|
|
* @param $name |
|
104
|
|
|
* |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
|
|
public function __get($name) |
|
108
|
|
|
{ |
|
109
|
|
|
return ($value = $this->getData()->get($name)) ? $value : data_get($this->response(), $name); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Set new value for data collection. |
|
114
|
|
|
* |
|
115
|
|
|
* @param $name |
|
116
|
|
|
* @param $value |
|
117
|
|
|
*/ |
|
118
|
|
|
public function __set($name, $value) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->getData()->offsetSet($name, $value); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get response data to pass to transformers. |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
abstract public function response(); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get response as object. |
|
132
|
|
|
* |
|
133
|
|
|
* @return object |
|
134
|
|
|
*/ |
|
135
|
|
|
public function toObject() |
|
136
|
|
|
{ |
|
137
|
|
|
return (object) $this->response(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the collection of items as a plain array. |
|
142
|
|
|
* |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public function toArray() |
|
146
|
|
|
{ |
|
147
|
|
|
return array_map(function ($value) { |
|
148
|
|
|
return $value instanceof Arrayable ? $value->toArray() : $value; |
|
149
|
|
|
}, $this->response()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Convert the collection to its string representation. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function __toString() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->toJson(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get the collection of items as JSON. |
|
164
|
|
|
* |
|
165
|
|
|
* @param int $options |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function toJson($options = 0) |
|
170
|
|
|
{ |
|
171
|
|
|
return json_encode($this->jsonSerialize(), $options); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Convert the object into something JSON serializable. |
|
176
|
|
|
* |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public function jsonSerialize() |
|
180
|
|
|
{ |
|
181
|
|
|
return array_map(function ($value) { |
|
182
|
|
|
if ($value instanceof JsonSerializable) { |
|
183
|
|
|
return $value->jsonSerialize(); |
|
184
|
|
|
} elseif ($value instanceof Jsonable) { |
|
185
|
|
|
return json_decode($value->toJson(), true); |
|
186
|
|
|
} elseif ($value instanceof Arrayable) { |
|
187
|
|
|
return $value->toArray(); |
|
188
|
|
|
} else { |
|
189
|
|
|
return $value; |
|
190
|
|
|
} |
|
191
|
|
|
}, $this->response()); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Return a new JSON response. |
|
196
|
|
|
* |
|
197
|
|
|
* @author Spatie |
|
198
|
|
|
* |
|
199
|
|
|
* @param callable|int $statusCode |
|
200
|
|
|
* @param callable|array $headers |
|
201
|
|
|
* |
|
202
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
203
|
|
|
*/ |
|
204
|
|
|
public function respond($statusCode = 200, $headers = []) |
|
205
|
|
|
{ |
|
206
|
|
|
$response = new JsonResponse(); |
|
207
|
|
|
|
|
208
|
|
|
$response->setData($this->toArray()); |
|
209
|
|
|
|
|
210
|
|
|
if (is_int($statusCode)) { |
|
211
|
|
|
$statusCode = function (JsonResponse $response) use ($statusCode) { |
|
212
|
|
|
return $response->setStatusCode($statusCode); |
|
213
|
|
|
}; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if (is_array($headers)) { |
|
217
|
|
|
$headers = function (JsonResponse $response) use ($headers) { |
|
218
|
|
|
return $response->withHeaders($headers); |
|
219
|
|
|
}; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if (is_callable($statusCode)) { |
|
223
|
|
|
$statusCode($response); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if (is_callable($headers)) { |
|
227
|
|
|
$headers($response); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $response; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Handle dynamic data collection method. |
|
235
|
|
|
* |
|
236
|
|
|
* @param $name |
|
237
|
|
|
* @param $arguments |
|
238
|
|
|
* |
|
239
|
|
|
* @return mixed |
|
240
|
|
|
*/ |
|
241
|
|
|
public function __call($name, $arguments) |
|
242
|
|
|
{ |
|
243
|
|
|
if (! method_exists($this, $name)) { |
|
244
|
|
|
return $this->getData()->$name(...$arguments); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $this->$name(...$arguments); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|