mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-08 20:42:42 +00:00
fix for xml::toArray to allow any node to use it
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7450 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
a180071f6c
commit
fb3eb24a0d
2 changed files with 72 additions and 60 deletions
|
@ -633,7 +633,59 @@ class XmlNode extends Object {
|
|||
}
|
||||
|
||||
return $d;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Return array representation of current object.
|
||||
*
|
||||
* @param object optional used mainly by the method itself to reverse children
|
||||
* @return array Array representation
|
||||
* @access public
|
||||
*/
|
||||
function toArray($object = null) {
|
||||
if ($object === null) {
|
||||
$object =& $this;
|
||||
}
|
||||
if (is_a($object, 'XmlNode')) {
|
||||
$out = $object->attributes;
|
||||
$multi = null;
|
||||
foreach ($object->children as $child) {
|
||||
$key = Inflector::camelize($child->name);
|
||||
if (is_a($child, 'XmlTextNode')) {
|
||||
$out['value'] = $child->value;
|
||||
continue;
|
||||
} elseif (isset($child->children[0]) && is_a($child->children[0], 'XmlTextNode')) {
|
||||
$value = $child->children[0]->value;
|
||||
if ($child->attributes) {
|
||||
$value = array_merge(array('value' => $value), $child->attributes);
|
||||
}
|
||||
if (isset($out[$child->name])) {
|
||||
if (!isset($multi)) {
|
||||
$multi = array($key => array($out[$child->name]));
|
||||
unset($out[$child->name]);
|
||||
}
|
||||
$multi[$key][] = $value;
|
||||
} else {
|
||||
$out[$child->name] = $value;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
$value = $this->toArray($child);
|
||||
}
|
||||
if (!isset($out[$key])) {
|
||||
$out[$key] = $value;
|
||||
} else {
|
||||
if (!is_array($out[$key]) || !isset($out[$key][0])) {
|
||||
$out[$key] = array($out[$key]);
|
||||
}
|
||||
$out[$key][] = $value;
|
||||
}
|
||||
}
|
||||
if (isset($multi)) {
|
||||
$out = array_merge($out, $multi);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
/**
|
||||
* Returns data from toString when this object is converted to a string.
|
||||
*
|
||||
|
@ -943,58 +995,6 @@ class Xml extends XmlNode {
|
|||
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* Return array representation of current object.
|
||||
*
|
||||
* @param object optional used mainly by the method itself to reverse children
|
||||
* @return array Array representation
|
||||
* @access public
|
||||
*/
|
||||
function toArray($object = null) {
|
||||
if ($object === null) {
|
||||
$object =& $this;
|
||||
}
|
||||
if (is_a($object, 'XmlNode')) {
|
||||
$out = $object->attributes;
|
||||
$multi = null;
|
||||
foreach ($object->children as $child) {
|
||||
$key = Inflector::camelize($child->name);
|
||||
if (is_a($child, 'XmlTextNode')) {
|
||||
$out['value'] = $child->value;
|
||||
continue;
|
||||
} elseif (isset($child->children[0]) && is_a($child->children[0], 'XmlTextNode')) {
|
||||
$value = $child->children[0]->value;
|
||||
if ($child->attributes) {
|
||||
$value = array_merge(array('value' => $value), $child->attributes);
|
||||
}
|
||||
if (isset($out[$child->name])) {
|
||||
if (!isset($multi)) {
|
||||
$multi = array($key => array($out[$child->name]));
|
||||
unset($out[$child->name]);
|
||||
}
|
||||
$multi[$key][] = $value;
|
||||
} else {
|
||||
$out[$child->name] = $value;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
$value = $this->toArray($child);
|
||||
}
|
||||
if (!isset($out[$key])) {
|
||||
$out[$key] = $value;
|
||||
} else {
|
||||
if (!is_array($out[$key]) || !isset($out[$key][0])) {
|
||||
$out[$key] = array($out[$key]);
|
||||
}
|
||||
$out[$key][] = $value;
|
||||
}
|
||||
}
|
||||
if (isset($multi)) {
|
||||
$out = array_merge($out, $multi);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
/**
|
||||
* Return a header used on the first line of the xml file
|
||||
*
|
||||
|
|
|
@ -627,8 +627,8 @@ class XmlTest extends CakeTestCase {
|
|||
|
||||
$xml = new Xml('<example><item><title>An example of a correctly reversed XMLNode</title><desc/></item></example>');
|
||||
$result = Set::reverse($xml);
|
||||
$expected = array('Example' =>
|
||||
array(
|
||||
$expected = array(
|
||||
'Example' => array(
|
||||
'Item' => array(
|
||||
'title' => 'An example of a correctly reversed XMLNode',
|
||||
'Desc' => array(),
|
||||
|
@ -639,8 +639,8 @@ class XmlTest extends CakeTestCase {
|
|||
|
||||
$xml = new Xml('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
|
||||
$result = $xml->toArray();
|
||||
$expected =
|
||||
array('Example' => array(
|
||||
$expected = array(
|
||||
'Example' => array(
|
||||
'Item' => array(
|
||||
'attr' => '123',
|
||||
'Titles' => array(
|
||||
|
@ -649,12 +649,12 @@ class XmlTest extends CakeTestCase {
|
|||
)
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
|
||||
$result = $xml->toArray();
|
||||
$expected =
|
||||
array('Example' => array(
|
||||
$expected = array(
|
||||
'Example' => array(
|
||||
'attr' => 'ex_attr',
|
||||
'Item' => array(
|
||||
'attr' => '123',
|
||||
|
@ -665,6 +665,18 @@ class XmlTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
|
||||
$example = $xml->child('example');
|
||||
$item = $example->child('item');
|
||||
$result = $item->toArray();
|
||||
|
||||
$expected = array(
|
||||
'attr' => '123',
|
||||
'titles' => 'list',
|
||||
'value' => 'textforitems'
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue