Skip to content

Commit

Permalink
Allow underscore separated keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Mar 17, 2014
1 parent f43dd53 commit f2ae3de
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/ZfrRest/View/Renderer/DefaultResourceRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ZfrRest\View\Renderer;

use Zend\Filter\Word\CamelCaseToUnderscore;
use Zend\Paginator\Paginator;
use Zend\Stdlib\Hydrator\HydratorPluginManager;
use ZfrRest\Exception\RuntimeException;
Expand Down Expand Up @@ -87,6 +88,11 @@ class DefaultResourceRenderer extends AbstractResourceRenderer
*/
protected $hydratorPluginManager;

/**
* @var bool
*/
protected $underscoreSeparatedKeys;

/**
* @var array
*/
Expand All @@ -95,13 +101,16 @@ class DefaultResourceRenderer extends AbstractResourceRenderer
/**
* @param ResourceMetadataFactory $resourceMetadataFactory
* @param HydratorPluginManager $hydratorManager
* @param bool $underscoreSeparatedKeys
*/
public function __construct(
ResourceMetadataFactory $resourceMetadataFactory,
HydratorPluginManager $hydratorManager
HydratorPluginManager $hydratorManager,
$underscoreSeparatedKeys = true
) {
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->hydratorPluginManager = $hydratorManager;
$this->underscoreSeparatedKeys = (bool) $underscoreSeparatedKeys;
}

/**
Expand Down Expand Up @@ -168,10 +177,16 @@ public function renderItem($object, ResourceMetadataInterface $resourceMetadata)
*/
protected function renderAssociations(array $data, ResourceMetadataInterface $resourceMetadata)
{
$inflector = new CamelCaseToUnderscore();

$classMetadata = $resourceMetadata->getClassMetadata();
$associations = $classMetadata->getAssociationNames();

foreach ($associations as $association) {
if ($this->underscoreSeparatedKeys) {
$association = strtolower($inflector->filter($association));
}

// If the association object is not in the payload or is not defined in mapping... we cannot do anything
if (!isset($data[$association]) || !$resourceMetadata->hasAssociationMetadata($association)) {
unset($data[$association]);
Expand Down

0 comments on commit f2ae3de

Please sign in to comment.