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

Source for file tple_exports.class.php

Documentation is available at tple_exports.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class TPLE_Exports
  9.  *
  10.  * @package tangra_lib
  11.  * @subpackage  web_site
  12.  */
  13.  
  14.  
  15. /**
  16.  *
  17.  */
  18. require_once(TANGRA_MAIN_DIR.'exceptions/te_var_already_exists.class.php');
  19. /**
  20.  *
  21.  */
  22. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_not_exists.class.php');
  23.  
  24.  
  25.  
  26. /**
  27.  * Container for variables that will be used by template engine
  28.  *
  29.  * @package tangra_lib
  30.  * @subpackage  web_site
  31.  */
  32. class Tple_Exports extends Tangra_Class {
  33.     /**
  34.      * Array of variables and their values
  35.      *
  36.      * @var unknown_type 
  37.      * @internal
  38.      */
  39.     private $pairs = array();
  40.  
  41.  
  42.     /**
  43.      * Constructor
  44.      *
  45.      */
  46.     function __construct({
  47.         $this->pairs = array();
  48.     }
  49.  
  50.  
  51.     /**
  52.      * Adds new variable and value
  53.      *
  54.      * Will throw TE_Var_Already_Exists if variable with same name already exists
  55.      *
  56.      * @param string $var_name Variable name
  57.      * @param mixed $value Variable value
  58.      * @throws TE_Var_Already_Exists
  59.      */
  60.     public function add_pair($var_name$value{
  61.         if (!array_key_exists($var_name$this->pairs)) {
  62.             $this->pairs[$var_name$value;
  63.         else {
  64.             throw new TE_Var_Already_Exists($var_name);
  65.         }
  66.     }
  67.  
  68.  
  69.     /**
  70.      * Merges variables of current TPLE_Exports with those provided by <var>$exports</var>
  71.      *
  72.      * @param Tple_Exports $exports 
  73.      */
  74.     public function merge(Tple_Exports $exports{
  75.         $new_exports $exports->get_pairs();
  76.         foreach($new_exports as $key => $value{
  77.             $this->add_pair($key$value);
  78.         }
  79.     }
  80.  
  81.  
  82.     /**
  83.      * Returns all variable-value pairs
  84.      *
  85.      * @return array 
  86.      */
  87.     public function &get_pairs({
  88.         return $this->pairs;
  89.     }
  90.  
  91.  
  92.     /**
  93.      * Changes value for pair
  94.      *
  95.      * Please note that if you need this function it is indicator that there is something
  96.      * wrong with your code - it is not normal to add variable and later to need to change its value.
  97.      * Design your code in a way that exports (add_pair()) are added just before parsing the template
  98.      *
  99.      * @param string $var_name Variable name
  100.      * @param mixed $value Variable value
  101.      */
  102.     public function update_pair_value($var_name$value{
  103.         if (array_key_exists($var_name$this->pairs)) {
  104.             $this->pairs[$var_name$value;
  105.         else {
  106.             throw new TE_Var_Already_Exists($var_name);
  107.         }
  108.     }
  109.  
  110.  
  111.     /**
  112.      * Gets value of pair
  113.      *
  114.      * If $forgiving is set to false will throw exception TE_Key_Not_Exists if such key does not exist.
  115.      * If $forgiving is set to true will return NULL if such key does not exist.
  116.      *
  117.      * @param string $key 
  118.      * @param boolean $forgiving 
  119.      * @return mixed 
  120.      * @throws TE_Key_Not_Exists
  121.      */
  122.     public function get_pair_value($key$forgiving true{
  123.         if ($this->key_exists($key)) {
  124.             $ret $this->pairs[$key];
  125.         else {
  126.             if ($forgiving{
  127.                 $ret NULL;
  128.             else {
  129.                 throw TE_Key_Not_Exists('Key not exists: '.$key);
  130.             }
  131.         }
  132.  
  133.         return $ret;
  134.     }
  135.  
  136.  
  137.     /**
  138.      * Checks if there is already added pair with key $key
  139.      *
  140.      * @param string $key 
  141.      * @return boolean 
  142.      */
  143.     public function key_exists($key{
  144.         if (array_key_exists($key$this->pairs)) {
  145.             $ret true;
  146.         else {
  147.             $ret false;
  148.         }
  149.  
  150.         return $ret;
  151.     }
  152. }