1 | <?php |
||
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 = []) |
||
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) |
||
38 | |||
39 | /** |
||
40 | * Get Custom Data. |
||
41 | * |
||
42 | * @return \Illuminate\Support\Collection |
||
43 | */ |
||
44 | protected function getData() |
||
48 | |||
49 | /** |
||
50 | * Set Custom Data. |
||
51 | * |
||
52 | * @param array $data |
||
53 | * |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function setData($data) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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() |
||
139 | |||
140 | /** |
||
141 | * Get the collection of items as a plain array. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function toArray() |
||
151 | |||
152 | /** |
||
153 | * Convert the collection to its string representation. |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function __toString() |
||
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) |
||
173 | |||
174 | /** |
||
175 | * Convert the object into something JSON serializable. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function jsonSerialize() |
||
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 = []) |
||
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) |
||
249 | } |
||
250 |