Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
90.00% covered (success)
90.00%
9 / 10
CRAP
98.81% covered (success)
98.81%
83 / 84
AssocXml
0.00% covered (danger)
0.00%
0 / 1
90.00% covered (success)
90.00%
9 / 10
29
98.81% covered (success)
98.81%
83 / 84
 __construct($rootName, $rowTagName)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 _addTag($dom, $element, $tag, $tagName)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 _addToGroupNode($dom, $element, $tag, $tagName)
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
14 / 14
 groupNodes($groupNodeName, $nodeNameArray, $parentNodeName = null)
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
7 / 7
 serialize($arrayOfAssocs)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 _parseRow($dom, $root, $row)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 _parseColumn($dom, $element, $tagName, $value)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
7 / 7
 _isAttribute($tagName)
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
14 / 14
 _addAttribute($element, $attributeName, $dom, $value)
0.00% covered (danger)
0.00%
0 / 1
4.00
93.75% covered (success)
93.75%
15 / 16
 colIsAttribute($attributeNodeName, $attributeName, $columnName = null)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
<?php
/**
 *
 */
namespace aae\serialize {
    /**
     * @author Axel Ancona Esselmann
     * @package aae\serialize
     */
    class AssocXml {
        protected $_rootName, $_rowTagName;
        protected $_attributes = array();
        protected $_groupedNodes = array();
        protected $_columnAttribute = array();
        public function __construct($rootName, $rowTagName) {
            $this->_rootName = $rootName;
            $this->_rowTagName = $rowTagName;
        }
        protected function _addTag($dom, $element, $tag, $tagName) {
            if (array_key_exists($tagName, $this->_groupedNodes)) {
                $this->_addToGroupNode($dom, $element, $tag, $tagName);
            } else {
                $element->appendChild($tag);
            }
        }
        protected function _addToGroupNode($dom, $element, $tag, $tagName) {
            $actualParentNodeName = $this->_groupedNodes[$tagName][1];
            #$parentNode = $element->parentNode;
            $parent = $element;
            $parentNodeName = $parent->tagName;
            if ($actualParentNodeName == $parentNodeName) {
                $groupNodeName = $this->_groupedNodes[$tagName][0];
                $nodeList = $parent->getElementsByTagName($groupNodeName);
                if ($nodeList->length < 1) {
                    $group = $dom->createElement($groupNodeName);
                    $parent->appendChild($group);
                } else {
                    $group = $nodeList->item(0);
                }
                $group->appendChild($tag);
            }
        }
        public function groupNodes($groupNodeName, $nodeNameArray, $parentNodeName = null) {
            if (is_null($parentNodeName)) {
                $parentNodeName = $this->_rowTagName;
            }
            foreach ($nodeNameArray as $nodeName) {
                $this->_groupedNodes[$nodeName] = array($groupNodeName, $parentNodeName);
            }
            return;
        }
        
        public function serialize($arrayOfAssocs) {
            $dom = new \DOMDocument();
            $root = $dom->createElement($this->_rootName);
            $dom->appendChild($root);
            foreach ($arrayOfAssocs as $row) {
                $this->_parseRow($dom, $root, $row);
            }
            return $dom->saveXML();
        }
        protected function _parseRow($dom, $root, $row) {
            $element = $dom->createElement($this->_rowTagName);
            $root->appendChild($element);
            foreach ($row as $tagName => $value) {
                $this->_parseColumn($dom, $element, $tagName, $value);
            }
        }
        protected function _parseColumn($dom, $element, $tagName, $value) {
            $result = null;
            if ($this->_isAttribute($tagName)) {
                $this->_addAttribute($element, $tagName, $dom, $value);
            } else {
                $tag = $dom->createElement($tagName, $value);
                $this->_addTag($dom, $element, $tag, $tagName);
            }
            return $result;
        }
        protected function _isAttribute($tagName) {
            $isAttribute = false;
            foreach ($this->_attributes as $key => $value) {
                if ($key == $tagName or $value == $tagName) {
                    $isAttribute = true;
                }
            }
            foreach ($this->_columnAttribute as $key => $value) {
                if ($key == $tagName or $value == $tagName) {
                    $isAttribute = true;
                }
            }
            if ($isAttribute) {
                #echo "$tagName is an attribute";
            }
            return $isAttribute;
        }
        protected function _addAttribute($element, $attributeName, $dom, $value) {
            if (array_key_exists($attributeName, $this->_columnAttribute)) {
                $attributeName = $this->_columnAttribute[$attributeName];
            }
            if (!array_key_exists($attributeName, $this->_attributes)) {
                return;
            }
            $attributeNodeName = $this->_attributes[$attributeName];
            $nodeList = $element->getElementsByTagName(            $attributeName);
            if ($nodeList->length < 1) {
                $node = $dom->createElement(                    $attributeNodeName);
                $element->appendChild($node);
            } else {
                $node = $nodeList->item(0);
            }
            $attribute = $dom->createAttribute($attributeName);
            $attribute->value = $value;
            $node->appendChild($attribute);
        }
    
        public function colIsAttribute($attributeNodeName, $attributeName, $columnName = null) {
            $this->_attributes[$attributeName] = $attributeNodeName;
            if (!is_null($columnName)) {
                $this->_columnAttribute[$columnName] = $attributeName;
            }
        }
    }
}