Adding support for array serialize values.

Using an array for 'serialize' gives a set of view vars
to convert into the view output.
This commit is contained in:
mark_story 2011-11-17 22:24:49 -05:00
parent d9482fef25
commit 50ec08f9bd
4 changed files with 85 additions and 6 deletions

View file

@ -47,6 +47,25 @@ class JsonViewTest extends CakeTestCase {
$this->assertIdentical('application/json', $Response->type());
}
/**
* Test render with an array in serialize
*
* @return void
*/
public function testRenderWithoutViewMultiple() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('serialize', array('no', 'user'));
$View = new JsonView($Controller);
$output = $View->render(false);
$this->assertIdentical(json_encode(array('no' =>$data['no'], 'user' => $data['user'])), $output);
$this->assertIdentical('application/json', $Response->type());
}
/**
* testRenderWithView method
*

View file

@ -48,6 +48,28 @@ class XmlViewTest extends CakeTestCase {
$this->assertIdentical('application/xml', $Response->type());
}
/**
* Test render with an array in serialize
*
* @return void
*/
public function testRenderWithoutViewMultiple() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('serialize', array('no', 'user'));
$View = new XmlView($Controller);
$output = $View->render(false);
$expected = array(
'response' => array('no' =>$data['no'], 'user' => $data['user'])
);
$this->assertIdentical(Xml::build($expected)->asXML(), $output);
$this->assertIdentical('application/xml', $Response->type());
}
/**
* testRenderWithView method
*

View file

@ -28,8 +28,20 @@ App::uses('View', 'View');
* When the view is rendered, the `$posts` view variable will be serialized
* into JSON.
*
* If you don't use the `serialize` key, you will need a view + layout just like a
* normal view.
* You can also define `'serialize'` as an array. This will create a top level object containing
* all the named view variables:
*
* {{{
* $this->set(compact('posts', 'users', 'stuff'));
* $this->set('serialize', array('posts', 'users'));
* }}}
*
* The above would generate a JSON object that looks like:
*
* `{"posts": [...], "users": [...]}`
*
* If you don't use the `serialize` key, you will need a view. You can use extended
* views to provide layout like functionality.
*
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
@ -73,7 +85,14 @@ class JsonView extends View {
public function render($view = null, $layout = null) {
if (isset($this->viewVars['serialize'])) {
$serialize = $this->viewVars['serialize'];
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
if (is_array($serialize)) {
$data = array();
foreach ($serialize as $key) {
$data[$key] = $this->viewVars[$key];
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
}
return $this->output = json_encode($data);
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {

View file

@ -31,8 +31,20 @@ App::uses('Xml', 'Utility');
*
* **Note** The view variable you specify must be compatible with Xml::fromArray().
*
* If you don't use the `serialize` key, you will need a view + layout just like a
* normal view.
* You can also define `'serialize'` as an array. This will create an additional
* top level element named `<response>` containing all the named view variables:
*
* {{{
* $this->set(compact('posts', 'users', 'stuff'));
* $this->set('serialize', array('posts', 'users'));
* }}}
*
* The above would generate a XML object that looks like:
*
* `<response><posts>...</posts><users>...</users></response>`
*
* If you don't use the `serialize` key, you will need a view. You can use extended
* views to provide layout like functionality.
*
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
@ -74,7 +86,14 @@ class XmlView extends View {
public function render($view = null, $layout = null) {
if (isset($this->viewVars['serialize'])) {
$serialize = $this->viewVars['serialize'];
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
if (is_array($serialize)) {
$data = array('response' => array());
foreach ($serialize as $key) {
$data['response'][$key] = $this->viewVars[$key];
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
}
return $this->output = Xml::fromArray($data)->asXML();
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {