mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-12 06:56:24 +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'));
|
throw new Exception(__('The key of input must be alphanumeric'));
|
||||||
}
|
}
|
||||||
if (is_array($input[$key])) {
|
if (is_array($input[$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 . ' />');
|
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . ' />');
|
||||||
|
}
|
||||||
self::_fromArrayRecursive($simpleXml, $input[$key], $format);
|
self::_fromArrayRecursive($simpleXml, $input[$key], $format);
|
||||||
} else {
|
} else {
|
||||||
$simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key] . '</' . $key . '>');
|
$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
|
if (array_keys($value) === range(0, count($value) - 1)) { // List
|
||||||
foreach ($value as $item) {
|
foreach ($value as $item) {
|
||||||
|
if (array_key_exists('@', $item)) {
|
||||||
|
$child = $node->addChild($key, (string)$item['@']);
|
||||||
|
unset($item['@']);
|
||||||
|
} else {
|
||||||
$child = $node->addChild($key);
|
$child = $node->addChild($key);
|
||||||
|
}
|
||||||
self::_fromArrayRecursive($child, $item, $format);
|
self::_fromArrayRecursive($child, $item, $format);
|
||||||
}
|
}
|
||||||
} else { // Struct
|
} else { // Struct
|
||||||
|
if (array_key_exists('@', $value)) {
|
||||||
|
$child = $node->addChild($key, (string)$value['@']);
|
||||||
|
unset($value['@']);
|
||||||
|
} else {
|
||||||
$child = $node->addChild($key);
|
$child = $node->addChild($key);
|
||||||
|
}
|
||||||
self::_fromArrayRecursive($child, $value, $format);
|
self::_fromArrayRecursive($child, $value, $format);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,7 +169,7 @@ class Xml {
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
$data = $asString;
|
$data = $asString;
|
||||||
} elseif (!empty($asString)) {
|
} elseif (!empty($asString)) {
|
||||||
$data['value'] = $asString;
|
$data['@'] = $asString;
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = $xml->getName();
|
$name = $xml->getName();
|
||||||
|
|
|
@ -103,6 +103,11 @@ class XmlTest extends CakeTestCase {
|
||||||
$this->assertEqual($obj->getName(), 'tag');
|
$this->assertEqual($obj->getName(), 'tag');
|
||||||
$this->assertEqual((string)$obj, '');
|
$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(
|
$xml = array(
|
||||||
'tags' => array(
|
'tags' => array(
|
||||||
'tag' => array(
|
'tag' => array(
|
||||||
|
@ -175,6 +180,38 @@ class XmlTest extends CakeTestCase {
|
||||||
$obj = Xml::fromArray($xml, 'tags');
|
$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>';
|
$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);
|
$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(
|
'root' => array(
|
||||||
'tag' => array(
|
'tag' => array(
|
||||||
'@id' => 1,
|
'@id' => 1,
|
||||||
'value' => 'defect'
|
'@' => 'defect'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue