1 | <?php |
||
28 | class TransformBuilder |
||
29 | { |
||
30 | /** |
||
31 | * A factory class for making Fractal resources. |
||
32 | * |
||
33 | * @var \Flugg\Responder\Contracts\Resources\ResourceFactory |
||
34 | */ |
||
35 | protected $resourceFactory; |
||
36 | |||
37 | /** |
||
38 | * A factory for making transformed arrays. |
||
39 | * |
||
40 | * @var \Flugg\Responder\Contracts\TransformFactory |
||
41 | */ |
||
42 | private $transformFactory; |
||
43 | |||
44 | /** |
||
45 | * A factory used to build Fractal paginator adapters. |
||
46 | * |
||
47 | * @var \Flugg\Responder\Contracts\Pagination\PaginatorFactory |
||
48 | */ |
||
49 | protected $paginatorFactory; |
||
50 | |||
51 | /** |
||
52 | * The resource that's being built. |
||
53 | * |
||
54 | * @var \League\Fractal\Resource\ResourceInterface |
||
55 | */ |
||
56 | protected $resource; |
||
57 | |||
58 | /** |
||
59 | * A serializer for formatting data after transforming. |
||
60 | * |
||
61 | * @var \League\Fractal\Serializer\SerializerAbstract |
||
62 | */ |
||
63 | protected $serializer; |
||
64 | |||
65 | /** |
||
66 | * A list of included relations. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $with = []; |
||
71 | |||
72 | /** |
||
73 | * A list of excluded relations. |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $without = []; |
||
78 | |||
79 | /** |
||
80 | * A list of sparse fieldsets. |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $only = []; |
||
85 | |||
86 | /** |
||
87 | * Construct the builder class. |
||
88 | * |
||
89 | * @param \Flugg\Responder\Contracts\Resources\ResourceFactory $resourceFactory |
||
90 | * @param \Flugg\Responder\Contracts\TransformFactory $transformFactory |
||
91 | * @param \Flugg\Responder\Contracts\Pagination\PaginatorFactory $paginatorFactory |
||
92 | */ |
||
93 | 75 | public function __construct(ResourceFactory $resourceFactory, TransformFactory $transformFactory, PaginatorFactory $paginatorFactory) |
|
99 | |||
100 | /** |
||
101 | * Make a resource from the given data and transformer and set the resource key. |
||
102 | * |
||
103 | * @param mixed $data |
||
104 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
105 | * @param string|null $resourceKey |
||
106 | * @return $this |
||
107 | */ |
||
108 | 74 | public function resource($data = null, $transformer = null, string $resourceKey = null) |
|
109 | { |
||
110 | 74 | $this->resource = $this->resourceFactory->make($data, $transformer, $resourceKey); |
|
111 | |||
112 | 74 | if ($data instanceof CursorPaginator) { |
|
113 | 1 | $this->cursor($this->paginatorFactory->makeCursor($data)); |
|
114 | 73 | } elseif ($data instanceof LengthAwarePaginator) { |
|
115 | 2 | $this->paginator($this->paginatorFactory->make($data)); |
|
|
|||
116 | } |
||
117 | |||
118 | 74 | return $this; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Manually set the cursor on the resource. |
||
123 | * |
||
124 | * @param \League\Fractal\Pagination\Cursor $cursor |
||
125 | * @return $this |
||
126 | */ |
||
127 | 3 | public function cursor(Cursor $cursor) |
|
135 | |||
136 | /** |
||
137 | * Manually set the paginator on the resource. |
||
138 | * |
||
139 | * @param \League\Fractal\Pagination\IlluminatePaginatorAdapter $paginator |
||
140 | * @return $this |
||
141 | */ |
||
142 | 4 | public function paginator(IlluminatePaginatorAdapter $paginator) |
|
150 | |||
151 | /** |
||
152 | * Add meta data appended to the response data. |
||
153 | * |
||
154 | * @param array $data |
||
155 | * @return $this |
||
156 | */ |
||
157 | 2 | public function meta(array $data) |
|
163 | |||
164 | /** |
||
165 | * Include relations to the transform. |
||
166 | * |
||
167 | * @param string[]|string $relations |
||
168 | * @return $this |
||
169 | */ |
||
170 | 61 | public function with($relations) |
|
185 | |||
186 | /** |
||
187 | * Exclude relations from the transform. |
||
188 | * |
||
189 | * @param string[]|string $relations |
||
190 | * @return $this |
||
191 | */ |
||
192 | 3 | public function without($relations) |
|
198 | |||
199 | /** |
||
200 | * Filter fields to output using sparse fieldsets. |
||
201 | * |
||
202 | * @param string[]|string $fields |
||
203 | * @return $this |
||
204 | */ |
||
205 | 57 | public function only($fields) |
|
211 | |||
212 | /** |
||
213 | * Set the serializer. |
||
214 | * |
||
215 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
||
216 | * @return $this |
||
217 | * @throws \Flugg\Responder\Exceptions\InvalidSuccessSerializerException |
||
218 | */ |
||
219 | 75 | public function serializer($serializer) |
|
233 | |||
234 | /** |
||
235 | * Transform and serialize the data and return the transformed array. |
||
236 | * |
||
237 | * @return array|null |
||
238 | */ |
||
239 | 68 | public function transform() |
|
249 | |||
250 | /** |
||
251 | * Prepare requested relations for the transformation. |
||
252 | * |
||
253 | * @param mixed $data |
||
254 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
255 | * @return void |
||
256 | */ |
||
257 | 68 | protected function prepareRelations($data, $transformer) |
|
271 | |||
272 | /** |
||
273 | * Filter out relations that have been explicitly excluded using the [without] method. |
||
274 | * |
||
275 | * @param array $relations |
||
276 | * @return array |
||
277 | */ |
||
278 | 46 | protected function removeExcludedRelations(array $relations): array |
|
284 | |||
285 | /** |
||
286 | * Strip parameter suffix from the relation string by only taking what is in front of |
||
287 | * the colon. |
||
288 | * |
||
289 | * @param string $relation |
||
290 | * @return string |
||
291 | */ |
||
292 | 24 | protected function stripParametersFromRelation(string $relation): string |
|
296 | |||
297 | /** |
||
298 | * Eager load all requested relations except the ones defined as an "include" method |
||
299 | * in the transformers. We also strip away any parameters from the relation name |
||
300 | * and normalize relations by swapping "null" constraints to empty closures. |
||
301 | * |
||
302 | * @param mixed $data |
||
303 | * @param array $requested |
||
304 | * @param \Flugg\Responder\Transformers\Transformer|callable|string|null $transformer |
||
305 | * @return void |
||
306 | */ |
||
307 | 41 | protected function eagerLoadRelations($data, array $requested, $transformer) |
|
325 | } |
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.