-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pipeline.php
299 lines (258 loc) · 9.36 KB
/
Pipeline.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
namespace Rexlabs\Smokescreen\Transformer;
use Rexlabs\Smokescreen\Exception\IncludeException;
use Rexlabs\Smokescreen\Exception\UnhandledResourceType;
use Rexlabs\Smokescreen\Helpers\ArrayHelper;
use Rexlabs\Smokescreen\Relations\RelationLoaderInterface;
use Rexlabs\Smokescreen\Resource\Collection;
use Rexlabs\Smokescreen\Resource\Item;
use Rexlabs\Smokescreen\Resource\ResourceInterface;
use Rexlabs\Smokescreen\Serializer\SerializerInterface;
class Pipeline
{
/** @var SerializerInterface */
protected $serializer;
/** @var TransformerResolverInterface|null */
protected $transformerResolver;
/** @var RelationLoaderInterface|null */
protected $relationLoader;
public function __construct()
{
}
public function setTransformerResolver(TransformerResolverInterface $transformerResolver)
{
$this->transformerResolver = $transformerResolver;
}
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
public function setRelationLoader(RelationLoaderInterface $relationLoader)
{
$this->relationLoader = $relationLoader;
}
/**
* @param Scope $scope
*
* @throws IncludeException
*
* @return array
*/
public function transform(Scope $scope): array
{
$resource = $scope->resource();
if (!($resource instanceof ResourceInterface)) {
if (\is_array($resource)) {
return $resource;
}
if (\is_object($resource) && method_exists($resource, 'toArray')) {
return $resource->toArray();
}
throw new UnhandledResourceType('Unable to serialize resource of type '.\gettype($resource));
}
// Try to resolve a transformer for a resource that does not have one assigned.
if (!$resource->hasTransformer()) {
$transformer = $this->resolveTransformerForResource($resource);
$resource->setTransformer($transformer);
}
// Call the relationship loader for any relations
$this->loadRelations($resource, $scope->resolvedRelationshipKeys());
// Build the output by recursively transforming each resource.
$output = null;
if ($resource instanceof Collection) {
$output = $this->transformCollectionResource($scope);
} elseif ($resource instanceof Item) {
$output = $this->transformItemResource($scope);
}
return $output;
}
/**
* Resolve the transformer to be used for a resource.
* Returns an interface, callable or null when a transformer cannot be resolved.
*
* @param $resource
*
* @return TransformerInterface|mixed|null
*/
protected function resolveTransformerForResource($resource)
{
$transformer = null;
if ($this->transformerResolver !== null) {
$transformer = $this->transformerResolver->resolve($resource);
}
return $transformer;
}
/**
* Applies the serializer to the Item resource.
*
* @param Scope $scope
*
* @throws IncludeException
*
* @return array
*/
protected function transformItemResource(Scope $scope): array
{
// Get the globally set serializer (resource may override)
$defaultSerializer = $this->getSerializer();
// The collection can have a custom serializer defined
// TODO: Check resource type is item
$item = $scope->resource();
$serializer = $item->getSerializer() ?? $defaultSerializer;
$isSerializerInterface = $serializer instanceof SerializerInterface;
// Transform the item data
$itemData = $this->transformData($scope, $item->getData());
// Serialize the item data
if ($isSerializerInterface) {
// Serialize via object implementing SerializerInterface
$output = $serializer->item($item->getResourceKey(), $itemData);
} elseif (\is_callable($serializer)) {
// Serialize via a callable/closure
$output = $serializer($item->getResourceKey(), $itemData);
} else {
// No serialization
$output = $itemData;
}
return $output;
}
/**
* @param Scope $scope
*
* @throws IncludeException
*
* @return array
*/
protected function transformCollectionResource(Scope $scope): array
{
// Get the globally set serializer (resource may override).
$defaultSerializer = $this->getSerializer();
// Collection resources implement IteratorAggregate ... so that's nice.
// TODO: Check type?
$collection = $scope->resource();
$items = [];
foreach ($collection as $itemData) {
// $item might be a Model or an array etc.
$items[] = $this->transformData($scope, $itemData);
}
// The collection can have a custom serializer defined.
$serializer = $collection->getSerializer() ?? $defaultSerializer;
if ($serializer instanceof SerializerInterface) {
// Serialize via object implementing SerializerInterface
$output = $serializer->collection($collection->getResourceKey(), $items);
if ($collection->hasPaginator()) {
$output = array_merge($output, $serializer->paginator($collection->getPaginator()));
}
} elseif (\is_callable($serializer)) {
// Serialize via a callable/closure
$output = $serializer($collection->getResourceKey(), $items);
} else {
// Serialization disabled for this resource
$output = $items;
}
return $output;
}
/**
* Apply transformation to the item data.
*
* @param Scope $scope
* @param mixed $data
*
* @throws IncludeException
*
* @return array
*/
protected function transformData(Scope $scope, $data): array
{
// Get the base data from the transformation
$transformedData = $scope->transform($data);
// Add includes to the payload
$includeMap = $scope->includeMap();
foreach ($scope->resolvedIncludeKeys() as $includeKey) {
$resource = $this->executeTransformerInclude($scope, $includeKey, $includeMap[$includeKey], $data);
// Create a new scope
$newScope = new Scope($resource, $scope->includes()->splice($includeKey), $scope);
if ($resource instanceof ResourceInterface) {
// Resource object
ArrayHelper::mutate(
$transformedData,
$resource->getResourceKey() ?: $includeKey,
$resource->getData() ? $this->transform($newScope) : null
);
} else {
// Plain old array
ArrayHelper::mutate(
$transformedData,
$includeKey,
$this->transform($newScope)
);
}
}
return $transformedData;
}
/**
* Execute the transformer.
*
* @param Scope $scope
* @param string $includeKey
* @param array $includeDefinition
* @param mixed $data
*
* @throws IncludeException
*
* @return ResourceInterface
*/
protected function executeTransformerInclude(Scope $scope, $includeKey, $includeDefinition, $data)
{
// Transformer explicitly provided an include method
$transformer = $scope->transformer();
$method = $includeDefinition['method'];
if (method_exists($transformer, $method)) {
return $transformer->$method($data, $scope);
}
// Otherwise try handle the include automatically
return $this->autoWireInclude($includeKey, $includeDefinition, $data);
}
/**
* Fire the relation loader (if defined) for this resource.
*
* @param ResourceInterface $resource
* @param array $relationshipKeys
*/
protected function loadRelations(ResourceInterface $resource, array $relationshipKeys)
{
if ($this->relationLoader !== null && !empty($relationshipKeys)) {
$this->relationLoader->load($resource, $relationshipKeys);
}
}
/**
* @param string $includeKey
* @param array $includeDefinition
* @param $item
*
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException
* @throws \Rexlabs\Smokescreen\Exception\IncludeException
*
* @return Collection|Item|ResourceInterface
*/
protected function autoWireInclude($includeKey, $includeDefinition, $item)
{
// Get the included data
$data = null;
if (\is_array($item) || $item instanceof \ArrayAccess) {
$data = $item[$includeKey] ?? null;
} elseif (\is_object($item)) {
$data = $item->$includeKey ?? null;
} else {
throw new IncludeException("Cannot auto-wire include for $includeKey: Cannot get include data");
}
if (!empty($includeDefinition['resource_type']) && $includeDefinition['resource_type'] === 'collection') {
return new Collection($data);
}
// Assume unless declared, that the resource is an item.
return new Item($data);
}
protected function getSerializer()
{
return $this->serializer;
}
}