From 46f784f95642487ed54e2e6a69305e1d6558ef98 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sun, 12 Sep 2010 11:25:37 -0300 Subject: [PATCH] Support to DOMDocument/DOMNode in Xml::toArray(). --- cake/libs/xml.php | 15 +++++++++------ cake/tests/cases/libs/xml.test.php | 3 +++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cake/libs/xml.php b/cake/libs/xml.php index f3153b2e4..9b1905b82 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -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; } diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index 4f71e0b35..6635d0662 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -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 = ''; $xml .= 'defect';