/** * Transform a response with a transformer. * * @param mixed $response * @param object $transformer * @param \Dingo\Api\Transformer\Binding $binding * @param \Dingo\Api\Http\Request $request * * @return array */ public function transform($response, $transformer, Binding $binding, Request $request) { $this->parseFractalIncludes($request); $resource = $this->createResource($response, $transformer, $binding->getParameters()); // If the response is a paginator then we'll create a new paginator // adapter for Laravel and set the paginator instance on our // collection resource. if ($response instanceof IlluminatePaginator) { $paginator = $this->createPaginatorAdapter($response); $resource->setPaginator($paginator); } if ($this->shouldEagerLoad($response)) { $eagerLoads = $this->mergeEagerLoads($transformer, $this->fractal->getRequestedIncludes()); $response->load($eagerLoads); } foreach ($binding->getMeta() as $key => $value) { $resource->setMetaValue($key, $value); } $binding->fireCallback($resource, $this->fractal); $scopeIdentifier = null; if (array_key_exists('scopeIdentifier', $binding->getParameters())) { $scopeIdentifier = $binding->getParameters()['scopeIdentifier']; } return $this->fractal->createData($resource, $scopeIdentifier)->toArray(); }
public function testRecursionLimiting() { $manager = new Manager(); // Should limit to 10 by default $manager->parseIncludes('a.b.c.d.e.f.g.h.i.j.NEVER'); $this->assertEquals(array('a', 'a.b', 'a.b.c', 'a.b.c.d', 'a.b.c.d.e', 'a.b.c.d.e.f', 'a.b.c.d.e.f.g', 'a.b.c.d.e.f.g.h', 'a.b.c.d.e.f.g.h.i', 'a.b.c.d.e.f.g.h.i.j'), $manager->getRequestedIncludes()); // Try setting to 3 and see what happens $manager->setRecursionLimit(3); $manager->parseIncludes('a.b.c.NEVER'); $this->assertEquals(array('a', 'a.b', 'a.b.c'), $manager->getRequestedIncludes()); }
/** * Get the Fractal resource instance. * * @return \League\Fractal\Resource\ResourceInterface */ public function getResource() : ResourceInterface { $this->manager->parseIncludes($this->relations); $transformer = $this->resource->getTransformer(); if ($transformer instanceof Transformer && $transformer->allRelationsAllowed()) { $this->resource->setTransformer($transformer->setRelations($this->manager->getRequestedIncludes())); } return $this->resource->setMeta($this->meta); }
/** * Is Requested. * * Check if - in relation to the current scope - this specific segment is allowed. * That means, if a.b.c is requested and the current scope is a.b, then c is allowed. If the current * scope is a then c is not allowed, even if it is there and potentially transformable. * * @internal * * @param string $checkScopeSegment * * @return bool Returns the new number of elements in the array. */ public function isRequested($checkScopeSegment) { if ($this->parentScopes) { $scopeArray = array_slice($this->parentScopes, 1); array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment); } else { $scopeArray = [$checkScopeSegment]; } $scopeString = implode('.', (array) $scopeArray); return in_array($scopeString, $this->manager->getRequestedIncludes()); }
/** * Get a list of objects * * @param Request $request Request object * @return ResponseInterface Response for request */ public function get(Request $request, Fractal $fractal, FractalCollection $collection) { $this->beforeRequest($request, func_get_args()); $transformer = $this->getTransformer(); $includes = $request->input('include', ''); $filter = $request->input('filter', false); $items = $this->getRepository(); if ($filter && is_string($filter)) { $filter = json_decode($filter, true); } $queryFilter = []; if (!empty($filter)) { $queryFilter = new QueryFilter($filter, $items->model(), $this->getTransformer()); } $fractal->parseIncludes($includes); $resource = $collection->setTransformer($transformer)->setResourceKey($this->getResourceKey()); $paginator = $items->paginate($queryFilter, $transformer->getSafeEagerLoad($fractal->getRequestedIncludes()), app('context')); $itemCollection = $paginator->getCollection(); $resource->setData($itemCollection); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); return $this->respondWithCollection($fractal->createData($resource)); }