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

/**
 * Registry
 *
 * @access     public
 * @package    yana
 * @subpackage vdrive
 */
class Registry extends VDrive
{

    /**
     * retrieves var from registry
     *
     * @access  public
     * @param   string  $key  (optional)
     * @return  mixed
     */ 
    function getVar($key = "*")
    {
        assert('is_string($key);');
        settype($key, "string");

        if (empty($key) || $key == "*") {
            return $this->options;
        } else if (strspn($key,'<')==1) {
            switch (strtoupper($key)) {

                case '<BOOLEAN>':
                    if (is_array($this->options)) {
                        foreach ($this->options as $a => $b) {
                            $RESULT[$a] = true;
                        }
                    } else {
                        $RESULT = true;
                    }
                    return $RESULT;
                break;

                case '<STRING>':
                    if (is_array($this->options)) {
                        foreach ($this->options as $a => $b) {
                            if (!is_array($b)) {
                                $RESULT[$a] = $b;
                            } else {
                                $RESULT[$a] = true;
                            }
                        }
                    } else {
                      $RESULT = $this->options;
                    }
                    return $RESULT;
                break;

                case '<ARRAY>':
                    if (is_array($this->options)) {
                        foreach ($this->options as $a => $b)
                        {
                            if (!is_array($b)) {
                                $RESULT[$a] = array();
                            } else {
                                $RESULT[$a] = $b;
                            }
                        }
                    } else {
                      $RESULT = array();
                    }
                    return $RESULT;
                break;

                default:
                    return false;
                break;

            } /* end switch */
      } else {
          $result =& Hashtable::get($this->options, $key);
          if (is_null($result)) {
              return false;
          } else {
              return $result;
          }
      } /* end if */

    } /* end getVar */

    /**
     * sets var on registry
     *
     * @access  public
     * @param   string  $key
     * @param   mixed   $value
     * @return  boolean
     */ 
    function setVar($key, $value)
    {
        settype($key, "string");

        if ($key == "" || $key == "*") {
            $this->options =& $value;
            return true;
        } else {
            return Hashtable::set($this->options, $key, $value);
        }

    }

    /**
    * merges the value at adresse $key with the provided array data
     *
     * @access  public
     * @param   string  $key
     * @param   array   $array
     * @return  boolean
     */ 
    function merge($key, $array)
    {
        assert('is_string($key);');
        assert('is_array($array);');
        settype($key, "string");
        settype($array, "array");
        
        if ($key == "" || $key == "*") {
            $this->options = mergeArrays($this->options, $array);
            return true;
        } else {
            $options =& Hashtable::get($this->options, $key);
            if (is_null($options)) {
                return Hashtable::set($this->options, $key, $array);
            } else {
                $options = mergeArrays($options, $array);
                return true;
            }
        }
    }

    /**
     * removes var from registry
     *
     * @access  public
     * @param   string  $key
     * @return  boolean
     */ 
    function unsetVar($key)
    {
        assert('is_string($key);');
        settype($key, "string");
        
        if ($key == "" || $key == "*") {
            $this->options = $this->attributes;
            return true;
        } else {
            return Hashtable::remove($this->options, $key);
        }

    }
    
    /**
     * sets the type of a var on registry
     *
     * @access public
     * @param string $key
     * @param string $type
     * @return boolean
     */ 
    function setType($key,$type)
    {
        assert('is_string($key);');
        assert('is_string($type);');
        settype($key, "string");
        settype($type, "string");
        return Hashtable::setType($this->options, $key, $type);
    }

}

?>