Permalink
Browse files

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
  • Loading branch information...
1 parent 90445e9 commit 9e1dec3ff3e116259eb5e390154c0f7fff4f6aeb gwoo committed Jul 31, 2009
Showing with 50 additions and 11 deletions.
  1. +11 −9 cake/libs/xml.php
  2. +19 −1 cake/tests/cases/libs/view/helpers/xml.test.php
  3. +20 −1 cake/tests/cases/libs/xml.test.php
View
@@ -201,6 +201,7 @@ function normalize($object, $keyName = null, $options = array()) {
}
$tagOpts = $this->__tagOptions($name);
+
if ($tagOpts === false) {
return;
}
@@ -221,7 +222,6 @@ function normalize($object, $keyName = null, $options = array()) {
$attributes = array();
$children = array();
$chldObjs = array();
- $document =& $this->document();
if (is_object($object)) {
$chldObjs = get_object_vars($object);
@@ -239,29 +239,32 @@ function normalize($object, $keyName = null, $options = array()) {
$node->createTextNode($chldObjs[$tagOpts['value']]);
unset($chldObjs[$tagOpts['value']]);
}
- unset($chldObjs['_name_']);
+
+ $n = $name;
+ if (!empty($chldObjs['_name_'])) {
+ $n = null;
+ unset($chldObjs['_name_']);
+ }
$c = 0;
foreach ($chldObjs as $key => $val) {
if (in_array($key, $attr) && !is_object($val) && !is_array($val)) {
$attributes[$key] = $val;
} else {
if (!isset($tagOpts['children']) || $tagOpts['children'] === array() || (is_array($tagOpts['children']) && in_array($key, $tagOpts['children']))) {
- $n = $key;
-
- if (is_numeric($n)) {
- $n = $name;
+ if (!is_numeric($key)) {
+ $n = $key;
}
if (is_array($val)) {
- foreach ($val as $i => $obj2) {
- $n2 = $i;
+ foreach ($val as $n2 => $obj2) {
if (is_numeric($n2)) {
$n2 = $n;
}
$node->normalize($obj2, $n2, $options);
}
} 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 @@ function toString($options = array(), $depth = 0) {
if ($options['whitespace']) {
$d .= "\n";
}
-
$count = count($this->children);
$cDepth = $depth + 1;
for ($i = 0; $i < $count; $i++) {
@@ -202,14 +202,32 @@ function testSerialize() {
$result = $this->Xml->serialize($data, array('format' => 'tags'));
$expected = '<service_day><service_time><service_time_price><dollar>1</dollar><cents>2</cents></service_time_price></service_time></service_day>';
$this->assertIdentical($result, $expected);
-
+
$data = array(
'Pages' => array('id' => 2, 'url' => 'http://www.url.com/rb/153/?id=bbbb&t=access')
);
$result = $this->Xml->serialize($data);
$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
*
@@ -98,6 +98,25 @@ function testSerialization() {
$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 = ''
*
@@ -118,7 +137,7 @@ function testSerializationOfBooleanAndBooleanishValues() {
$result = $xml->toString(false);
$expected = '<data example="" />';
$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
-
+
$xml =& new Xml(array('data' => array('example' => 0)));
$result = $xml->toString(false);
$expected = '<data example="0" />';

0 comments on commit 9e1dec3

Please sign in to comment.