mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
XmlView: configure top level element name
Allow the top level element name to be configured by setting XmlView.RootNodeName.
This commit is contained in:
parent
570acfa77b
commit
a60a730e7e
2 changed files with 21 additions and 5 deletions
|
@ -64,6 +64,13 @@ class XmlViewTest extends CakeTestCase {
|
|||
|
||||
$expected = Xml::build(array('response' => array('users' => $data)))->asXML();
|
||||
$this->assertSame($expected, $output);
|
||||
|
||||
$Controller->set('_rootNode', 'custom_name');
|
||||
$View = new XmlView($Controller);
|
||||
$output = $View->render(false);
|
||||
|
||||
$expected = Xml::build(array('custom_name' => array('users' => $data)))->asXML();
|
||||
$this->assertSame($expected, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,13 +86,20 @@ class XmlViewTest extends CakeTestCase {
|
|||
$Controller->set($data);
|
||||
$Controller->set('_serialize', array('no', 'user'));
|
||||
$View = new XmlView($Controller);
|
||||
$this->assertSame('application/xml', $Response->type());
|
||||
$output = $View->render(false);
|
||||
|
||||
$expected = array(
|
||||
'response' => array('no' => $data['no'], 'user' => $data['user'])
|
||||
);
|
||||
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
||||
$this->assertSame('application/xml', $Response->type());
|
||||
|
||||
$Controller->set('_rootNode', 'custom_name');
|
||||
$View = new XmlView($Controller);
|
||||
$output = $View->render(false);
|
||||
$expected = array(
|
||||
'custom_name' => array('no' => $data['no'], 'user' => $data['user'])
|
||||
);
|
||||
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -99,15 +99,17 @@ class XmlView extends View {
|
|||
* @return string The serialized data
|
||||
*/
|
||||
protected function _serialize($serialize) {
|
||||
$rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
|
||||
|
||||
if (is_array($serialize)) {
|
||||
$data = array('response' => array());
|
||||
$data = array($rootNode => array());
|
||||
foreach ($serialize as $key) {
|
||||
$data['response'][$key] = $this->viewVars[$key];
|
||||
$data[$rootNode][$key] = $this->viewVars[$key];
|
||||
}
|
||||
} else {
|
||||
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
||||
if (is_array($data) && Set::numeric(array_keys($data))) {
|
||||
$data = array('response' => array($serialize => $data));
|
||||
$data = array($rootNode => array($serialize => $data));
|
||||
}
|
||||
}
|
||||
return Xml::fromArray($data)->asXML();
|
||||
|
|
Loading…
Reference in a new issue