diff --git a/src/Utils/TransformerData.php b/src/Utils/TransformerData.php new file mode 100644 index 0000000..38791ff --- /dev/null +++ b/src/Utils/TransformerData.php @@ -0,0 +1,135 @@ +transform = $transform; + $manager = new Manager(); + $manager->setSerializer(new ArraySerializer()); + + if (is_array($data)) { + foreach ($data as $varName => &$var) { + $var = $this->transform($manager, $var); + } + unset($var); + } else { + $data = $this->transform($manager, $data); + } + + return $data; + } + + /** + * @param \League\Fractal\Manager $manager + * @param $var + * @return array + * @throws \Exception + */ + protected function transform(Manager $manager, $var) + { + if (!$transformer = $this->getTransformer()) { + return $var; + } + + if (is_array($var) || $var instanceof Query || $var instanceof ResultSet) { + $resource = new Collection($var, $transformer, 'data'); + } elseif ($var instanceof EntityInterface) { + $resource = new Item($var, $transformer); + } else { + throw new Exception('Unserializable variable'); + } + + return $manager->createData($resource)->toArray(); + } + + /** + * @return bool|\League\Fractal\TransformerAbstract + * @throws \Exception + */ + protected function getTransformer() + { + $transformerClass = $this->transform; + + if ($transformerClass === false) { + return false; + } + + if (!class_exists($transformerClass)) { + throw new Exception(sprintf('Invalid Transformer class: %s', $transformerClass)); + } + + $transformer = new $transformerClass; + if (!($transformer instanceof TransformerAbstract)) { + throw new Exception( + sprintf( + 'Transformer class not instance of TransformerAbstract: %s', $transformerClass + ) + ); + } + + return $transformer; + } + + /** + * @param $var + * @return bool|string + */ + protected function getTransformerClass($var) + { + $entity = null; + if ($var instanceof Query) { + $entity = $var->repository()->newEntity(); + } elseif ($var instanceof ResultSet) { + $entity = $var->first(); + } elseif ($var instanceof EntityInterface) { + $entity = $var; + } elseif (is_array($var)) { + $entity = reset($var); + } + + if (!$entity || !is_object($entity)) { + return false; + } + + $entityClass = get_class($entity); + $transformerClass = str_replace('\\Model\\Entity\\', '\\Model\\Transformer\\', $entityClass) . 'Transformer'; + + if (!class_exists($transformerClass)) { + return false; + } + + return $transformerClass; + } +} diff --git a/src/View/FractalTransformerView.php b/src/View/FractalTransformerView.php index c601393..b62e07b 100644 --- a/src/View/FractalTransformerView.php +++ b/src/View/FractalTransformerView.php @@ -1,7 +1,7 @@ setSerializer($viewOptions['serializer']); } @@ -118,7 +119,7 @@ protected function getTransformerClass($var) * * @param mixed $var variable * @param bool $varName variable name - * @return bool + * @return bool|\League\Fractal\TransformerAbstract * @throws Exception */ protected function getTransformer($var, $varName = false) @@ -156,7 +157,7 @@ protected function getTransformer($var, $varName = false) /** * Transform var using given manager * - * @param Manager $manager Fractal manager + * @param Manager $manager Fractal manager * @param mixed $var variable * @param bool $varName variable name * @return array