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 | 18 | 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 | 17 | 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 | 2 | 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 | 2 | 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 | 1 | public function meta(array $data) |
|
162 | |||
163 | /** |
||
164 | * Include relations to the transform. |
||
165 | * |
||
166 | * @param string[]|string $relations |
||
167 | * @return $this |
||
168 | */ |
||
169 | 4 | public function with($relations) |
|
175 | |||
176 | /** |
||
177 | * Exclude relations from the transform. |
||
178 | * |
||
179 | * @param string[]|string $relations |
||
180 | * @return $this |
||
181 | */ |
||
182 | 2 | public function without($relations) |
|
188 | |||
189 | /** |
||
190 | * Filter fields to output using sparse fieldsets. |
||
191 | * |
||
192 | * @param string[]|string $fields |
||
193 | * @return $this |
||
194 | */ |
||
195 | 2 | public function only($fields) |
|
201 | |||
202 | /** |
||
203 | * Set the serializer. |
||
204 | * |
||
205 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
206 | * @return $this |
||
207 | * @throws \Flugg\Responder\Exceptions\InvalidSuccessSerializerException |
||
208 | */ |
||
209 | 18 | public function serializer($serializer) |
|
223 | |||
224 | /** |
||
225 | * Transform and serialize the data and return the transformed array. |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | 11 | public function transform(): array |
|
239 | |||
240 | /** |
||
241 | * Prepare requested relations for the transformation. |
||
242 | * |
||
243 | * @param mixed $data |
||
244 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
245 | * @return void |
||
246 | */ |
||
247 | 11 | protected function prepareRelations($data, $transformer) |
|
259 | |||
260 | /** |
||
261 | * Include default relationships and add eager load constraints from transformer. |
||
262 | * |
||
263 | * @param \Flugg\Responder\Transformers\Transformer $transformer |
||
264 | * @return void |
||
265 | */ |
||
266 | protected function includeTransformerRelations(BaseTransformer $transformer) |
||
277 | |||
278 | /** |
||
279 | * Remove eager load constraint functions from the given relations. |
||
280 | * |
||
281 | * @param array $relations |
||
282 | * @return array |
||
283 | */ |
||
284 | protected function stripEagerLoadConstraints(array $relations): array |
||
290 | |||
291 | /** |
||
292 | * Remove parameters from relations that must be loaded. |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 2 | protected function relationsWithoutParameters(): array |
|
313 | } |
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.