<?php 
class ErgophoneXml extends XMLWriter {

    private $name;
    private $createFile;
    public function __construct($name, $dir, $createFile = true) {
        
        $this->setName($name);
        $this->setCreateFile($createFile);
        
        if ($this->createFile) {
            $this->openURI($dir."/".$name.".xml");
        } else {
            $this->openURI('php://output');
        }
        
        $this->startDocument(/*'1.0',"UTF-8"*/);
        $this->startElement($this->name);
    }
    
    public function addItems($items) {
        /* NOTE : 
        *      if you need more items with the same name in one parent item add the extension __{suffix} to the array key 
        *      everything after the dubble unterline will be removed.
        */      
        foreach($items as $name => $value) {
            if (strpos($name,'__') !== false) {
                $name = substr($name,0,(strpos($name,'__')));
            } 
            $this->startElement($name);
            if (!is_array($value)) {
                $this->text($value);
            } else {
                if ($value !== null) {
                    $this->addItems($value);
                }
            }
            
            $this->endElement();
        }
        
    }
    
    public function writeXmlFile(){
    
        $this->endDocument(); // $name tag 
     
        $this->flush();
    }
    
    public function setName($name) {
        $this->name = $name;
    }
    
    public function setCreateFile($createFile) {
        $this->createFile = $createFile;
    }
    
    public static function  xmlToArray($raw_data) {

        $xml = new SimpleXMLElement($raw_data);

        $attributes = get_object_vars($xml);

        foreach($attributes as $key => $attribute) {
            if ($attribute instanceof SimpleXMLElement) {
                $attributes[$key] = self::simpleXmlElementToArray($attribute);
            }
            if( is_array($attribute)) {
                foreach($attribute as $attributeKey => $attributeValue) {
                    if ( $attributeValue instanceof SimpleXMLElement ) {
                        $attribute[$attributeKey] = self::simpleXmlElementToArray($attributeValue);
                    }
                }
                $attributes[$key] = $attribute;
                
            } 
            
        }
        return $attributes;
    }
    
    public static function  simpleXmlElementToArray(SimpleXMLElement $xml) {
        
        $attributes = get_object_vars($xml);
        
        foreach ($attributes as $key => $attribute) {
            if ($attribute instanceof SimpleXMLElement) {
                $attributes[$key] = self::simpleXmlElementToArray($attribute);
            }
            if( is_array($attribute)) {
                foreach($attribute as $attributeKey => $attributeValue) {
                    if ( $attributeValue instanceof SimpleXMLElement ) {
                        $attribute[$attributeKey] = self::simpleXmlElementToArray($attributeValue);
                    }
                }
                $attributes[$key] = $attribute;
                
            }
        }
        
        return $attributes;
    }
    
}
?>