1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Http; |
4
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\Transformable; |
6
|
|
|
use Flugg\Responder\Exceptions\InvalidSerializerException; |
7
|
|
|
use Flugg\Responder\Exceptions\InvalidTransformerException; |
8
|
|
|
use Flugg\Responder\Exceptions\SerializerNotFoundException; |
9
|
|
|
use Flugg\Responder\ResourceFactory; |
10
|
|
|
use Flugg\Responder\ResourceResolver; |
11
|
|
|
use Flugg\Responder\Transformation; |
12
|
|
|
use Flugg\Responder\Transformer; |
13
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use League\Fractal\Manager; |
17
|
|
|
use League\Fractal\Resource\ResourceInterface; |
18
|
|
|
use League\Fractal\Serializer\SerializerAbstract; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class is a response builder for building successful JSON API responses and is |
22
|
|
|
* responsible for transforming and serializing the data. |
23
|
|
|
* |
24
|
|
|
* @package flugger/laravel-responder |
25
|
|
|
* @author Alexander Tømmerås <[email protected]> |
26
|
|
|
* @license The MIT License |
27
|
|
|
*/ |
28
|
|
|
class SuccessResponseBuilder extends ResponseBuilder |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The manager responsible for transforming and serializing data. |
32
|
|
|
* |
33
|
|
|
* @var \Flugg\Responder\Contracts\Manager |
34
|
|
|
*/ |
35
|
|
|
protected $manager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The meta data appended to the serialized data. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $meta = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The Fractal resource instance containing the data and transformer. |
46
|
|
|
* |
47
|
|
|
* @var \League\Fractal\Resource\ResourceInterface |
48
|
|
|
*/ |
49
|
|
|
protected $resource; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The resource factory used to generate resource instances. |
53
|
|
|
* |
54
|
|
|
* @var \Flugg\Responder\ResourceFactory |
55
|
|
|
*/ |
56
|
|
|
protected $resourceFactory; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The HTTP status code for the response. |
60
|
|
|
* |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
protected $statusCode = 200; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* SuccessResponseBuilder constructor. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory |
69
|
|
|
* @param \Flugg\Responder\ResourceFactory $resourceFactory |
70
|
|
|
* @param \League\Fractal\Manager $manager |
71
|
|
|
*/ |
72
|
|
|
public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager) |
73
|
|
|
{ |
74
|
|
|
$this->resourceFactory = $resourceFactory; |
75
|
|
|
$this->manager = $manager; |
|
|
|
|
76
|
|
|
$this->resource = $this->resourceFactory->make(); |
77
|
|
|
|
78
|
|
|
parent::__construct($responseFactory); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add data to the meta data appended to the response data. |
83
|
|
|
* |
84
|
|
|
* @param array $data |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
|
|
public function addMeta(array $data):SuccessResponseBuilder |
88
|
|
|
{ |
89
|
|
|
$this->meta = array_merge($this->meta, $data); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the serializer used to serialize the resource data. |
96
|
|
|
* |
97
|
|
|
* @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
|
|
public function serializer($serializer):SuccessResponseBuilder |
101
|
|
|
{ |
102
|
|
|
$this->manager->setSerializer($this->resolveSerializer($serializer)); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the HTTP status code for the response. |
109
|
|
|
* |
110
|
|
|
* @param int $statusCode |
111
|
|
|
* @return self |
112
|
|
|
* @throws \InvalidArgumentException |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function setStatus(int $statusCode):ResponseBuilder |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if ($statusCode < 100 || $statusCode >= 400) { |
117
|
|
|
throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code."); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return parent::setStatus($statusCode); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the transformation data. This will set a new resource instance on the response |
125
|
|
|
* builder depending on what type of data is provided. |
126
|
|
|
* |
127
|
|
|
* @param mixed|null $data |
128
|
|
|
* @param callable|string|null $transformer |
129
|
|
|
* @param string|null $resourceKey |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder |
133
|
|
|
{ |
134
|
|
|
$resource = $this->resourceFactory->make($data); |
135
|
|
|
|
136
|
|
|
if (! is_null($resource->getData())) { |
137
|
|
|
$model = $this->resolveModel($resource->getData()); |
138
|
|
|
$transformer = $this->resolveTransformer($model, $transformer); |
139
|
|
|
$resourceKey = $this->resolveResourceKey($model, $resourceKey); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey); |
|
|
|
|
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Convert the response to an array. |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function toArray():array |
153
|
|
|
{ |
154
|
|
|
return $this->serialize($this->getResource()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the Fractal resource instance. |
159
|
|
|
* |
160
|
|
|
* @return \League\Fractal\Resource\ResourceInterface |
161
|
|
|
*/ |
162
|
|
|
public function getResource():ResourceInterface |
163
|
|
|
{ |
164
|
|
|
return $this->resource->setMeta($this->meta); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get the Fractal manager responsible for transforming and serializing the data. |
169
|
|
|
* |
170
|
|
|
* @return \League\Fractal\Manager |
171
|
|
|
*/ |
172
|
|
|
public function getManager():Manager |
173
|
|
|
{ |
174
|
|
|
return $this->manager; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Resolve a serializer instance from the value. |
179
|
|
|
* |
180
|
|
|
* @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
181
|
|
|
* @return \League\Fractal\Serializer\SerializerAbstract |
182
|
|
|
* @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
183
|
|
|
*/ |
184
|
|
|
protected function resolveSerializer($serializer):SerializerAbstract |
185
|
|
|
{ |
186
|
|
|
if (is_string($serializer)) { |
187
|
|
|
$serializer = new $serializer; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (! $serializer instanceof SerializerAbstract) { |
191
|
|
|
throw new InvalidSerializerException(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $serializer; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Resolve a model instance from the data. |
199
|
|
|
* |
200
|
|
|
* @param \Illuminate\Database\Eloquent\Model|array $data |
201
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
202
|
|
|
* @throws \InvalidArgumentException |
203
|
|
|
*/ |
204
|
|
|
protected function resolveModel($data):Model |
205
|
|
|
{ |
206
|
|
|
if ($data instanceof Model) { |
207
|
|
|
return $data; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$model = $data[0]; |
211
|
|
|
if (! $model instanceof Model) { |
212
|
|
|
throw new InvalidArgumentException('You can only transform data containing Eloquent models.'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $model; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Resolve a transformer. |
220
|
|
|
* |
221
|
|
|
* @param \Illuminate\Database\ELoquent\Model $model |
222
|
|
|
* @param \Flugg\Responder\Transformer|callable|null $transformer |
223
|
|
|
* @return \Flugg\Responder\Transformer|callable |
224
|
|
|
*/ |
225
|
|
|
protected function resolveTransformer(Model $model, $transformer = null) |
226
|
|
|
{ |
227
|
|
|
$transformer = $transformer ?: $this->resolveTransformerFromModel($model); |
228
|
|
|
|
229
|
|
|
if (is_string($transformer)) { |
230
|
|
|
$transformer = new $transformer; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->parseTransformer($transformer); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Resolve a transformer from the model. If the model is not transformable, a closure |
238
|
|
|
* based transformer will be created instead, from the model's fillable attributes. |
239
|
|
|
* |
240
|
|
|
* @param \Illuminate\Database\ELoquent\Model $model |
241
|
|
|
* @return \Flugg\Responder\Transformer|callable |
242
|
|
|
*/ |
243
|
|
|
protected function resolveTransformerFromModel(Model $model) |
244
|
|
|
{ |
245
|
|
|
if (! $model instanceof Transformable) { |
246
|
|
|
return function () use ($model) { |
247
|
|
|
return $model->toArray(); |
248
|
|
|
}; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $model::transformer(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Parse a transformer class and set relations. |
256
|
|
|
* |
257
|
|
|
* @param \Flugg\Responder\Transformer|callable $transformer |
258
|
|
|
* @return \Flugg\Responder\Transformer|callable |
259
|
|
|
* @throws \InvalidTransformerException |
260
|
|
|
*/ |
261
|
|
|
protected function parseTransformer($transformer) |
262
|
|
|
{ |
263
|
|
|
if ($transformer instanceof Transformer) { |
264
|
|
|
$transformer = $transformer->setRelations($this->resolveRelations($model)); |
|
|
|
|
265
|
|
|
$this->manager->parseIncludes($transformer->getRelations()); |
266
|
|
|
|
267
|
|
|
if (! is_callable($transformer)) { |
268
|
|
|
throw new InvalidTransformerException($model); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $transformer; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Resolve eager loaded relations from the model. |
277
|
|
|
* |
278
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
279
|
|
|
* @return array |
280
|
|
|
*/ |
281
|
|
|
protected function resolveRelations(Model $model):array |
282
|
|
|
{ |
283
|
|
|
return array_keys($model->getRelations()); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Resolve the resource key from the model. |
288
|
|
|
* |
289
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
290
|
|
|
* @param string|null $resourceKey |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
protected function resolveResourceKey(Model $model, string $resourceKey = null):string |
294
|
|
|
{ |
295
|
|
|
if (! is_null($resourceKey)) { |
296
|
|
|
return $resourceKey; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (method_exists($model, 'getResourceKey')) { |
300
|
|
|
return $model->getResourceKey(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return $model->getTable(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Serialize the transformation data. |
308
|
|
|
* |
309
|
|
|
* @param ResourceInterface $resource |
310
|
|
|
* @return array |
311
|
|
|
*/ |
312
|
|
|
protected function serialize(ResourceInterface $resource):array |
313
|
|
|
{ |
314
|
|
|
return $this->manager->createData($resource)->toArray(); |
315
|
|
|
} |
316
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..