mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Support to DOMDocument/DOMNode in Xml::toArray().
This commit is contained in:
parent
a006ee0019
commit
46f784f956
2 changed files with 12 additions and 6 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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>';
|
||||
|
|
Loading…
Add table
Reference in a new issue