Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merging hotfix. Forward port zendframework/zendframework#2463
Browse files Browse the repository at this point in the history
  • Loading branch information
Freeaqingme committed Feb 8, 2013
2 parents f306e35 + 0e07a55 commit 958decf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Renderer/JsonRenderer.php
Expand Up @@ -130,6 +130,8 @@ public function render($nameOrModel, $values = null)
// Serialize variables in view model
if ($nameOrModel instanceof Model) {
if ($nameOrModel instanceof JsonModel) {
$children = $this->recurseModel($nameOrModel, false);
$this->injectChildren($nameOrModel, $children);
$values = $nameOrModel->serialize();
} else {
$values = $this->recurseModel($nameOrModel);
Expand Down Expand Up @@ -183,11 +185,17 @@ public function canRenderTrees()
* Retrieve values from a model and recurse its children to build a data structure
*
* @param Model $model
* @param bool $mergeWithVariables Whether or not to merge children with
* the variables of the $model
* @return array
*/
protected function recurseModel(Model $model)
protected function recurseModel(Model $model, $mergeWithVariables = true)
{
$values = $model->getVariables();
$values = array();
if ($mergeWithVariables) {
$values = $model->getVariables();
}

if ($values instanceof Traversable) {
$values = ArrayUtils::iteratorToArray($values);
}
Expand All @@ -207,7 +215,8 @@ protected function recurseModel(Model $model)
$childValues = $this->recurseModel($child);
if ($captureTo) {
// Capturing to a specific key
//TODO please complete if append is true. must change old value to array and append to array?
// TODO please complete if append is true. must change old
// value to array and append to array?
$values[$captureTo] = $childValues;
} elseif ($mergeChildren) {
// Merging values with parent
Expand All @@ -216,4 +225,19 @@ protected function recurseModel(Model $model)
}
return $values;
}

/**
* Inject discovered child model values into parent model
*
* @todo detect collisions and decide whether to append and/or aggregate?
* @param Model $model
* @param array $children
*/
protected function injectChildren(Model $model, array $children)
{
foreach ($children as $child => $value) {
// TODO detect collisions and decide whether to append and/or aggregate?
$model->setVariable($child, $value);
}
}
}
27 changes: 27 additions & 0 deletions test/Renderer/JsonRendererTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\View\Renderer\JsonRenderer;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;

/**
Expand Down Expand Up @@ -234,4 +235,30 @@ public function testRendersNonTraversableNonJsonSerializableObjectsAsJsonObjects
$test = $this->renderer->render($model);
$this->assertEquals($expected, $test);
}

/**
* @group 2463
*/
public function testRecursesJsonModelChildrenWhenRendering()
{
$root = new JsonModel(array('foo' => 'bar'));
$child1 = new JsonModel(array('foo' => 'bar'));
$child2 = new JsonModel(array('foo' => 'bar'));
$child1->setCaptureTo('child1');
$child2->setCaptureTo('child2');
$root->addChild($child1)
->addChild($child2);

$expected = array(
'foo' => 'bar',
'child1' => array(
'foo' => 'bar',
),
'child2' => array(
'foo' => 'bar',
),
);
$test = $this->renderer->render($root);
$this->assertEquals(json_encode($expected), $test);
}
}

0 comments on commit 958decf

Please sign in to comment.