Skip to content

Commit

Permalink
Support to namespaces in Xml::fromArray().
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Sep 12, 2010
1 parent c8c20ea commit 2e7851c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 16 deletions.
61 changes: 45 additions & 16 deletions cake/libs/xml.php
Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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.
*
Expand Down
56 changes: 56 additions & 0 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 2e7851c

Please sign in to comment.