Support to DOMDocument/DOMNode in Xml::toArray().

This commit is contained in:
Juan Basso 2010-09-12 11:25:37 -03:00
parent a006ee0019
commit 46f784f956
2 changed files with 12 additions and 6 deletions

View file

@ -268,16 +268,19 @@ class Xml {
/**
* Returns this XML structure as a array.
*
* @param object $simpleXML SimpleXMLElement instance
* @param object $obj SimpleXMLElement, DOMDocument or DOMNode instance
* @return array Array representation of the XML structure.
*/
public static function toArray($simpleXML) {
if (!($simpleXML instanceof SimpleXMLElement)) {
throw new Exception(__('The input is not instance of SimpleXMLElement.'));
public static function toArray($obj) {
if ($obj instanceof DOMNode) {
$obj = simplexml_import_dom($obj);
}
if (!($obj instanceof SimpleXMLElement)) {
throw new Exception(__('The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.'));
}
$result = array();
$namespaces = array_merge(array('' => ''), $simpleXML->getNamespaces(true));
self::_toArray($simpleXML, $result, '', array_keys($namespaces));
$namespaces = array_merge(array('' => ''), $obj->getNamespaces(true));
self::_toArray($obj, $result, '', array_keys($namespaces));
return $result;
}

View file

@ -360,7 +360,9 @@ class XmlTest extends CakeTestCase {
)
);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, 'attributes')), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument', 'format' => 'attributes'))), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($array)), $array);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
$array = array(
'tags' => array(
@ -395,6 +397,7 @@ class XmlTest extends CakeTestCase {
)
);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, 'attributes')), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))), $expected);
$xml = '<root>';
$xml .= '<tag id="1">defect</tag>';