mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Changing Xml::fromArray to use DOMDocument unless SimpleXMLElement. The return continue as SimpleXMLElement.
This commit is contained in:
parent
ca7e327af1
commit
a8b6182d43
1 changed files with 23 additions and 26 deletions
|
@ -115,33 +115,25 @@ class Xml {
|
||||||
if (is_integer($key)) {
|
if (is_integer($key)) {
|
||||||
throw new Exception(__('The key of input must be alphanumeric'));
|
throw new Exception(__('The key of input must be alphanumeric'));
|
||||||
}
|
}
|
||||||
if (is_array($input[$key])) {
|
$dom = new DOMDocument('1.0');
|
||||||
if (array_key_exists('@', $input[$key])) {
|
self::_fromArray($dom, $dom, $input, $format);
|
||||||
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key]['@'] . '</' . $key . '>');
|
return new SimpleXMLElement($dom->saveXML());
|
||||||
unset($input[$key]['@']);
|
|
||||||
} else {
|
|
||||||
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . ' />');
|
|
||||||
}
|
|
||||||
self::_fromArrayRecursive($simpleXml, $input[$key], $format);
|
|
||||||
} else {
|
|
||||||
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key] . '</' . $key . '>');
|
|
||||||
}
|
|
||||||
return $simpleXml;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursive method to create SimpleXMLElement from array
|
* Recursive method to create childs from array
|
||||||
*
|
*
|
||||||
* @param object $node Handler to SimpleXMLElement
|
* @param object $dom Handler to DOMDocument
|
||||||
* @param array $array Array of data to append to the $node.
|
* @param object $node Handler to DOMElement (child)
|
||||||
|
* @param array $data Array of data to append to the $node.
|
||||||
* @param string $format Either 'attribute' or 'tags'. This determines where nested keys go.
|
* @param string $format Either 'attribute' or 'tags'. This determines where nested keys go.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected static function _fromArrayRecursive(&$node, &$array, $format = 'attribute') {
|
protected function _fromArray(&$dom, &$node, &$data, $format) {
|
||||||
if (empty($array) || !is_array($array)) {
|
if (empty($data) || !is_array($data)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach ($array as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
if (is_string($key)) {
|
if (is_string($key)) {
|
||||||
if (!is_array($value)) {
|
if (!is_array($value)) {
|
||||||
if (is_bool($value)) {
|
if (is_bool($value)) {
|
||||||
|
@ -150,12 +142,15 @@ class Xml {
|
||||||
$value = '';
|
$value = '';
|
||||||
}
|
}
|
||||||
if ($key[0] !== '@' && $format === 'tags') {
|
if ($key[0] !== '@' && $format === 'tags') {
|
||||||
$node->addChild($key, $value);
|
$child = $dom->createElement($key, $value);
|
||||||
|
$node->appendChild($child);
|
||||||
} else {
|
} else {
|
||||||
if ($key[0] === '@') {
|
if ($key[0] === '@') {
|
||||||
$key = substr($key, 1);
|
$key = substr($key, 1);
|
||||||
}
|
}
|
||||||
$node->addAttribute($key, $value);
|
$attribute = $dom->createAttribute($key);
|
||||||
|
$attribute->appendChild($dom->createTextNode($value));
|
||||||
|
$node->appendChild($attribute);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($key[0] === '@') {
|
if ($key[0] === '@') {
|
||||||
|
@ -164,21 +159,23 @@ class Xml {
|
||||||
if (array_keys($value) === range(0, count($value) - 1)) { // List
|
if (array_keys($value) === range(0, count($value) - 1)) { // List
|
||||||
foreach ($value as $item) {
|
foreach ($value as $item) {
|
||||||
if (array_key_exists('@', $item)) {
|
if (array_key_exists('@', $item)) {
|
||||||
$child = $node->addChild($key, (string)$item['@']);
|
$child = $dom->createElement($key, (string)$item['@']);
|
||||||
unset($item['@']);
|
unset($item['@']);
|
||||||
} else {
|
} else {
|
||||||
$child = $node->addChild($key);
|
$child = $dom->createElement($key);
|
||||||
}
|
}
|
||||||
self::_fromArrayRecursive($child, $item, $format);
|
self::_fromArray($dom, $child, $item, $format);
|
||||||
|
$node->appendChild($child);
|
||||||
}
|
}
|
||||||
} else { // Struct
|
} else { // Struct
|
||||||
if (array_key_exists('@', $value)) {
|
if (array_key_exists('@', $value)) {
|
||||||
$child = $node->addChild($key, (string)$value['@']);
|
$child = $dom->createElement($key, (string)$value['@']);
|
||||||
unset($value['@']);
|
unset($value['@']);
|
||||||
} else {
|
} else {
|
||||||
$child = $node->addChild($key);
|
$child = $dom->createElement($key);
|
||||||
}
|
}
|
||||||
self::_fromArrayRecursive($child, $value, $format);
|
self::_fromArray($dom, $child, $value, $format);
|
||||||
|
$node->appendChild($child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue