tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file context.class.php

Documentation is available at context.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Context
  8.  *
  9.  * @package  tangra_lib
  10.  * @subpackage  core
  11.  */
  12.  
  13. /**
  14.  * Loading exception
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'exceptions/te_var_already_exists.class.php');
  17. /**
  18.  * Loading exception
  19.  */
  20. require_once(TANGRA_MAIN_DIR.'exceptions/te_var_not_exists.class.php');
  21. /**
  22.  * Loading exception
  23.  */
  24. require_once(TANGRA_MAIN_DIR.'exceptions/te_not_an_array.class.php');
  25.  
  26.  
  27. /**
  28.  * Container for variables.
  29.  *
  30.  * @package  tangra_lib
  31.  * @subpackage  core
  32.  * @see Web_Context, Vars_Manager
  33.  */
  34. class Context extends Tangra_Class {
  35.     /**
  36.      * Storage for the values
  37.      *
  38.      * @var array simple associative array
  39.      */
  40.     private $vars = array();
  41.  
  42.  
  43.     /**
  44.      * Constructor
  45.      *
  46.      * @param array $vars Simple associative array used to initialize private $vars.
  47.      * @throws  TE_Not_An_Array
  48.      */
  49.     function __construct(&$vars NULL{
  50.         if ($vars{
  51.             if (is_array($vars)) {
  52.                 $this->vars = $vars;
  53.             else {
  54.                 throw new TE_Not_An_Array($vars);
  55.             }
  56.         }
  57.     }
  58.  
  59.  
  60.     /**
  61.      * Adds new variable.
  62.      *
  63.      * @param string $name 
  64.      * @param any $value 
  65.      */
  66.     public function add_var($name&$value{
  67.         if (!array_key_exists($name$this->vars)) {
  68.             $this->_set_var($name$value);
  69.         else {
  70.             throw new TE_Var_Already_Exists($name);
  71.         }
  72.     }
  73.  
  74.  
  75.     /**
  76.      * Sets variable.
  77.      * Throws exception if variable with name provided with $name does not exists.
  78.      *
  79.      * @param string $name 
  80.      * @param any $value 
  81.      * @throws TE_Var_Not_Exists
  82.      */
  83.     public function set_var($name$value{
  84.         if (array_key_exists($name$this->vars)) {
  85.             $this->_set_var($name$value);
  86.         else {
  87.             throw new  TE_Var_Not_Exists($name);
  88.         }
  89.     }
  90.  
  91.  
  92.     /**
  93.      * Gets variable as reference. Use this function if you intend to change the value.
  94.      *
  95.      * @param string $name 
  96.      * @return any 
  97.      * @throws TE_Var_Not_Exists
  98.      */
  99.     public function &get_var($name{
  100.         if (array_key_exists($name$this->vars)) {
  101.             return $this->vars[$name];
  102.         else {
  103.             throw new TE_Var_Not_Exists($name);
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Query variable. Similar to get_var() but returns copy of the variable, not reference. Use this function instead of get_var() if you are not intending to change the value.
  109.      *
  110.      * @param string $name 
  111.      * @return any 
  112.      */
  113.     public function query_var($name{
  114.         if (array_key_exists($name$this->vars)) {
  115.             return $this->vars[$name];
  116.         else {
  117.             throw new TE_Var_Not_Exists($name);
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * Checks if var exists (it is already set)
  123.      *
  124.      * @param string $key 
  125.      * @return boolean 
  126.      */
  127.     public function vars_exists($key{
  128.         $ret false;
  129.         if (array_key_exists($key$this->vars)) {
  130.             $ret true;
  131.         }
  132.  
  133.         return $ret;
  134.     }
  135.  
  136.  
  137.     /**
  138.      * Actual set of the variable
  139.      *
  140.      * @param string $name 
  141.      * @param any $value 
  142.      * @access  private
  143.      */
  144.     private function _set_var($name&$value{
  145.         $this->vars[$name&$value;
  146.     }
  147. }