mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #1405 from jippi/feature/_serialize-in-json-and-xml-alias
Allow serialize in json and xml alias.
This commit is contained in:
commit
753200196f
4 changed files with 58 additions and 4 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue