Skip to content

Commit

Permalink
Add TransformerData
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonsimaogoncalves committed May 26, 2018
1 parent 95d9af4 commit 4e4afa4
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 4 deletions.
135 changes: 135 additions & 0 deletions src/Utils/TransformerData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* Created by PhpStorm.
* User: jeffersonsimaogoncalves
* Date: 25/05/2018
* Time: 23:45
*/

namespace FractalTransformerView\Utils;

use Cake\Datasource\EntityInterface;
use Cake\ORM\Query;
use Cake\ORM\ResultSet;
use Exception;
use FractalTransformerView\Serializer\ArraySerializer;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;

/**
* Class TransformerData
*
* @property \League\Fractal\TransformerAbstract transform
*
* @package FractalTransformerView\Utils
*/
class TransformerData
{
/**
* @param $data
* @param $transform
* @return array
* @throws \Exception
*/
public function getData($data, $transform)
{
$this->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;
}
}
9 changes: 5 additions & 4 deletions src/View/FractalTransformerView.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace FractalTransformerView\View;

use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventManager;
use Cake\Network\Request;
Expand Down Expand Up @@ -43,7 +43,8 @@ public function __construct(
Response $response = null,
EventManager $eventManager = null,
array $viewOptions = []
) {
)
{
if (isset($viewOptions['serializer'])) {
$this->setSerializer($viewOptions['serializer']);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e4afa4

Please sign in to comment.