<?php

/**
 * Třída pro práci s XML
 *
 * @author Milan Lempera
 * @package Classes
 * @copyright Web-systems.cz  2009
 *
 */

class Xml {

  /**
   *	Funkce pro načtení nastavení z XML souborů
   *	@var  $file, $xmlPath, array $elements
   *	@return	array / false
   */
  function fileToArray($file, $xmlPath, array $elements) {
    if (file_exists($file)) {
      include $file;
      $xml = new SimpleXMLElement($xml_string);
      if ($xml == false)
      return false;

      $my_root = $xml->$xmlPath;
      $result = array();
      foreach($elements as $name => $type) {
        switch ($type){
          case 'string':
            $result[$name] = ( string )  $my_root->$name;
            break;

          case 'int':
          case 'integer':
            $result[$name] = ( int )  $my_root->$name;
            break;

          case 'real':
            $result[$name] = ( real )  $my_root->$name;
            break;

          case 'double':
            $result[$name] = ( double )  $my_root->$name;
            break;

          case 'float':
            $result[$name] = ( float )  $my_root->$name;
            break;

          case 'bool':
          case 'boolean':
            $result[$name] = ( bool )  $my_root->$name;
            break;

          case 'object':
            $result[$name] = ( object )  $my_root->$name;
            break;

          default:
            $result[$name] = $my_root->$name;
            break;
        }
      }
      return $result;
    }
    else
    return false;
  }

  function arrayToFile($file, $xmlRoot = 'root', $xmlPath = 'path',array $elements){

    $newXML = '<?xml version="1.0" encoding="UTF-8" ?><'.$xmlRoot.' />';

    $xml = new SimpleXMLElement($newXML);
    $child = $xml->addChild($xmlPath);
    foreach($elements as $key => $value) {
      $child->addChild($key,$value);
    }
    return file_put_contents($file, $xml->asXML());
  }


}
?>