Permalink
Browse files

XmlView: configure top level element name

Allow the top level element name to be configured by setting
XmlView.RootNodeName.
  • Loading branch information...
1 parent 35686c9 commit 54056fdda7f96b7c2d4c9a4c9fbcc9c0083ea060 @larryb82 committed Nov 2, 2012
Showing with 26 additions and 5 deletions.
  1. +20 −2 lib/Cake/Test/Case/View/XmlViewTest.php
  2. +6 −3 lib/Cake/View/XmlView.php
View
22 lib/Cake/Test/Case/View/XmlViewTest.php
@@ -35,6 +35,14 @@ class XmlViewTest extends CakeTestCase {
* @return void
*/
public function testRenderWithoutView() {
+ Configure::write('XmlView.RootNodeName', null);
+ $this->__testRenderWithoutView('response');
+
+ Configure::write('XmlView.RootNodeName', 'custom_name');
+ $this->__testRenderWithoutView('custom_name');
+ }
+
+ private function __testRenderWithoutView($rootNodeName) {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
@@ -62,16 +70,26 @@ public function testRenderWithoutView() {
$View = new XmlView($Controller);
$output = $View->render(false);
- $expected = Xml::build(array('response' => array('users' => $data)))->asXML();
+ $expected = Xml::build(array($rootNodeName => array('users' => $data)))->asXML();
$this->assertSame($expected, $output);
}
+
+
/**
* Test render with an array in _serialize
*
* @return void
*/
public function testRenderWithoutViewMultiple() {
+ Configure::write('XmlView.RootNodeName', null);
+ $this->__testRenderWithoutViewMultiple('response');
+
+ Configure::write('XmlView.RootNodeName', 'custom_name');
+ $this->__testRenderWithoutViewMultiple('custom_name');
+ }
+
+ private function __testRenderWithoutViewMultiple($rootNodeName) {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
@@ -82,7 +100,7 @@ public function testRenderWithoutViewMultiple() {
$output = $View->render(false);
$expected = array(
- 'response' => array('no' => $data['no'], 'user' => $data['user'])
+ $rootNodeName => array('no' => $data['no'], 'user' => $data['user'])
);
$this->assertSame(Xml::build($expected)->asXML(), $output);
$this->assertSame('application/xml', $Response->type());
View
9 lib/Cake/View/XmlView.php
@@ -99,15 +99,18 @@ public function render($view = null, $layout = null) {
* @return string The serialized data
*/
protected function _serialize($serialize) {
+ $root = Configure::read('XmlView.RootNodeName');
+ $root = empty($root) ? 'response' : $root;
+
if (is_array($serialize)) {
- $data = array('response' => array());
+ $data = array($root => array());
foreach ($serialize as $key) {
- $data['response'][$key] = $this->viewVars[$key];
+ $data[$root][$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($root => array($serialize => $data));
}
}
return Xml::fromArray($data)->asXML();

0 comments on commit 54056fd

Please sign in to comment.