From cbb4f7db90616e5902fb4658afac9e15cb2f0b82 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Sat, 6 Jul 2013 16:57:46 +0000 Subject: [PATCH] allow serialize in json and xml alias --- lib/Cake/Test/Case/View/JsonViewTest.php | 19 ++++++++++++++++ lib/Cake/Test/Case/View/XmlViewTest.php | 29 ++++++++++++++++++++++++ lib/Cake/View/JsonView.php | 7 ++++-- lib/Cake/View/XmlView.php | 7 ++++-- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 5ee975657..01547024a 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -67,6 +67,25 @@ class JsonViewTest extends CakeTestCase { $this->assertSame('application/json', $Response->type()); } +/** + * Test render with an array in _serialize and alias + * + * @return void + */ + public function testRenderWithoutViewMultipleAndAlias() { + $Request = new CakeRequest(); + $Response = new CakeResponse(); + $Controller = new Controller($Request, $Response); + $data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2')); + $Controller->set($data); + $Controller->set('_serialize', array('new_name' => 'original_name', 'user')); + $View = new JsonView($Controller); + $output = $View->render(false); + + $this->assertSame(json_encode(array('new_name' => $data['original_name'], 'user' => $data['user'])), $output); + $this->assertSame('application/json', $Response->type()); + } + /** * testJsonpResponse method * diff --git a/lib/Cake/Test/Case/View/XmlViewTest.php b/lib/Cake/Test/Case/View/XmlViewTest.php index bf6bf3dcb..8d150e737 100644 --- a/lib/Cake/Test/Case/View/XmlViewTest.php +++ b/lib/Cake/Test/Case/View/XmlViewTest.php @@ -103,6 +103,35 @@ class XmlViewTest extends CakeTestCase { $this->assertSame(Xml::build($expected)->asXML(), $output); } +/** + * Test render with an array in _serialize and alias + * + * @return void + */ + public function testRenderWithoutViewMultipleAndAlias() { + $Request = new CakeRequest(); + $Response = new CakeResponse(); + $Controller = new Controller($Request, $Response); + $data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2')); + $Controller->set($data); + $Controller->set('_serialize', array('new_name' => 'original_name', 'user')); + $View = new XmlView($Controller); + $this->assertSame('application/xml', $Response->type()); + $output = $View->render(false); + $expected = array( + 'response' => array('new_name' => $data['original_name'], 'user' => $data['user']) + ); + $this->assertSame(Xml::build($expected)->asXML(), $output); + + $Controller->set('_rootNode', 'custom_name'); + $View = new XmlView($Controller); + $output = $View->render(false); + $expected = array( + 'custom_name' => array('new_name' => $data['original_name'], 'user' => $data['user']) + ); + $this->assertSame(Xml::build($expected)->asXML(), $output); + } + /** * testRenderWithView method * diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index cc73e3629..3cb81ca97 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -119,8 +119,11 @@ class JsonView extends View { protected function _serialize($serialize) { if (is_array($serialize)) { $data = array(); - foreach ($serialize as $key) { - $data[$key] = $this->viewVars[$key]; + foreach ($serialize as $alias => $key) { + if (is_numeric($alias)) { + $alias = $key; + } + $data[$alias] = $this->viewVars[$key]; } } else { $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null; diff --git a/lib/Cake/View/XmlView.php b/lib/Cake/View/XmlView.php index e77698eec..f95fbc675 100644 --- a/lib/Cake/View/XmlView.php +++ b/lib/Cake/View/XmlView.php @@ -104,8 +104,11 @@ class XmlView extends View { if (is_array($serialize)) { $data = array($rootNode => array()); - foreach ($serialize as $key) { - $data[$rootNode][$key] = $this->viewVars[$key]; + foreach ($serialize as $alias => $key) { + if (is_numeric($alias)) { + $alias = $key; + } + $data[$rootNode][$alias] = $this->viewVars[$key]; } } else { $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;