mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06: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.
|
* 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.
|
* @return array Array representation of the XML structure.
|
||||||
*/
|
*/
|
||||||
public static function toArray($simpleXML) {
|
public static function toArray($obj) {
|
||||||
if (!($simpleXML instanceof SimpleXMLElement)) {
|
if ($obj instanceof DOMNode) {
|
||||||
throw new Exception(__('The input is not instance of SimpleXMLElement.'));
|
$obj = simplexml_import_dom($obj);
|
||||||
|
}
|
||||||
|
if (!($obj instanceof SimpleXMLElement)) {
|
||||||
|
throw new Exception(__('The input is not instance of SimpleXMLElement, DOMDocument or DOMNode.'));
|
||||||
}
|
}
|
||||||
$result = array();
|
$result = array();
|
||||||
$namespaces = array_merge(array('' => ''), $simpleXML->getNamespaces(true));
|
$namespaces = array_merge(array('' => ''), $obj->getNamespaces(true));
|
||||||
self::_toArray($simpleXML, $result, '', array_keys($namespaces));
|
self::_toArray($obj, $result, '', array_keys($namespaces));
|
||||||
return $result;
|
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, '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);
|
||||||
|
$this->assertEqual(Xml::toArray(Xml::fromArray($array, array('return' => 'domdocument'))), $array);
|
||||||
|
|
||||||
$array = array(
|
$array = array(
|
||||||
'tags' => 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, 'attributes')), $expected);
|
||||||
|
$this->assertEqual(Xml::toArray(Xml::fromArray($array, array('format' => 'attributes', 'return' => 'domdocument'))), $expected);
|
||||||
|
|
||||||
$xml = '<root>';
|
$xml = '<root>';
|
||||||
$xml .= '<tag id="1">defect</tag>';
|
$xml .= '<tag id="1">defect</tag>';
|
||||||
|
|
Loading…
Add table
Reference in a new issue