|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Rexlabs\Smokescreen\Scope; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Rexlabs\Smokescreen\Exception\InvalidTransformerException; |
|
8
|
|
|
use Rexlabs\Smokescreen\Helpers\StrHelper; |
|
9
|
|
|
use Rexlabs\Smokescreen\Relations\RelationLoaderInterface; |
|
10
|
|
|
use Rexlabs\Smokescreen\Resource\Item; |
|
11
|
|
|
use Rexlabs\Smokescreen\Resource\ResourceInterface; |
|
12
|
|
|
use Rexlabs\Smokescreen\Serializer\SerializerInterface; |
|
13
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Compiler |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Scope */ |
|
18
|
|
|
protected $scope; |
|
19
|
|
|
|
|
20
|
|
|
/** @var SerializerInterface */ |
|
21
|
|
|
protected $serializer; |
|
22
|
|
|
|
|
23
|
|
|
/** @var RelationLoaderInterface|null */ |
|
24
|
|
|
protected $loader; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Scope $scope, SerializerInterface $serializer, RelationLoaderInterface $loader = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->scope = $scope; |
|
29
|
|
|
$this->serializer = $serializer; |
|
30
|
|
|
$this->loader = $loader; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function compile() |
|
34
|
|
|
{ |
|
35
|
|
|
$output = []; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Scope $scope |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function serialize(Scope $scope): array |
|
45
|
|
|
{ |
|
46
|
|
|
// Relationship loading |
|
47
|
|
|
$this->loadRelations($scope->getResource()); |
|
48
|
|
|
|
|
49
|
|
|
// Build the output by recursively transforming each resource |
|
50
|
|
|
return $scope->isCollection() ? |
|
51
|
|
|
$this->serializeCollection($scope) : $this->serializeItem($scope); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function loadRelations(ResourceInterface $resource) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->loader !== null) { |
|
57
|
|
|
$this->loader->load($resource); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function serializeCollection(Scope $scope) |
|
62
|
|
|
{ |
|
63
|
|
|
$items = array_map(function($data) use ($scope) { |
|
64
|
|
|
|
|
65
|
|
|
$scope = new Scope( |
|
|
|
|
|
|
66
|
|
|
new Item( |
|
67
|
|
|
$data, |
|
68
|
|
|
$scope->getResource()->getTransformer(), |
|
69
|
|
|
$scope->getResource()->getResourceKey() |
|
70
|
|
|
), |
|
71
|
|
|
$scope->getIncludes() |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
return $this->transformItem($item); |
|
|
|
|
|
|
75
|
|
|
}, $scope->getResource()->toArray()); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
// Serialize all the items |
|
78
|
|
|
$output = $this->serializer->collection( |
|
79
|
|
|
$collection->getResourceKey(), |
|
|
|
|
|
|
80
|
|
|
$items |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
// Merge in any pagination |
|
84
|
|
|
if ($collection->hasPaginator()) { |
|
85
|
|
|
$output = array_merge($output, $this->serializer->paginator($collection->getPaginator())); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $output; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Item $item |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function transformItem(Item $item): array |
|
97
|
|
|
{ |
|
98
|
|
|
$transformer = $item->getTransformer(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return []; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function callTransformerIncludeMethodFor($transformer, $includeKey, $item) |
|
104
|
|
|
{ |
|
105
|
|
|
return $transformer->{'include' . StrHelper::studlyCase($includeKey)}($item); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function serializeItem(Scope $scope) |
|
109
|
|
|
{ |
|
110
|
|
|
$resource = $scope->getResource(); |
|
111
|
|
|
|
|
112
|
|
|
return $this->serializer->item( |
|
113
|
|
|
$resource->getResourceKey(), |
|
114
|
|
|
$this->transformItem($resource->getData(), $resource->getTransformer()) |
|
|
|
|
|
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |