Browse files

Reworked _addData() method to correct a couple problems and simplify:

When data consists of nested, associative arrays, the keys that were outputted into XML were incorrect. When a top level element contained an array containing string elements, _addData() failed because it was expected the values of the array to be arrays themselves.
  • Loading branch information...
1 parent fb808c9 commit d4a7834db2257ce54a47ffe50cdb1b920e4890c9 @jenwachter jenwachter committed Feb 15, 2013
Showing with 21 additions and 25 deletions.
  1. +21 −25 library/Hal/Resource.php
View
46 library/Hal/Resource.php
@@ -287,33 +287,29 @@ public function setXML(SimpleXMLElement $xml)
*
* @param SimpleXMLElement $xml
* @param array $data
- * @param string $key
+ * @param string $keyOverride
*/
- protected function _addData(SimpleXMLElement $xml, $data, $key = null)
+ protected function _addData(SimpleXMLElement $xml, array $data, $keyOverride = null)
{
- if(null !== $key && !is_numeric($key)){
- $node = $xml->addChild($key);
- $this->_addData($node, $data);
- } else {
- foreach ($data as $_key => $value) {
- if(!is_numeric($_key) && is_array($value)){
- foreach ($value as $v) {
- $c = $xml->addChild($_key)->addChild($_key);
- foreach ($v as $name => $value) {
- $c->addChild($name, $value);
- }
- }
- }
- elseif(!is_numeric($_key) && !is_array($value) && $value){
- if($key && !is_numeric($key)){
- $_k = $key;
- } else {
- $_k = $_key;
- }
- $xml->addChild($_k, $value);
- } elseif(is_array($value)){
- $this->_addData($xml, $value, $_key);
- }
+ foreach ($data as $key => $value) {
+
+ // alpha-numeric key => array value
+ if(!is_numeric($key) && is_array($value)){
+ $c = $xml->addChild($key);
+ $this->_addData($c, $value, $key);
+ }
+
+ // alpha-numeric key => non-array value
+ elseif(!is_numeric($key) && !is_array($value)){
+ $xml->addChild($key, $value);
+
+ // numeric key => array value
+ } elseif(is_array($value)){
+ $this->_addData($xml, $value);
+
+ // numeric key => non-array value
+ } else {
+ $xml->addChild($keyOverride, $value);
}
}
}

0 comments on commit d4a7834

Please sign in to comment.