<?php
/**
 * A class for storing and retrieving global variables.
 */
class registry
{
    /**
     * @var array The variables.
     */
    protected static $data = array();

    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Gets one or all registry variables.
     * 
     * @param string (optional) $name The variable name.
     * @throws outofrangeexception If the variable does not exist.
     * @return mixed The value.
     */
    public static function get($name = null)
    {
        if ($name === null)
        {
            return self::$data;
        }
        
        if (!self::is_set($name))
        {
            throw new outofrangeexception('Variable ' . $name . ' does not exist.');
        }
        
        return self::$data[$name];
    }

    /**
     * Sets one or more registry variables.
     * 
     * @param string $name The variable name.
     * @param mixed $value The value.
     * -
     * @param array $name The variables.
     */
    public static function set($name, $value = null)
    {
        if (is_array($name))
        {
            foreach ($name as $k => $v)
            {
                self::$data[$k] = $v;
            }
        }
        else
        {
            self::$data[$name] = $value;
        }
    }

    /**
     * Checks if a registry variable is set or not.
     * 
     * @param string $name The variable name.
     * @return boolean True if the variable exists, else false.
     */
    public static function is_set($name)
    {
        return array_key_exists($name, self::$data);
    }
    
    /**
     * Clears one or all registry variables.
     * 
     * @param string (optional) $name The variable name.
     */
    public static function clear($name = null)
    {
        if ($name === null)
        {
            self::$data = array();
        }
        else
        {
            unset(self::$data[$name]);
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}