fixes #6538, xml serialization on multi dimensional

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8264 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2009-07-31 01:50:14 +00:00
parent 90445e9119
commit 9e1dec3ff3
3 changed files with 50 additions and 11 deletions

View file

@ -201,6 +201,7 @@ class XmlNode extends Object {
}
$tagOpts = $this->__tagOptions($name);
if ($tagOpts === false) {
return;
}
@ -221,7 +222,6 @@ class XmlNode extends Object {
$attributes = array();
$children = array();
$chldObjs = array();
$document =& $this->document();
if (is_object($object)) {
$chldObjs = get_object_vars($object);
@ -239,7 +239,12 @@ class XmlNode extends Object {
$node->createTextNode($chldObjs[$tagOpts['value']]);
unset($chldObjs[$tagOpts['value']]);
}
$n = $name;
if (!empty($chldObjs['_name_'])) {
$n = null;
unset($chldObjs['_name_']);
}
$c = 0;
foreach ($chldObjs as $key => $val) {
@ -247,14 +252,11 @@ class XmlNode extends Object {
$attributes[$key] = $val;
} else {
if (!isset($tagOpts['children']) || $tagOpts['children'] === array() || (is_array($tagOpts['children']) && in_array($key, $tagOpts['children']))) {
if (!is_numeric($key)) {
$n = $key;
if (is_numeric($n)) {
$n = $name;
}
if (is_array($val)) {
foreach ($val as $i => $obj2) {
$n2 = $i;
foreach ($val as $n2 => $obj2) {
if (is_numeric($n2)) {
$n2 = $n;
}
@ -262,6 +264,7 @@ class XmlNode extends Object {
}
} else {
if (is_object($val)) {
$node->normalize($val, $n, $options);
} elseif ($options['format'] == 'tags' && $this->__tagOptions($key) !== false) {
$tmp =& $node->createElement($key);
@ -630,7 +633,6 @@ class XmlNode extends Object {
if ($options['whitespace']) {
$d .= "\n";
}
$count = count($this->children);
$cDepth = $depth + 1;
for ($i = 0; $i < $count; $i++) {

View file

@ -210,6 +210,24 @@ class XmlHelperTest extends CakeTestCase {
$expected = '<pages id="2" url="http://www.url.com/rb/153/?id=bbbb&amp;t=access" />';
$this->assertIdentical($result, $expected);
}
/**
* testSerializeOnMultiDimensionalArray method
*
* @access public
* @return void
*/
function testSerializeOnMultiDimensionalArray() {
$data = array(
'Statuses' => array(
array('Status' => array('id' => 1)),
array('Status' => array('id' => 2))
)
);
$result = $this->Xml->serialize($data, array('format' => 'tags'));
$expected = '<statuses><status><id>1</id></status><status><id>2</id></status></statuses>';
$this->assertIdentical($result, $expected);
}
/**
* testHeader method
*

View file

@ -98,6 +98,25 @@ class XmlTest extends CakeTestCase {
$this->assertEqual($result, $expected);
}
/**
* testSerializeOnMultiDimensionalArray method
*
* @access public
* @return void
*/
function testSerializeOnMultiDimensionalArray() {
$data = array(
'Statuses' => array(
array('Status' => array('id' => 1)),
array('Status' => array('id' => 2))
)
);
$result =& new Xml($data, array('format' => 'tags'));
$expected = '<statuses><status><id>1</id></status><status><id>2</id></status></statuses>';
$this->assertIdentical($result->toString(), $expected);
}
/**
* test serialization of boolean and null values. false = 0, true = 1, null = ''
*