Location: PHPKode > scripts > PAX > pax/core/classes/Config.class.php
<?php

/**
 * Page DocBlock definition
 * @package org.zadara.marius.pax
 */


/**
 * Class definition for a hash-map.
 * This class will contain associations between variables and theirs value. 
 * 
 * @author Marius Zadara <hide@address.com>
 * @category Classes
 * @copyright (C) 2008-2009 Marius Zadara
 * @license Free for non-comercial use
 * @package org.zadara.marius.pax
 * @final 
 * @see PAXObject
 * @see IConfig
 * @version 6.0
 * @since 5.0 
 */
final class Config extends PAXObject implements IConfig 
{
	/**
	 * The hash-map associations array.
	 *
	 * @access private
	 * @var array
	 */
	private $hashmap;

	/**
	 * Class constructor.
	 *
	 * @access public
	 */
	public function __construct()
	{
		// call the parent's constructor first
		parent::__construct();
		
		// init the hash-map
		// at this moment set it to NULL to preserve memory
		$this->hashmap = null;
	}
	
	/**
	 * Method to set a variable to a value in the hash-map.
	 * If the key already exists, the value wil be updated.
	 *
	 * @access public
	 * @param string <b>$variable</b> The variable name
	 * @param mixed <b>$value</b> The variable value
	 * @return void
	 */
	public function set($variable, $value)
	{
		// check the hash-map	
		if (is_null($this->hashmap))
			$this->hashmap = array();
		
		// set the variable to its value
		$this->hashmap[$variable] = $value;
	}
	
	/**
	 * Method to get the variabile value from the hash-map.
	 *
	 * @access public
	 * @param string <b>$variable</b> The variable name
	 * @param mixed <b>$defaultValue</b> The variable default value (used in case the variabile has not been found)
	 * @return mixed The value of the variables as defined 
	 */
	public function get($variable, $defaultValue=NULL)
	{
		// if the hash-map has not been set, 
		// return the default value
		if (is_null($this->hashmap))
			return $defaultValue;
		
		// if the variable is not set in the hash-map,
		// return the default value
		if (!isset($this->hashmap[$variable]))
			return $defaultValue;
			
		// at this point, the variable is set
		// return its value from the hash-map
		return $this->hashmap[$variable];
	}	
	
	/**
	 * Method to return only the keys.
	 *
	 * @access public
	 * @return array The list of all the keys 
	 */
	public function getKeys()
	{
		if (is_null($this->hashmap))
			return false;

		if (!is_array($this->hashmap))
			return false;
			
		return array_keys($this->hashmap);	
	}

	
	/**
	 * Method to return only the values.
	 *
	 * @access public
	 * @return array The list of all the values
	 */
	public function getValues()
	{
		if (is_null($this->hashmap))
			return false;

		if (!is_array($this->hashmap))
			return false;
			
		return array_values($this->hashmap);			
	}

	
	/**
	 * Method to delete a key.
	 *
	 * @access public
	 * @param string <b>$key</b> The name of the key
	 * @return boolean True/False if the key has been deleted
	 */
	public function deleteKey($key)
	{
		if (is_null($this->hashmap))
			return false;
			
		if (!is_array($this->hashmap))
			return false;
			
		if (isset($this->hashmap[$key]))
		{
			unset($this->hashmap[$key]);
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * Method to delete the keys matching a pattern.
	 * The match is performed case insensitive.
	 *
	 * @access public
	 * @param string <b>$pattern</b> The pattern to use when searching for the keys
	 * @return boolean True/False if the key(s) has been deleted
	 */
	public function deleteKeysLike($pattern)
	{
		if (is_null($this->hashmap))
			return false;
			
		if (!is_array($this->hashmap))
			return false;
		
		$keysDeleted = false;

		// parse the hash-map list
		foreach ($this->hashmap as $keyName => $keyValue)
		{
			// search the pattern inside the key name
			if (stripos($keyName, $pattern) !== false)
			{
				// if found, then try to delete the key
				if ($this->deleteKey($keyName))
					$keysDeleted = true;
			}
		}

		return $keysDeleted;
	}
	
	
	/**
	 * Method to get the key-value pairs from the hash-map.
	 * Due to construction of the hash-map, will return the content of te hash-map.
	 * 
	 * @access public
	 * @return array The object's content
	 */
	public function getKeyValuePairs()
	{
		if (is_null($this->hashmap))
			return false;
			
		if (!is_array($this->hashmap))
			return false;

		return $this->hashmap;
	}
	
	
	/**
	 * Method to search if a key exists.
	 * 
	 * @access public 
	 * @param string <b>$key</b> The key searched
	 * @return boolean True/False if the key has been found or not
	 */
	public function keyExists($key)
	{
		if (is_null($this->hashmap))
			return false;
			
		if (!is_array($this->hashmap))
			return false;

		return isset($this->hashmap[$key]);
	}
	
	
	
	
	/**
	 * Class destructor.
	 *
	 * @access public
	 */
	function __destruct()
	{
		// nothing special to do at this moment
	}
}


?>
Return current item: PAX