<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 */

/**
 * ConfigFile
 *
 * @access      public
 * @package     yana
 * @subpackage  io
 *
 * @name configfile
 */
class ConfigFile extends SecureFileStream
{
    /**#@+
     * @ignore
     */

    /* private bool */      var $is_ready = false;
    /* private array */     var $value    = array();

    /**#@-*/

    /**
     * get
     *
     * @access public
     * @param string $key (optional)
     * @return mixed
     *
     * @name configfile::get()
     */
    function get($key = "*")
    {
        settype($key, "string");

        if (!$this->is_ready) {
            $this->init();
        }

        if (empty($key) || $key == "*") {
            return $this->value;
        } else {
            return Hashtable::get($this->value, $key);
        }
    }

    /**
     * insert an array into the file
     *
     * This merges the current file content with the 
     * provided array. New values will replace old ones.
     *
     * @access  public
     * @param   array  $REF
     * @return  boolean
     *
     * @name configfile::insert()
     */
    function insert($REF)
    {
        settype($REF, "array");
        $this->value = mergeArrays($this->value,$REF);
        $this->content = makeConfig($this->value,null,CASE_MIXED);
        return true;
    }

    /**
     * set the content of the file
     *
     * Replaces the file content with the provided array.
     *
     * @access  public
     * @param   array  $REF
     * @return  boolean
     *
     * @name configfile::set()
     */
    function set($REF)
    {
        settype($REF, "array");
        $this->value = $REF;
        $this->content = makeConfig($this->value,null,CASE_MIXED);
        return true;
    }

    /**
     * reset the file
     *
     * Changes to the file will not be safed unless you
     * explicitely call $configFile->write().
     * So if you want or need to revert your changes just call
     * $configFile->reset() and all will be fine.
     *
     * @access public
     *
     * @name configfile::reset()
     */
    function reset() {
        $this->content = makeConfig($this->value,null,CASE_MIXED);
        $this->options=$this->attributes;
    }

    /**
     * get a string representation
     *
     * @access public
     * @return string
     *
     * @name configfile::toString()
     */
    function toString()
    {
        if (!$this->exists()) {
            return "Config-file ".$this->filename." does not exist";
        } else if ($this->isEmpty()) {
            return "Config-file ".$this->filename." is wether empty or not loaded";
        } else {
            return makeConfig($this->value,null,CASE_MIXED);
        }
    }

    /**
     * initialize file contents
     *
     * You should always call this before anything else.
     *
     * @access public
     * @return mixed
     *
     * @name configfile::init()
     */
    function init()
    {

     if ($this->isEmpty()) {
         $this->read();
     }
     if (!$this->exists()) {
         return false;
     }

     $this->value = getConfigFile($this->source,CASE_MIXED);
     $this->is_ready = true;
     return $this->value;

    }

    /**
     * get the number of elements
     *
     * This returns how many elements can
     * be found inside the array at position
     * $key. If $key points to a non-existing value,
     * or an empty array, the function returns 0.
     *
     * @access  public
     * @param   string  $key  (optional)
     * @return  int
     *
     * @name configfile::length()
     */
    function length($key = "*")
    {
        settype($key, "string");
        if ($key == "*") {
            return count($this->value);
        } else {
            $result =& Hashtable::get($this->value, $key);
            if (is_null($result)) {
                return 0;
            } else {
                assert('is_array($result);');
                return count($result);
            }
        }
    }

    /**
     * remove an entry from the file
     *
     * @access  public
     * @param   string  $key  (optional)
     * @return  boolean
     *
     * @name configfile::remove()
     */
    function remove($key = "*")
    {
        settype($key, "string");

        if (Hashtable::remove($this->value, $key)) {
            $this->content = makeConfig($this->value,null,CASE_MIXED);
            return true;
        } else {
            return false;
        }

    }
    
    /**
     * test if a certain value exists
     *
     * @param string $key (optional)
     * @access public
     * @return boolean
     *
     * @name configfile::exists()
     */
    function exists($key = '*')
    {
        assert('is_string($key);');
        settype($key, "string");

        if (empty($key) || $key == '*') {
            return parent::exists();
        } else {
            return Hashtable::exists($this->value, $key);
        }
        
    }

}

?>