/** * Determine if a transformer has any available includes. * * @author Morten Rugaard <*****@*****.**> * * @param \Nodes\Api\Transformer\TransformerAbstract|\League\Fractal\TransformerAbstract|callable $transformer * @return bool */ protected function transformerHasIncludes($transformer) { if (!$transformer instanceof NodesTransformerAbstract && !$transformer instanceof FractalTransformerAbstract) { return false; } $defaultIncludes = $transformer->getDefaultIncludes(); $availableIncludes = $transformer->getAvailableIncludes(); return !empty($defaultIncludes) || !empty($availableIncludes); }
/** * Fire the main transformer. * * @internal * * @param TransformerAbstract|callable $transformer * @param mixed $data * * @return array * @throws EntityNotFoundException */ protected function fireTransformer($transformer, $data) { $this->transformer = $transformer; $includedData = []; $transformedData = []; $dataTransformer = []; if ($this->getManager()->getSerializer() instanceof DataHydraSerializer && !empty($data)) { $transformedData['@id'] = $this->getGenerateRoute($data); } if ($this->getManager()->getSerializer() instanceof DataHydraSerializer && !empty($this->getEntityName())) { $transformedData['@type'] = $this->getEntityName(); } try { $dataTransformer = is_callable($transformer) ? call_user_func($transformer, $data) : $transformer->transform($data); } catch (EntityNotFoundException $e) { if ($this->resource instanceof Item && !$this->parent instanceof Scope) { throw new EntityNotFoundException(); } } $transformedData = array_merge($transformedData, $dataTransformer); if ($this->getManager()->getGroupsContextChainer() instanceof GroupsContextChainer) { $transformedData = $this->getManager()->getGroupsContextChainer()->serialize($this->transformer->getCurrentResourceKey(), $transformedData, $this->getDunglasResource()->getVersion()); } if ($this->transformerHasIncludes($transformer)) { $includedData = $this->fireIncludedTransformers($transformer, $data); // If the serializer does not want the includes to be side-loaded then // the included data must be merged with the transformed data. if (!$this->manager->getSerializer()->sideloadIncludes() && is_array($includedData)) { $transformedData = array_merge($transformedData, $includedData); } } return array($transformedData, $includedData); }
/** * Transforms the given collection. * * @param mixed $collection * @param string|Transformer $transformer * @param null|string $resourceKey * @return Collection */ public function collection($collection, $transformer, $resourceKey = null) { if (!$transformer instanceof TransformerAbstract) { $transformer = $this->getTransformer($transformer); } $resourceKey = $transformer->resourceKey(); return parent::collection($collection, $transformer, $resourceKey); }
/** * Fire the included transformers. * * @internal * * @param \League\Fractal\TransformerAbstract $transformer * @param mixed $data * * @return array */ protected function fireIncludedTransformers($transformer, $data) { $this->availableIncludes = $transformer->getAvailableIncludes(); return $transformer->processIncludedResources($this, $data) ?: []; }
/** * Get includes as their array keys for eager loading. * * @param \League\Fractal\TransformerAbstract $transformer * @param string|array $requestedIncludes * * @return array */ protected function mergeEagerLoads($transformer, $requestedIncludes) { $availableIncludes = array_intersect($transformer->getAvailableIncludes(), (array) $requestedIncludes); $includes = array_merge($availableIncludes, $transformer->getDefaultIncludes()); $eagerLoads = []; foreach ($includes as $key => $value) { $eagerLoads[] = is_string($key) ? $key : $value; } return $eagerLoads; }
/** * Specify relations for eager loading. * * @return array */ protected function getEagerLoad() { $includes = $this->transformer->getAvailableIncludes(); return $includes ?: []; }
/** * Prepare root scope and set some meta information. * * @param Item|Collection $resource * * @return \League\Fractal\Scope */ protected function prepareRootScope($resource) { $resource->setMetaValue('available_includes', $this->transformer->getAvailableIncludes()); $resource->setMetaValue('default_includes', $this->transformer->getDefaultIncludes()); return $this->fractal->createData($resource); }
/** * Remove support for include data for the current request */ protected function removeIncludeSupport() { $this->transformer->setAvailableIncludes([]); }
/** * (non-PHPdoc) * @see \Solvire\API\Serializers\DataFields\DataField::getData() */ public function getData() { return $this->transformer->transform($this->data); }
/** * @param TransformerAbstract $transformer * @param $serviceId */ public function add(TransformerAbstract $transformer, $serviceId) { $transformer->setRequest($this->request); $this->mapping[$serviceId] = $transformer; }
/** * @inheritDoc */ protected function collection($data, $transformer, $resourceKey = null) { /* @var BaseTransformer $transformer */ // TODO: check if there is a necessity to only return null if a transformer is marked included if (is_null($data)) { return null; } return parent::collection($data, $transformer, $resourceKey); }
/** * @param TransformerAbstract $transformer * @param Request $request * * @return array */ private function getEmbedsWithoutOptions(TransformerAbstract $transformer, Request $request) { $embeds = $request->get('embed', null); if (null === $embeds) { $embeds = implode(',', $transformer->getDefaultIncludes()); } $embedAvailable = $request->headers->get(self::EMBED_AVAILABLE, 0); if ($request->headers->has(self::EMBED_AVAILABLE) && !$embedAvailable) { $transformer->setDefaultIncludes([]); } if ($embedAvailable) { if (is_null($embeds)) { $embeds = implode(',', $transformer->getAvailableIncludes()); } else { $arrayEmbed = array_merge(explode(',', $embeds), $transformer->getAvailableIncludes()); $embeds = implode(',', $arrayEmbed); } } return explode(',', $embeds); }
/** * Call Include Method. * * @internal * * @param Scope $scope * @param string $includeName * @param mixed $data * * @throws \Exception * * @return \League\Fractal\Resource\ResourceInterface */ protected function callIncludeMethod(Scope $scope, $includeName, $data) { if (method_exists($scope, 'setData')) { $scope->setData($data); } return parent::callIncludeMethod($scope, $includeName, $data); }
/** * @param TransformerAbstract $transformer * @param $object * @param $embed * @param string $version * * @return bool */ protected function isEmbed(TransformerAbstract $transformer, $object, $embed, $version = 'v2') { if (in_array($embed, $transformer->getAvailableIncludes())) { return call_user_func([$object, 'get' . ucfirst($embed)]); } return null; }
/** * Fire the included transformers. * * @internal * * @param \League\Fractal\TransformerAbstract $transformer * @param mixed $data * * @return array */ protected function fireIncludedTransformers($transformer, $data) { return $transformer->processIncludedResources($this, $data) ?: []; }
/** * Quick hack until Fractal support embeded data without the use of * the data-key * * @see https://github.com/thephpleague/fractal/issues/37 * * {@inheritDoc} */ public function processIncludedResources(Scope $scope, $data) { $embeded = parent::processIncludedResources($scope, $data); if (!is_null($embeded) && !is_bool($embeded)) { $embeded = array_map(function ($d) { return current($d); }, $embeded); } return $embeded; }