Permalink
Browse files

Adding support for array serialize values.

Using an array for 'serialize' gives a set of view vars
to convert into the view output.
  • Loading branch information...
1 parent d9482fe commit 50ec08f9bd6c6ee19b787249434cd22956b215af @markstory markstory committed Nov 18, 2011
@@ -48,6 +48,25 @@ public function testRenderWithoutView() {
}
/**
+ * 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
*
* @return void
@@ -49,6 +49,28 @@ public function testRenderWithoutView() {
}
/**
+ * 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
*
* @return void
View
@@ -28,8 +28,20 @@
* 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 @@ public function __construct($controller) {
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
@@ -31,8 +31,20 @@
*
* **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 @@ public function __construct($controller) {
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)) {

0 comments on commit 50ec08f

Please sign in to comment.