mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Arrays with @ as key now is the value of tag in Xml.
This commit is contained in:
parent
f777bd983f
commit
f8b4d92a34
2 changed files with 57 additions and 5 deletions
|
@ -57,7 +57,12 @@ class Xml {
|
|||
throw new Exception(__('The key of input must be alphanumeric'));
|
||||
}
|
||||
if (is_array($input[$key])) {
|
||||
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . ' />');
|
||||
if (array_key_exists('@', $input[$key])) {
|
||||
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key]['@'] . '</' . $key . '>');
|
||||
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 . '>');
|
||||
|
@ -99,11 +104,21 @@ class Xml {
|
|||
}
|
||||
if (array_keys($value) === range(0, count($value) - 1)) { // List
|
||||
foreach ($value as $item) {
|
||||
$child = $node->addChild($key);
|
||||
if (array_key_exists('@', $item)) {
|
||||
$child = $node->addChild($key, (string)$item['@']);
|
||||
unset($item['@']);
|
||||
} else {
|
||||
$child = $node->addChild($key);
|
||||
}
|
||||
self::_fromArrayRecursive($child, $item, $format);
|
||||
}
|
||||
} else { // Struct
|
||||
$child = $node->addChild($key);
|
||||
if (array_key_exists('@', $value)) {
|
||||
$child = $node->addChild($key, (string)$value['@']);
|
||||
unset($value['@']);
|
||||
} else {
|
||||
$child = $node->addChild($key);
|
||||
}
|
||||
self::_fromArrayRecursive($child, $value, $format);
|
||||
}
|
||||
}
|
||||
|
@ -154,7 +169,7 @@ class Xml {
|
|||
if (empty($data)) {
|
||||
$data = $asString;
|
||||
} elseif (!empty($asString)) {
|
||||
$data['value'] = $asString;
|
||||
$data['@'] = $asString;
|
||||
}
|
||||
|
||||
$name = $xml->getName();
|
||||
|
|
|
@ -103,6 +103,11 @@ class XmlTest extends CakeTestCase {
|
|||
$this->assertEqual($obj->getName(), 'tag');
|
||||
$this->assertEqual((string)$obj, '');
|
||||
|
||||
$xml = array('tag' => array('@' => 'value'));
|
||||
$obj = Xml::fromArray($xml);
|
||||
$this->assertEqual($obj->getName(), 'tag');
|
||||
$this->assertEqual((string)$obj, 'value');
|
||||
|
||||
$xml = array(
|
||||
'tags' => array(
|
||||
'tag' => array(
|
||||
|
@ -175,6 +180,38 @@ class XmlTest extends CakeTestCase {
|
|||
$obj = Xml::fromArray($xml, 'tags');
|
||||
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
|
||||
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
||||
|
||||
$xml = array(
|
||||
'tags' => array(
|
||||
'tag' => array(
|
||||
array(
|
||||
'@id' => '1',
|
||||
'name' => 'defect',
|
||||
'@' => 'Tag 1'
|
||||
),
|
||||
array(
|
||||
'@id' => '2',
|
||||
'name' => 'enhancement'
|
||||
),
|
||||
),
|
||||
'@' => 'All tags'
|
||||
)
|
||||
);
|
||||
$obj = Xml::fromArray($xml, 'tags');
|
||||
$xmlText = '<' . '?xml version="1.0"?><tags>All tags<tag id="1">Tag 1<name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
|
||||
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
||||
|
||||
$xml = array(
|
||||
'tags' => array(
|
||||
'tag' => array(
|
||||
'id' => 1,
|
||||
'@' => 'defect'
|
||||
)
|
||||
)
|
||||
);
|
||||
$obj = Xml::fromArray($xml);
|
||||
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1">defect</tag></tags>';
|
||||
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -385,7 +422,7 @@ class XmlTest extends CakeTestCase {
|
|||
'root' => array(
|
||||
'tag' => array(
|
||||
'@id' => 1,
|
||||
'value' => 'defect'
|
||||
'@' => 'defect'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue