1 | <?php |
||
26 | class TransformBuilder |
||
27 | { |
||
28 | /** |
||
29 | * A factory class for making Fractal resources. |
||
30 | * |
||
31 | * @var \Flugg\Responder\Contracts\Resources\ResourceFactory |
||
32 | */ |
||
33 | protected $resourceFactory; |
||
34 | |||
35 | /** |
||
36 | * A factory for making transformed arrays. |
||
37 | * |
||
38 | * @var \Flugg\Responder\Contracts\TransformFactory |
||
39 | */ |
||
40 | private $transformFactory; |
||
41 | |||
42 | /** |
||
43 | * A factory used to build Fractal paginator adapters. |
||
44 | * |
||
45 | * @var \Flugg\Responder\Contracts\Pagination\PaginatorFactory |
||
46 | */ |
||
47 | protected $paginatorFactory; |
||
48 | |||
49 | /** |
||
50 | * The resource that's being built. |
||
51 | * |
||
52 | * @var \League\Fractal\Resource\ResourceInterface |
||
53 | */ |
||
54 | protected $resource; |
||
55 | |||
56 | /** |
||
57 | * A serializer for formatting data after transforming. |
||
58 | * |
||
59 | * @var \League\Fractal\Serializer\SerializerAbstract |
||
60 | */ |
||
61 | protected $serializer; |
||
62 | |||
63 | /** |
||
64 | * A list of included relations. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $with = []; |
||
69 | |||
70 | /** |
||
71 | * A list of excluded relations. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $without = []; |
||
76 | |||
77 | /** |
||
78 | * A list of sparse fieldsets. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $only = []; |
||
83 | |||
84 | /** |
||
85 | * Construct the builder class. |
||
86 | * |
||
87 | * @param \Flugg\Responder\Contracts\Resources\ResourceFactory $resourceFactory |
||
88 | * @param \Flugg\Responder\Contracts\TransformFactory $transformFactory |
||
89 | * @param \Flugg\Responder\Contracts\Pagination\PaginatorFactory $paginatorFactory |
||
90 | */ |
||
91 | 17 | public function __construct(ResourceFactory $resourceFactory, TransformFactory $transformFactory, PaginatorFactory $paginatorFactory) |
|
92 | { |
||
93 | 17 | $this->resourceFactory = $resourceFactory; |
|
94 | 17 | $this->transformFactory = $transformFactory; |
|
95 | 17 | $this->paginatorFactory = $paginatorFactory; |
|
96 | 17 | } |
|
97 | |||
98 | /** |
||
99 | * Make a resource from the given data and transformer and set the resource key. |
||
100 | * |
||
101 | * @param mixed $data |
||
102 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
103 | * @param string|null $resourceKey |
||
104 | * @return $this |
||
105 | */ |
||
106 | 16 | public function resource($data = null, $transformer = null, string $resourceKey = null) |
|
118 | |||
119 | /** |
||
120 | * Manually set the cursor on the resource. |
||
121 | * |
||
122 | * @param \League\Fractal\Pagination\Cursor $cursor |
||
123 | * @return $this |
||
124 | */ |
||
125 | 2 | public function cursor(Cursor $cursor) |
|
131 | |||
132 | /** |
||
133 | * Manually set the paginator on the resource. |
||
134 | * |
||
135 | * @param \League\Fractal\Pagination\IlluminatePaginatorAdapter $paginator |
||
136 | * @return $this |
||
137 | */ |
||
138 | 2 | public function paginator(IlluminatePaginatorAdapter $paginator) |
|
144 | |||
145 | /** |
||
146 | * Add meta data appended to the response data. |
||
147 | * |
||
148 | * @param array $data |
||
149 | * @return $this |
||
150 | */ |
||
151 | 1 | public function meta(array $data) |
|
157 | |||
158 | /** |
||
159 | * Include relations to the transform. |
||
160 | * |
||
161 | * @param string[]|string $relations |
||
162 | * @return $this |
||
163 | */ |
||
164 | 3 | public function with($relations) |
|
170 | |||
171 | /** |
||
172 | * Exclude relations from the transform. |
||
173 | * |
||
174 | * @param string[]|string $relations |
||
175 | * @return $this |
||
176 | */ |
||
177 | 2 | public function without($relations) |
|
183 | |||
184 | /** |
||
185 | * Filter fields to output using sparse fieldsets. |
||
186 | * |
||
187 | * @param string[]|string $fields |
||
188 | * @return $this |
||
189 | */ |
||
190 | 2 | public function only($fields) |
|
196 | |||
197 | /** |
||
198 | * Set the serializer. |
||
199 | * |
||
200 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
201 | * @return $this |
||
202 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
||
203 | */ |
||
204 | 17 | public function serializer($serializer) |
|
218 | |||
219 | /** |
||
220 | * Transform and serialize the data and return the transformed array. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | 10 | public function transform(): array |
|
234 | |||
235 | /** |
||
236 | * Prepare requested relations for the transformation. |
||
237 | * |
||
238 | * @param mixed $data |
||
239 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
240 | * @return void |
||
241 | */ |
||
242 | 10 | protected function prepareRelations($data, $transformer) |
|
254 | |||
255 | /** |
||
256 | * Remove eager load constraint functions from the given relations. |
||
257 | * |
||
258 | * @param array $relations |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function stripEagerLoadConstraints(array $relations): array |
||
267 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: