|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rexlabs\Smokescreen; |
|
4
|
|
|
|
|
5
|
|
|
use Rexlabs\Smokescreen\Exception\IncludeException; |
|
6
|
|
|
use Rexlabs\Smokescreen\Exception\InvalidTransformerException; |
|
7
|
|
|
use Rexlabs\Smokescreen\Exception\MissingResourceException; |
|
8
|
|
|
use Rexlabs\Smokescreen\Exception\UnhandledResourceType; |
|
9
|
|
|
use Rexlabs\Smokescreen\Helpers\JsonHelper; |
|
10
|
|
|
use Rexlabs\Smokescreen\Includes\IncludeParser; |
|
11
|
|
|
use Rexlabs\Smokescreen\Includes\IncludeParserInterface; |
|
12
|
|
|
use Rexlabs\Smokescreen\Includes\Includes; |
|
13
|
|
|
use Rexlabs\Smokescreen\Relations\RelationLoaderInterface; |
|
14
|
|
|
use Rexlabs\Smokescreen\Resource\Collection; |
|
15
|
|
|
use Rexlabs\Smokescreen\Resource\Item; |
|
16
|
|
|
use Rexlabs\Smokescreen\Resource\ResourceInterface; |
|
17
|
|
|
use Rexlabs\Smokescreen\Serializer\DefaultSerializer; |
|
18
|
|
|
use Rexlabs\Smokescreen\Serializer\SerializerInterface; |
|
19
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerInterface; |
|
20
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerResolverInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Smokescreen is a library for transforming and serializing data - typically RESTful API output. |
|
24
|
|
|
*/ |
|
25
|
|
|
class Smokescreen implements \JsonSerializable |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var ResourceInterface Item or Collection to be transformed */ |
|
28
|
|
|
protected $resource; |
|
29
|
|
|
|
|
30
|
|
|
/** @var SerializerInterface */ |
|
31
|
|
|
protected $serializer; |
|
32
|
|
|
|
|
33
|
|
|
/** @var IncludeParserInterface */ |
|
34
|
|
|
protected $includeParser; |
|
35
|
|
|
|
|
36
|
|
|
/** @var RelationLoaderInterface */ |
|
37
|
|
|
protected $relationLoader; |
|
38
|
|
|
|
|
39
|
|
|
/** @var Includes */ |
|
40
|
|
|
protected $includes; |
|
41
|
|
|
|
|
42
|
|
|
/** @var TransformerResolverInterface */ |
|
43
|
|
|
protected $transformerResolver; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Return the current resource. |
|
47
|
|
|
* |
|
48
|
|
|
* @return ResourceInterface|mixed|null |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function getResource() |
|
51
|
|
|
{ |
|
52
|
6 |
|
return $this->resource; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the resource to be transformed. |
|
57
|
|
|
* |
|
58
|
|
|
* @param ResourceInterface|mixed|null $resource |
|
59
|
|
|
* |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
21 |
|
public function setResource($resource) |
|
63
|
|
|
{ |
|
64
|
21 |
|
$this->resource = $resource; |
|
65
|
|
|
|
|
66
|
21 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the resource item to be transformed. |
|
71
|
|
|
* |
|
72
|
|
|
* @param mixed $data |
|
73
|
|
|
* @param TransformerInterface|mixed|null $transformer |
|
74
|
|
|
* @param string|null $key |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
78
|
|
|
*/ |
|
79
|
15 |
|
public function item($data, $transformer = null, $key = null) |
|
80
|
|
|
{ |
|
81
|
15 |
|
$this->setResource(new Item($data, $transformer, $key)); |
|
82
|
|
|
|
|
83
|
14 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Set the resource collection to be transformed. |
|
88
|
|
|
* |
|
89
|
|
|
* @param mixed $data |
|
90
|
|
|
* @param TransformerInterface|mixed|null $transformer |
|
91
|
|
|
* @param string|null $key |
|
92
|
|
|
* @param callable|null $callback |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
96
|
|
|
*/ |
|
97
|
7 |
|
public function collection($data, TransformerInterface $transformer = null, $key = null, callable $callback = null) |
|
98
|
|
|
{ |
|
99
|
7 |
|
$this->setResource(new Collection($data, $transformer, $key)); |
|
100
|
7 |
|
if ($callback !== null) { |
|
101
|
1 |
|
$callback($this->resource); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
7 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets the transformer to be used to transform the resource ... later. |
|
109
|
|
|
* |
|
110
|
|
|
* @throws MissingResourceException |
|
111
|
|
|
* |
|
112
|
|
|
* @return TransformerInterface|mixed|null |
|
113
|
|
|
*/ |
|
114
|
5 |
|
public function getTransformer() |
|
115
|
|
|
{ |
|
116
|
5 |
|
if (!$this->resource) { |
|
117
|
1 |
|
throw new MissingResourceException('Resource must be specified before setting a transformer'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
return $this->resource->getTransformer(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets the transformer to be used to transform the resource ... later. |
|
125
|
|
|
* |
|
126
|
|
|
* @param TransformerInterface|mixed|null $transformer |
|
127
|
|
|
* |
|
128
|
|
|
* @throws MissingResourceException |
|
129
|
|
|
* |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function setTransformer($transformer = null) |
|
133
|
|
|
{ |
|
134
|
2 |
|
if (!$this->resource) { |
|
135
|
1 |
|
throw new MissingResourceException('Resource must be specified before setting a transformer'); |
|
136
|
|
|
} |
|
137
|
1 |
|
$this->resource->setTransformer($transformer); |
|
138
|
|
|
|
|
139
|
1 |
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns an object (stdClass) representation of the transformed/serialized data. |
|
144
|
|
|
* |
|
145
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
146
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
147
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\JsonEncodeException |
|
148
|
|
|
* |
|
149
|
|
|
* @return \stdClass |
|
150
|
|
|
* @throws IncludeException |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function toObject(): \stdClass |
|
153
|
|
|
{ |
|
154
|
1 |
|
return (object) json_decode($this->toJson(), false); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Outputs a JSON string of the resulting transformed and serialized data. |
|
159
|
|
|
* |
|
160
|
|
|
* @param int $options |
|
161
|
|
|
* |
|
162
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
163
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
164
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
165
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\JsonEncodeException |
|
166
|
|
|
* |
|
167
|
|
|
* @return string |
|
168
|
|
|
* @throws IncludeException |
|
169
|
|
|
*/ |
|
170
|
2 |
|
public function toJson($options = 0): string |
|
171
|
|
|
{ |
|
172
|
2 |
|
return JsonHelper::encode($this->jsonSerialize(), $options); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Output the transformed and serialized data as an array. |
|
177
|
|
|
* Implements PHP's JsonSerializable interface. |
|
178
|
|
|
* |
|
179
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
180
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
181
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
* |
|
185
|
|
|
* @see Smokescreen::toArray() |
|
186
|
|
|
* @throws IncludeException |
|
187
|
|
|
*/ |
|
188
|
2 |
|
public function jsonSerialize(): array |
|
189
|
|
|
{ |
|
190
|
2 |
|
return $this->toArray(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Return the transformed data as an array. |
|
195
|
|
|
* |
|
196
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
197
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
198
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
* @throws IncludeException |
|
202
|
|
|
*/ |
|
203
|
17 |
|
public function toArray(): array |
|
204
|
|
|
{ |
|
205
|
17 |
|
if (!$this->resource) { |
|
206
|
1 |
|
throw new MissingResourceException('No resource has been defined to transform'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Kick of serialization of the resource |
|
210
|
16 |
|
return $this->serializeResource($this->resource, $this->getIncludes()); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return SerializerInterface |
|
215
|
|
|
*/ |
|
216
|
15 |
|
public function getSerializer(): SerializerInterface |
|
217
|
|
|
{ |
|
218
|
15 |
|
return $this->serializer ?? new DefaultSerializer(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Set the serializer which will be used to output the transformed resource. |
|
223
|
|
|
* |
|
224
|
|
|
* @param SerializerInterface|null $serializer |
|
225
|
|
|
* |
|
226
|
|
|
* @return $this |
|
227
|
|
|
*/ |
|
228
|
1 |
|
public function setSerializer(SerializerInterface $serializer = null) |
|
229
|
|
|
{ |
|
230
|
1 |
|
$this->serializer = $serializer; |
|
231
|
|
|
|
|
232
|
1 |
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get the current includes object. |
|
237
|
|
|
* |
|
238
|
|
|
* @return Includes |
|
239
|
|
|
*/ |
|
240
|
18 |
|
public function getIncludes(): Includes |
|
241
|
|
|
{ |
|
242
|
18 |
|
return $this->includes ?? new Includes(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Set the Includes object used for determining included resources. |
|
247
|
|
|
* |
|
248
|
|
|
* @param Includes $includes |
|
249
|
|
|
* |
|
250
|
|
|
* @return $this |
|
251
|
|
|
*/ |
|
252
|
1 |
|
public function setIncludes(Includes $includes) |
|
253
|
|
|
{ |
|
254
|
1 |
|
$this->includes = $includes; |
|
255
|
|
|
|
|
256
|
1 |
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Parse the given string to generate a new Includes object. |
|
261
|
|
|
* |
|
262
|
|
|
* @param string $str |
|
263
|
|
|
* |
|
264
|
|
|
* @return $this |
|
265
|
|
|
*/ |
|
266
|
7 |
|
public function parseIncludes($str) |
|
267
|
|
|
{ |
|
268
|
7 |
|
$this->includes = $this->getIncludeParser()->parse(!empty($str) ? $str : ''); |
|
269
|
|
|
|
|
270
|
7 |
|
return $this; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Return the include parser object. |
|
275
|
|
|
* If not set explicitly via setIncludeParser(), it will return the default IncludeParser object. |
|
276
|
|
|
* |
|
277
|
|
|
* @return IncludeParserInterface |
|
278
|
|
|
* |
|
279
|
|
|
* @see Smokescreen::setIncludeParser() |
|
280
|
|
|
*/ |
|
281
|
7 |
|
public function getIncludeParser(): IncludeParserInterface |
|
282
|
|
|
{ |
|
283
|
7 |
|
return $this->includeParser ?? new IncludeParser(); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Set the include parser to handle converting a string to an Includes object. |
|
288
|
|
|
* |
|
289
|
|
|
* @param IncludeParserInterface $includeParser |
|
290
|
|
|
* |
|
291
|
|
|
* @return $this |
|
292
|
|
|
*/ |
|
293
|
1 |
|
public function setIncludeParser(IncludeParserInterface $includeParser) |
|
294
|
|
|
{ |
|
295
|
1 |
|
$this->includeParser = $includeParser; |
|
296
|
|
|
|
|
297
|
1 |
|
return $this; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Get the current relation loader. |
|
302
|
|
|
* |
|
303
|
|
|
* @return RelationLoaderInterface|null |
|
304
|
|
|
*/ |
|
305
|
1 |
|
public function getRelationLoader() |
|
306
|
|
|
{ |
|
307
|
1 |
|
return $this->relationLoader; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Set the relationship loader. |
|
312
|
|
|
* |
|
313
|
|
|
* @param RelationLoaderInterface $relationLoader |
|
314
|
|
|
* |
|
315
|
|
|
* @return $this |
|
316
|
|
|
*/ |
|
317
|
1 |
|
public function setRelationLoader(RelationLoaderInterface $relationLoader) |
|
318
|
|
|
{ |
|
319
|
1 |
|
$this->relationLoader = $relationLoader; |
|
320
|
|
|
|
|
321
|
1 |
|
return $this; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Returns true if a RelationLoaderInterface object has been defined. |
|
326
|
|
|
* |
|
327
|
|
|
* @return bool |
|
328
|
|
|
*/ |
|
329
|
1 |
|
public function hasRelationLoader(): bool |
|
330
|
|
|
{ |
|
331
|
1 |
|
return $this->relationLoader !== null; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @param ResourceInterface|mixed $resource |
|
336
|
|
|
* @param Includes $includes |
|
337
|
|
|
* |
|
338
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidSerializerException |
|
339
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
340
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
341
|
|
|
* |
|
342
|
|
|
* @return array|mixed |
|
343
|
|
|
* @throws IncludeException |
|
344
|
|
|
*/ |
|
345
|
16 |
|
protected function serializeResource($resource, Includes $includes): array |
|
346
|
|
|
{ |
|
347
|
16 |
|
if ($resource instanceof ResourceInterface) { |
|
348
|
14 |
|
if (!$resource->hasTransformer()) { |
|
349
|
|
|
// Try to resolve a transformer for a resource that does not have one assigned. |
|
350
|
9 |
|
$transformer = $this->resolveTransformerForResource($resource); |
|
351
|
9 |
|
$resource->setTransformer($transformer); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
// Load relations for any resource which implements the interface. |
|
355
|
14 |
|
$this->loadRelations($resource); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
// Build the output by recursively transforming each resource. |
|
359
|
16 |
|
$output = null; |
|
360
|
16 |
|
if ($resource instanceof Collection) { |
|
361
|
4 |
|
$output = $this->serializeCollection($resource, $includes); |
|
362
|
15 |
|
} elseif ($resource instanceof Item) { |
|
363
|
13 |
|
$output = $this->serializeItem($resource, $includes); |
|
364
|
3 |
|
} elseif (\is_array($resource)) { |
|
365
|
1 |
|
$output = $resource; |
|
366
|
2 |
|
} elseif (\is_object($resource) && method_exists($resource, 'toArray')) { |
|
367
|
1 |
|
$output = $resource->toArray(); |
|
368
|
|
|
} else { |
|
369
|
1 |
|
throw new UnhandledResourceType('Unable to serialize resource of type '.\gettype($resource)); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
15 |
|
return $output; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Fire the relation loader (if defined) for this resource. |
|
377
|
|
|
* |
|
378
|
|
|
* @param ResourceInterface $resource |
|
379
|
|
|
*/ |
|
380
|
14 |
|
protected function loadRelations(ResourceInterface $resource) |
|
381
|
|
|
{ |
|
382
|
14 |
|
if ($this->relationLoader !== null) { |
|
383
|
1 |
|
$this->relationLoader->load($resource); |
|
384
|
|
|
} |
|
385
|
14 |
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* @param Collection $collection |
|
389
|
|
|
* @param Includes $includes |
|
390
|
|
|
* |
|
391
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidSerializerException |
|
392
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
393
|
|
|
* @throws InvalidTransformerException |
|
394
|
|
|
* |
|
395
|
|
|
* @return array |
|
396
|
|
|
* @throws IncludeException |
|
397
|
|
|
*/ |
|
398
|
4 |
|
protected function serializeCollection(Collection $collection, Includes $includes): array |
|
399
|
|
|
{ |
|
400
|
|
|
// Get the globally set serializer (resource may override). |
|
401
|
4 |
|
$defaultSerializer = $this->getSerializer(); |
|
402
|
|
|
|
|
403
|
|
|
// Collection resources implement IteratorAggregate ... so that's nice. |
|
404
|
4 |
|
$items = []; |
|
405
|
4 |
|
foreach ($collection as $item) { |
|
406
|
|
|
// $item might be a Model or an array etc. |
|
407
|
4 |
|
$items[] = $this->transformItem($item, $collection->getTransformer(), $includes); |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
// The collection can have a custom serializer defined. |
|
411
|
4 |
|
$serializer = $collection->getSerializer() ?? $defaultSerializer; |
|
412
|
|
|
|
|
413
|
4 |
|
if ($serializer instanceof SerializerInterface) { |
|
414
|
|
|
// Serialize via object implementing SerializerInterface |
|
415
|
2 |
|
$output = $serializer->collection($collection->getResourceKey(), $items); |
|
416
|
2 |
|
if ($collection->hasPaginator()) { |
|
417
|
2 |
|
$output = array_merge($output, $serializer->paginator($collection->getPaginator())); |
|
418
|
|
|
} |
|
419
|
2 |
|
} elseif (\is_callable($serializer)) { |
|
420
|
|
|
// Serialize via a callable/closure |
|
421
|
1 |
|
$output = $serializer($collection->getResourceKey(), $items); |
|
422
|
|
|
} else { |
|
423
|
|
|
// Serialization disabled for this resource |
|
424
|
1 |
|
$output = $items; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
4 |
|
return $output; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Apply transformation to the item. |
|
432
|
|
|
* |
|
433
|
|
|
* @param mixed $item |
|
434
|
|
|
* @param mixed|TransformerInterface|callable|null $transformer |
|
435
|
|
|
* @param Includes $includes |
|
436
|
|
|
* |
|
437
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidSerializerException |
|
438
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
439
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
440
|
|
|
* |
|
441
|
|
|
* @return array |
|
442
|
|
|
* @throws IncludeException |
|
443
|
|
|
*/ |
|
444
|
14 |
|
protected function transformItem($item, $transformer, Includes $includes): array |
|
445
|
|
|
{ |
|
446
|
14 |
|
if ($transformer === null) { |
|
447
|
|
|
// No transformation can be applied. |
|
448
|
7 |
|
return (array) $item; |
|
449
|
|
|
} |
|
450
|
8 |
|
if (\is_callable($transformer)) { |
|
451
|
|
|
// Callable should simply return an array. |
|
452
|
3 |
|
return (array) $transformer($item); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
// Only these keys may be mapped |
|
456
|
7 |
|
$availableIncludeKeys = $transformer->getAvailableIncludes(); |
|
457
|
|
|
|
|
458
|
|
|
// Wanted includes is a either the explicit includes requested, or the defaults for the transformer. |
|
459
|
7 |
|
$wantIncludeKeys = $includes->baseKeys() ?: $transformer->getDefaultIncludes(); |
|
460
|
|
|
|
|
461
|
|
|
// Find the keys that are declared in the $includes of the transformer |
|
462
|
7 |
|
$mappedIncludeKeys = array_filter($wantIncludeKeys, function ($includeKey) use ($availableIncludeKeys) { |
|
463
|
6 |
|
return \in_array($includeKey, $availableIncludeKeys, true); |
|
464
|
7 |
|
}); |
|
465
|
|
|
|
|
466
|
|
|
// We can consider our props anything that has not been mapped. |
|
467
|
7 |
|
$filterProps = array_filter($wantIncludeKeys, function ($includeKey) use ($mappedIncludeKeys) { |
|
468
|
6 |
|
return !\in_array($includeKey, $mappedIncludeKeys, true); |
|
469
|
7 |
|
}); |
|
470
|
|
|
|
|
471
|
|
|
// Were any filter props explicitly provided? |
|
472
|
|
|
// If not, see if defaults were provided from the transformer. |
|
473
|
7 |
|
if (empty($filterProps)) { |
|
474
|
|
|
// No explicit props provided |
|
475
|
7 |
|
$defaultProps = $transformer->getDefaultProps(); |
|
476
|
7 |
|
if (!empty($defaultProps)) { |
|
477
|
1 |
|
$filterProps = $defaultProps; |
|
478
|
|
|
} |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
// Get the base data from the transformation |
|
482
|
7 |
|
$data = (array) $transformer->transform($item); |
|
|
|
|
|
|
483
|
|
|
|
|
484
|
|
|
// Filter the sparse field-set |
|
485
|
7 |
|
if (!empty($filterProps)) { |
|
486
|
1 |
|
$filteredData = array_filter($data, function ($key) use ($filterProps) { |
|
487
|
1 |
|
return \in_array($key, $filterProps, true); |
|
488
|
1 |
|
}, ARRAY_FILTER_USE_KEY); |
|
489
|
|
|
|
|
490
|
|
|
// We must always have some data after filtering |
|
491
|
|
|
// If our filtered data is empty, we should just ignore it |
|
492
|
1 |
|
if (!empty($filteredData)) { |
|
493
|
1 |
|
$data = $filteredData; |
|
494
|
|
|
} |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
// Add includes to the payload |
|
498
|
7 |
|
$includeMap = $transformer->getIncludeMap(); |
|
499
|
7 |
|
foreach ($mappedIncludeKeys as $includeKey) { |
|
500
|
5 |
|
$resource = $this->executeTransformerInclude($transformer, $includeKey, $includeMap[$includeKey], $item); |
|
501
|
|
|
|
|
502
|
5 |
|
if ($resource instanceof ResourceInterface) { |
|
503
|
|
|
// Resource object |
|
504
|
4 |
|
$data[$resource->getResourceKey() ?: $includeKey] = !$resource->getData() ? null : $this->serializeResource($resource, |
|
505
|
4 |
|
$includes->splice($includeKey)); |
|
506
|
|
|
} else { |
|
507
|
|
|
// Plain old array |
|
508
|
5 |
|
$data[$includeKey] = $this->serializeResource($resource, $includes->splice($includeKey)); |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
7 |
|
return $data; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Execute the transformer. |
|
517
|
|
|
* |
|
518
|
|
|
* @param mixed $transformer |
|
519
|
|
|
* @param string $includeKey |
|
520
|
|
|
* @param array $includeDefinition |
|
521
|
|
|
* @param mixed $item |
|
522
|
|
|
* |
|
523
|
|
|
* @return ResourceInterface |
|
524
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
525
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
526
|
|
|
* @throws IncludeException |
|
527
|
|
|
*/ |
|
528
|
5 |
|
protected function executeTransformerInclude($transformer, $includeKey, $includeDefinition, $item) |
|
529
|
|
|
{ |
|
530
|
|
|
// Transformer explicitly provided an include method |
|
531
|
5 |
|
$method = $includeDefinition['method']; |
|
532
|
5 |
|
if (method_exists($transformer, $method)) { |
|
533
|
3 |
|
return $transformer->$method($item); |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
// Otherwise try handle the include automatically |
|
537
|
2 |
|
return $this->autoWireInclude($includeKey, $includeDefinition, $item); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
/** |
|
541
|
|
|
* @param string $includeKey |
|
542
|
|
|
* @param array $includeDefinition |
|
543
|
|
|
* @param $item |
|
544
|
|
|
* |
|
545
|
|
|
* @return Collection|Item |
|
546
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
547
|
|
|
* @throws IncludeException |
|
548
|
|
|
*/ |
|
549
|
2 |
|
protected function autoWireInclude($includeKey, $includeDefinition, $item) |
|
550
|
|
|
{ |
|
551
|
|
|
// Get the included data |
|
552
|
2 |
|
$data = null; |
|
553
|
2 |
|
if (\is_array($item) || $item instanceof \ArrayAccess) { |
|
554
|
1 |
|
$data = $item[$includeKey] ?? null; |
|
555
|
1 |
|
} elseif (\is_object($item)) { |
|
556
|
1 |
|
$data = $item->$includeKey ?? null; |
|
557
|
|
|
} else { |
|
558
|
|
|
throw new IncludeException("Cannot auto-wire include for $includeKey: Cannot get include data"); |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
2 |
|
if (!empty($includeDefinition['resource_type']) && $includeDefinition['resource_type'] === 'collection') { |
|
562
|
1 |
|
return new Collection($data); |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
// Assume unless declared, that the resource is an item. |
|
566
|
2 |
|
return new Item($data); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* Applies the serializer to the Item resource. |
|
571
|
|
|
* |
|
572
|
|
|
* @param Item $item |
|
573
|
|
|
* @param Includes $includes |
|
574
|
|
|
* |
|
575
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
576
|
|
|
* @throws InvalidTransformerException |
|
577
|
|
|
* |
|
578
|
|
|
* @return array |
|
579
|
|
|
* @throws IncludeException |
|
580
|
|
|
*/ |
|
581
|
13 |
|
protected function serializeItem(Item $item, Includes $includes): array |
|
582
|
|
|
{ |
|
583
|
|
|
// Get the globally set serializer (resource may override) |
|
584
|
13 |
|
$defaultSerializer = $this->getSerializer(); |
|
585
|
|
|
|
|
586
|
|
|
// The collection can have a custom serializer defined |
|
587
|
13 |
|
$serializer = $item->getSerializer() ?? $defaultSerializer; |
|
588
|
13 |
|
$isSerializerInterface = $serializer instanceof SerializerInterface; |
|
589
|
|
|
|
|
590
|
|
|
// Transform the item data |
|
591
|
13 |
|
$itemData = $this->transformItem($item->getData(), $item->getTransformer(), $includes); |
|
592
|
|
|
|
|
593
|
|
|
// Serialize the item data |
|
594
|
13 |
|
if ($isSerializerInterface) { |
|
595
|
|
|
// Serialize via object implementing SerializerInterface |
|
596
|
11 |
|
$output = $serializer->item($item->getResourceKey(), $itemData); |
|
597
|
2 |
|
} elseif (\is_callable($serializer)) { |
|
598
|
|
|
// Serialize via a callable/closure |
|
599
|
1 |
|
$output = $serializer($item->getResourceKey(), $itemData); |
|
600
|
|
|
} else { |
|
601
|
|
|
// No serialization |
|
602
|
1 |
|
$output = $itemData; |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
13 |
|
return $output; |
|
606
|
|
|
} |
|
607
|
|
|
|
|
608
|
|
|
/** |
|
609
|
|
|
* Resolve the transformer to be used for a resource. |
|
610
|
|
|
* Returns an interface, callable or null when a transformer cannot be resolved. |
|
611
|
|
|
* |
|
612
|
|
|
* @param $resource |
|
613
|
|
|
* |
|
614
|
|
|
* @return TransformerInterface|mixed|null |
|
615
|
|
|
*/ |
|
616
|
9 |
|
protected function resolveTransformerForResource($resource) |
|
617
|
|
|
{ |
|
618
|
9 |
|
$transformer = null; |
|
619
|
|
|
|
|
620
|
9 |
|
if ($this->transformerResolver !== null) { |
|
621
|
2 |
|
$transformer = $this->transformerResolver->resolve($resource); |
|
622
|
|
|
} |
|
623
|
|
|
|
|
624
|
9 |
|
return $transformer; |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
/** |
|
628
|
|
|
* @return TransformerResolverInterface|null |
|
629
|
|
|
*/ |
|
630
|
1 |
|
public function getTransformerResolver() |
|
631
|
|
|
{ |
|
632
|
1 |
|
return $this->transformerResolver; |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
/** |
|
636
|
|
|
* Set the transformer resolve to user |
|
637
|
|
|
* |
|
638
|
|
|
* @param TransformerResolverInterface|null $transformerResolver |
|
639
|
|
|
* |
|
640
|
|
|
* @return $this |
|
641
|
|
|
*/ |
|
642
|
3 |
|
public function setTransformerResolver(TransformerResolverInterface $transformerResolver = null) |
|
643
|
|
|
{ |
|
644
|
3 |
|
$this->transformerResolver = $transformerResolver; |
|
645
|
|
|
|
|
646
|
3 |
|
return $this; |
|
647
|
|
|
} |
|
648
|
|
|
} |
|
649
|
|
|
|