Permalink
Browse files

allow serialize in json and xml alias

  • Loading branch information...
1 parent fd2c096 commit cbb4f7db90616e5902fb4658afac9e15cb2f0b82 @jippi jippi committed Jul 6, 2013
@@ -68,6 +68,25 @@ public function testRenderWithoutViewMultiple() {
}
/**
+ * 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
*
* @return void
@@ -104,6 +104,35 @@ public function testRenderWithoutViewMultiple() {
}
/**
+ * 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
*
* @return void
@@ -119,8 +119,11 @@ public function render($view = null, $layout = null) {
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;
@@ -104,8 +104,11 @@ protected function _serialize($serialize) {
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;

0 comments on commit cbb4f7d

Please sign in to comment.