Skip to content
Permalink
Browse files

Support to namespaces in Xml::fromArray().

  • Loading branch information...
jrbasso committed Sep 11, 2010
1 parent c8c20ea commit 2e7851c2be5258b76cb9ac685be52d1c19795330
Showing with 101 additions and 16 deletions.
  1. +45 −16 cake/libs/xml.php
  2. +56 −0 cake/tests/cases/libs/xml.test.php
@@ -183,6 +183,12 @@ protected function _fromArray(&$dom, &$node, &$data, $format) {
} elseif ($value === null) {
$value = '';
}
$isNamespace = strpos($key, 'xmlns:');
$nsUri = null;
if ($isNamespace !== false) {
$node->setAttributeNS('http://www.w3.org/2000/xmlns/', $key, $value);
continue;
}
if ($key[0] !== '@' && $format === 'tags') {
$child = $dom->createElement($key, $value);
$node->appendChild($child);
@@ -200,24 +206,12 @@ protected function _fromArray(&$dom, &$node, &$data, $format) {
}
if (array_keys($value) === range(0, count($value) - 1)) { // List
foreach ($value as $item) {
if (array_key_exists('@', $item)) {
$child = $dom->createElement($key, (string)$item['@']);
unset($item['@']);
} else {
$child = $dom->createElement($key);
}
self::_fromArray($dom, $child, $item, $format);
$node->appendChild($child);
$data = compact('dom', 'node', 'key', 'format');
$data['value'] = $item;
self::__createChild($data);
}
} else { // Struct
if (array_key_exists('@', $value)) {
$child = $dom->createElement($key, (string)$value['@']);
unset($value['@']);
} else {
$child = $dom->createElement($key);
}
self::_fromArray($dom, $child, $value, $format);
$node->appendChild($child);
self::__createChild(compact('dom', 'node', 'key', 'value', 'format'));
}
}
} else {
@@ -226,6 +220,41 @@ protected function _fromArray(&$dom, &$node, &$data, $format) {
}
}
/**
* Helper to _fromArray(). It will create childs of arrays
*
* @param array $data Array with informations to create childs
* @return void
*/
private function __createChild($data) {
extract($data);
$childNS = $childValue = null;
if (is_array($value)) {
if (isset($value['@'])) {
$childValue = (string)$value['@'];
unset($value['@']);
}
if (isset($value['xmlns:'])) {
$childNS = $value['xmlns:'];
unset($value['xmlns:']);
}
} elseif (!empty($value) || $value === 0) {
$childValue = (string)$value;
}
if ($childValue) {
$child = $dom->createElement($key, $childValue);
} else {
$child = $dom->createElement($key);
}
if ($childNS) {
$child->setAttribute('xmlns', $childNS);
}
self::_fromArray($dom, $child, $value, $format);
$node->appendChild($child);
}
/**
* Returns this XML structure as a array.
*
@@ -606,6 +606,62 @@ public function testNamespace() {
$xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
$this->assertEqual(Xml::toArray($xmlResponse), $expected);
$xml = array(
'root' => array(
'ns:attr' => array(
'xmlns:ns' => 'http://cakephp.org',
'@' => 1
)
)
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$xml = array(
'root' => array(
'tag' => array(
'xmlns:pref' => 'http://cakephp.org',
'pref:item' => array(
'item 1',
'item 2'
)
)
)
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns:pref="http://cakephp.org"><pref:item>item 1</pref:item><pref:item>item 2</pref:item></tag></root>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$xml = array(
'root' => array(
'tag' => array(
'xmlns:' => 'http://cakephp.org'
)
)
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$xml = array(
'root' => array(
'xmlns:' => 'http://cakephp.org'
)
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
$xml = array(
'root' => array(
'xmlns:ns' => 'http://cakephp.org'
)
);
$expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
$xmlResponse = Xml::fromArray($xml);
$this->assertEqual(str_replace(array("\r", "\n"), '', $xmlResponse->asXML()), $expected);
}
/**

0 comments on commit 2e7851c

Please sign in to comment.
You can’t perform that action at this time.