1 | <?php |
||
25 | class TransformBuilder |
||
26 | { |
||
27 | /** |
||
28 | * A factory class for making Fractal resources. |
||
29 | * |
||
30 | * @var \Flugg\Responder\Contracts\Resources\ResourceFactory |
||
31 | */ |
||
32 | protected $resourceFactory; |
||
33 | |||
34 | /** |
||
35 | * A factory for making transformed arrays. |
||
36 | * |
||
37 | * @var \Flugg\Responder\Contracts\TransformFactory |
||
38 | */ |
||
39 | private $transformFactory; |
||
40 | |||
41 | /** |
||
42 | * A factory used to build Fractal paginator adapters. |
||
43 | * |
||
44 | * @var \Flugg\Responder\Contracts\Pagination\PaginatorFactory |
||
45 | */ |
||
46 | protected $paginatorFactory; |
||
47 | |||
48 | /** |
||
49 | * The resource that's being built. |
||
50 | * |
||
51 | * @var \League\Fractal\Resource\ResourceInterface |
||
52 | */ |
||
53 | protected $resource; |
||
54 | |||
55 | /** |
||
56 | * A serializer for formatting data after transforming. |
||
57 | * |
||
58 | * @var \League\Fractal\Serializer\SerializerAbstract |
||
59 | */ |
||
60 | protected $serializer; |
||
61 | |||
62 | /** |
||
63 | * A list of included relations. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $with = []; |
||
68 | |||
69 | /** |
||
70 | * A list of excluded relations. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $without = []; |
||
75 | |||
76 | /** |
||
77 | * A list of sparse fieldsets. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $only = []; |
||
82 | |||
83 | /** |
||
84 | * Construct the builder class. |
||
85 | * |
||
86 | * @param \Flugg\Responder\Contracts\Resources\ResourceFactory $resourceFactory |
||
87 | * @param \Flugg\Responder\Contracts\TransformFactory $transformFactory |
||
88 | * @param \Flugg\Responder\Contracts\Pagination\PaginatorFactory $paginatorFactory |
||
89 | */ |
||
90 | public function __construct(ResourceFactory $resourceFactory, TransformFactory $transformFactory, PaginatorFactory $paginatorFactory) |
||
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 self |
||
105 | */ |
||
106 | public function resource($data = null, $transformer = null, string $resourceKey = null): TransformBuilder |
||
118 | |||
119 | /** |
||
120 | * Manually set the cursor on the resource. |
||
121 | * |
||
122 | * @param \League\Fractal\Pagination\Cursor $cursor |
||
123 | * @return self |
||
124 | */ |
||
125 | public function cursor(Cursor $cursor): TransformBuilder |
||
131 | |||
132 | /** |
||
133 | * Manually set the paginator on the resource. |
||
134 | * |
||
135 | * @param \League\Fractal\Pagination\IlluminatePaginatorAdapter $paginator |
||
136 | * @return self |
||
137 | */ |
||
138 | public function paginator(IlluminatePaginatorAdapter $paginator): TransformBuilder |
||
144 | |||
145 | /** |
||
146 | * Add meta data appended to the response data. |
||
147 | * |
||
148 | * @param array $meta |
||
149 | * @return self |
||
150 | */ |
||
151 | public function meta(array $meta): TransformBuilder |
||
157 | |||
158 | /** |
||
159 | * Include relations to the transform. |
||
160 | * |
||
161 | * @param string[]|string $relations |
||
162 | * @return self |
||
163 | */ |
||
164 | public function with($relations): TransformBuilder |
||
170 | |||
171 | /** |
||
172 | * Exclude relations from the transform. |
||
173 | * |
||
174 | * @param string[]|string $relations |
||
175 | * @return self |
||
176 | */ |
||
177 | public function without($relations): TransformBuilder |
||
183 | |||
184 | /** |
||
185 | * Filter fields to output using sparse fieldsets. |
||
186 | * |
||
187 | * @param string[]|string $fields |
||
188 | * @return self |
||
189 | */ |
||
190 | public function only($fields): TransformBuilder |
||
196 | |||
197 | /** |
||
198 | * Set the serializer. |
||
199 | * |
||
200 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
201 | * @return self |
||
202 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
||
203 | */ |
||
204 | public function serializer($serializer): TransformBuilder |
||
218 | |||
219 | /** |
||
220 | * Transform and serialize the data and return the transformed array. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function transform(): array |
||
234 | |||
235 | /** |
||
236 | * Prepare requested relations for the transformation. |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | protected function prepareRelations() |
||
247 | |||
248 | /** |
||
249 | * Set default includes extracted from the transformer. |
||
250 | * |
||
251 | * @param \Flugg\Responder\Transformers\Transformer|callable $transformer |
||
252 | * @return void |
||
253 | */ |
||
254 | protected function setDefaultIncludes($transformer) |
||
260 | |||
261 | /** |
||
262 | * Eager load relations on the given data, if it's an Eloquent model or collection. |
||
263 | * |
||
264 | * @param mixed $data |
||
265 | * @return void |
||
266 | */ |
||
267 | protected function eagerLoadIfApplicable($data) |
||
273 | |||
274 | /** |
||
275 | * Remove eager load constraint functions from the given array. |
||
276 | * |
||
277 | * @param array $relations |
||
278 | * @return void |
||
279 | */ |
||
280 | protected function trimEagerLoadFunctions(array $relations) |
||
286 | } |
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: