1 | <?php |
||
27 | class TransformBuilder |
||
28 | { |
||
29 | /** |
||
30 | * A factory class for making Fractal resources. |
||
31 | * |
||
32 | * @var \Flugg\Responder\Contracts\Resources\ResourceFactory |
||
33 | */ |
||
34 | protected $resourceFactory; |
||
35 | |||
36 | /** |
||
37 | * A factory for making transformed arrays. |
||
38 | * |
||
39 | * @var \Flugg\Responder\Contracts\TransformFactory |
||
40 | */ |
||
41 | private $transformFactory; |
||
42 | |||
43 | /** |
||
44 | * A factory used to build Fractal paginator adapters. |
||
45 | * |
||
46 | * @var \Flugg\Responder\Contracts\Pagination\PaginatorFactory |
||
47 | */ |
||
48 | protected $paginatorFactory; |
||
49 | |||
50 | /** |
||
51 | * The resource that's being built. |
||
52 | * |
||
53 | * @var \League\Fractal\Resource\ResourceInterface |
||
54 | */ |
||
55 | protected $resource; |
||
56 | |||
57 | /** |
||
58 | * A serializer for formatting data after transforming. |
||
59 | * |
||
60 | * @var \League\Fractal\Serializer\SerializerAbstract |
||
61 | */ |
||
62 | protected $serializer; |
||
63 | |||
64 | /** |
||
65 | * A list of included relations. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $with = []; |
||
70 | |||
71 | /** |
||
72 | * A list of excluded relations. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $without = []; |
||
77 | |||
78 | /** |
||
79 | * A list of sparse fieldsets. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $only = []; |
||
84 | |||
85 | /** |
||
86 | * Construct the builder class. |
||
87 | * |
||
88 | * @param \Flugg\Responder\Contracts\Resources\ResourceFactory $resourceFactory |
||
89 | * @param \Flugg\Responder\Contracts\TransformFactory $transformFactory |
||
90 | * @param \Flugg\Responder\Contracts\Pagination\PaginatorFactory $paginatorFactory |
||
91 | */ |
||
92 | 66 | public function __construct(ResourceFactory $resourceFactory, TransformFactory $transformFactory, PaginatorFactory $paginatorFactory) |
|
98 | |||
99 | /** |
||
100 | * Make a resource from the given data and transformer and set the resource key. |
||
101 | * |
||
102 | * @param mixed $data |
||
103 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
104 | * @param string|null $resourceKey |
||
105 | * @return $this |
||
106 | */ |
||
107 | 65 | public function resource($data = null, $transformer = null, string $resourceKey = null) |
|
119 | |||
120 | /** |
||
121 | * Manually set the cursor on the resource. |
||
122 | * |
||
123 | * @param \League\Fractal\Pagination\Cursor $cursor |
||
124 | * @return $this |
||
125 | */ |
||
126 | 3 | public function cursor(Cursor $cursor) |
|
134 | |||
135 | /** |
||
136 | * Manually set the paginator on the resource. |
||
137 | * |
||
138 | * @param \League\Fractal\Pagination\IlluminatePaginatorAdapter $paginator |
||
139 | * @return $this |
||
140 | */ |
||
141 | 4 | public function paginator(IlluminatePaginatorAdapter $paginator) |
|
149 | |||
150 | /** |
||
151 | * Add meta data appended to the response data. |
||
152 | * |
||
153 | * @param array $data |
||
154 | * @return $this |
||
155 | */ |
||
156 | 2 | public function meta(array $data) |
|
162 | |||
163 | /** |
||
164 | * Include relations to the transform. |
||
165 | * |
||
166 | * @param string[]|string $relations |
||
167 | * @return $this |
||
168 | */ |
||
169 | 52 | public function with($relations) |
|
184 | |||
185 | /** |
||
186 | * Exclude relations from the transform. |
||
187 | * |
||
188 | * @param string[]|string $relations |
||
189 | * @return $this |
||
190 | */ |
||
191 | 3 | public function without($relations) |
|
197 | |||
198 | /** |
||
199 | * Filter fields to output using sparse fieldsets. |
||
200 | * |
||
201 | * @param string[]|string $fields |
||
202 | * @return $this |
||
203 | */ |
||
204 | 48 | public function only($fields) |
|
210 | |||
211 | /** |
||
212 | * Set the serializer. |
||
213 | * |
||
214 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
215 | * @return $this |
||
216 | * @throws \Flugg\Responder\Exceptions\InvalidSuccessSerializerException |
||
217 | */ |
||
218 | 66 | public function serializer($serializer) |
|
232 | |||
233 | /** |
||
234 | * Transform and serialize the data and return the transformed array. |
||
235 | * |
||
236 | * @return array|null |
||
237 | */ |
||
238 | 59 | public function transform() |
|
248 | |||
249 | /** |
||
250 | * Prepare requested relations for the transformation. |
||
251 | * |
||
252 | * @param mixed $data |
||
253 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
254 | * @return void |
||
255 | */ |
||
256 | 59 | protected function prepareRelations($data, $transformer) |
|
271 | |||
272 | /** |
||
273 | * Add query constraints defined as "load" methods in the transformers to the list of |
||
274 | * requested relations. |
||
275 | * |
||
276 | * @param array $requested |
||
277 | * @param array $whitelisted |
||
278 | * @return array |
||
279 | */ |
||
280 | protected function addMissingQueryConstraints(array $requested, array $whitelisted): array |
||
288 | |||
289 | /** |
||
290 | * Filter out relations that are not whitelisted in the transformer or relations that |
||
291 | * have been explicitly excluded using the [without] method. |
||
292 | * |
||
293 | * @param array $relations |
||
294 | * @param array $whitelisted |
||
295 | * @return array |
||
296 | */ |
||
297 | protected function removeForbiddenRelations(array $relations, array $whitelisted): array |
||
305 | |||
306 | /** |
||
307 | * Strip parameter suffix from the relation string by only taking what is in front of |
||
308 | * the colon. |
||
309 | * |
||
310 | * @param string $relation |
||
311 | * @return string |
||
312 | */ |
||
313 | 16 | protected function stripParametersFromRelation(string $relation): string |
|
317 | |||
318 | /** |
||
319 | * Eager load all requested relations except the ones defined as an "include" method |
||
320 | * in the transformers. We also strip away any parameters from the relation name |
||
321 | * and normalize relations by swapping "null" constraints to empty closures. |
||
322 | * |
||
323 | * @param mixed $data |
||
324 | * @param array $requested |
||
325 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
326 | * @return void |
||
327 | */ |
||
328 | protected function eagerLoadRelations($data, array $requested, $transformer) |
||
342 | } |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.