Skip to content

Commit

Permalink
Considered exclusion strategies for application/json
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Wolf committed Feb 24, 2015
1 parent 89abe6b commit 705b58d
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/Detail/Apigility/View/JsonRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Zend\View\Renderer\JsonRenderer as BaseJsonRenderer;

use ZF\Hal\Collection as HalCollection;
use ZF\Hal\Entity as HalEntity;

use Detail\Normalization\Normalizer\NormalizerInterface;

Expand All @@ -34,14 +35,19 @@ public function render($nameOrModel, $values = null)
if ($nameOrModel->isEntity()) {
/** @var \ZF\Hal\Entity $halEntity */
$halEntity = $nameOrModel->getPayload();
$entity = $halEntity->entity;
$normalizationGroups = $this->getNormalizationGroups($halEntity);

$payload = $this->getNormalizer()->normalize($halEntity->entity);
$payload = $this->getNormalizer()->normalize($entity, $normalizationGroups);

return parent::render($payload);
}

if ($nameOrModel->isCollection()) {
$payload = $this->getCollectionPayload($nameOrModel->getPayload());
/** @var HalCollection $collection */
$collection = $nameOrModel->getPayload();

$payload = $this->getCollectionPayload($collection);

// if ($payload instanceof ApiProblem) {
// return $this->renderApiProblem($payload);
Expand All @@ -63,21 +69,24 @@ protected function getCollectionPayload(HalCollection $halCollection)

$attributes = $halCollection->getAttributes();

$normalizationGroups = $this->getNormalizationGroups($halCollection);

if ($collection instanceof Paginator) {
$pageSize = (int) (isset($attributes['page_size']) ? $attributes['page_size'] : $halCollection->getPageSize());

$collection->setItemCountPerPage($pageSize);
$items = (array) $collection->getCurrentItems();

/** @todo Force snake case as collection name? */
$payload = array(
$collectionName => $this->getNormalizer()->normalize($items),
$collectionName => $this->getNormalizer()->normalize($items, $normalizationGroups),
'page_count' => (int) (isset($attributes['page_count']) ? $attributes['page_count'] : $collection->count()),
'page_size' => $pageSize,
'total_items' => (int) (isset($attributes['total_items']) ? $attributes['total_items'] : $collection->getTotalItemCount()),
);
} else {
$payload = array(
$collectionName => $this->getNormalizer()->normalize($collection),
$collectionName => $this->getNormalizer()->normalize($collection, $normalizationGroups),
);

if (is_array($collection) || $collection instanceof Countable) {
Expand All @@ -89,4 +98,27 @@ protected function getCollectionPayload(HalCollection $halCollection)

return $payload;
}

/**
* @param mixed $object
* @return array
* @todo Support groups for HAL results
*/
protected function getNormalizationGroups($object)
{
// The default group is always required so that properties without a group don't get excluded
$groups = array('Default');

if ($object instanceof HalCollection) {
$groups[] = $object->getCollectionName();
} elseif ($object instanceof HalEntity) {
$entity = $object->entity;
$entityName = (new \ReflectionClass($entity))->getShortName();

// Snake case
$groups[] = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $entityName))));
}

return $groups;
}
}

0 comments on commit 705b58d

Please sign in to comment.