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

Source for file redirect_composer.class.php

Documentation is available at redirect_composer.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Redirect_Composer
  8.  *
  9.  * @package tangra_lib
  10.  * @subpackage  web_site
  11.  */
  12.  
  13.  
  14. /**
  15.  *
  16.  */
  17. require_once(TANGRA_MAIN_DIR.'exceptions/te_key_already_exists.class.php');
  18.  
  19.  
  20. /**
  21.  * Redirect_Composer's purpose is to compose address for redirection
  22.  *
  23.  * @see Redirect_Composer_Local
  24.  *
  25.  * @package tangra_lib
  26.  * @subpackage  web_site
  27.  */
  28. class Redirect_Composer extends Tangra_Class {
  29.     /**
  30.      * Target address
  31.      *
  32.      * @var string 
  33.      * @internal
  34.      */
  35.     private $target;
  36.  
  37.     /**
  38.      * Parameters that will be added to target address
  39.      *
  40.      * @var array 
  41.      * @internal
  42.      */
  43.     private $param_pairs;
  44.  
  45.  
  46.     /**
  47.      * Constructor
  48.      *
  49.      * @param string $target Target address
  50.      * @param array $param_pairs Parameters that will be added to target address ($key => $value pairs)
  51.      * @throws Tangra_Exception
  52.      */
  53.     function __construct($target$param_pairs array()) {
  54.         $this->target = $target;
  55.         if (is_array($param_pairs)) {
  56.             $this->param_pairs = $param_pairs;
  57.         else {
  58.             throw new Tangra_Exception('Parameter $params_pairs is not an array. Current value: '.$param_pairs);
  59.         }
  60.     }
  61.  
  62.  
  63.     /**
  64.      * Returns composed location that can be used directly by header()
  65.      *
  66.      * @return string 
  67.      */
  68.     public function get_location({
  69.         $params $this->get_params();
  70.         $ret 'Location: '.$this->get_target_address();
  71.  
  72.         return $ret;
  73.     }
  74.  
  75.  
  76.     /**
  77.      * Sets $target
  78.      *
  79.      * @param Web_Context $target 
  80.      * @internal
  81.      */
  82.     private function set_target(Web_Context $target{
  83.         $this->_target $target;
  84.     }
  85.  
  86.  
  87.     /**
  88.      * Returns target
  89.      *
  90.      * @return string 
  91.      */
  92.     public function get_target({
  93.         return $this->target;
  94.     }
  95.  
  96.  
  97.     /**
  98.      * Returns composed target address with parameters
  99.      *
  100.      * @return string 
  101.      */
  102.     public function get_target_address({
  103.         $params $this->get_params();
  104.         return $this->target.$params;
  105.     }
  106.  
  107.  
  108.     /**
  109.      * Add new parameter pair
  110.      *
  111.      * @param string $key Parameter name
  112.      * @param string $value Parameter value
  113.      * @throws TE_Key_Already_Exists
  114.      */
  115.     public function add_param_pair($key$value{
  116.         if (!array_key_exists($key$this->param_pairs)) {
  117.             $this->param_pairs[$key$value;
  118.         else {
  119.             throw new TE_Key_Already_Exists($key);
  120.         }
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Removes param pair
  126.      *
  127.      * @param string $key Name of the param pair
  128.      */
  129.     public function remove_param($key{
  130.         if (array_key_exists($key$this->param_pairs)) {
  131.             unset($this->param_pairs[$key]);
  132.         }
  133.     }
  134.  
  135.  
  136.     /**
  137.      * Checks if there is param pair with param name $key
  138.      *
  139.      * @param string $key 
  140.      * @return boolean 
  141.      */
  142.     public function param_exists($key{
  143.         return array_key_exists($key$this->param_pairs);
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Returns all paramteres
  149.      *
  150.      * @return array 
  151.      */
  152.     protected function get_params({
  153.         $params '';
  154.  
  155.         foreach($this->param_pairs as $key => $value{
  156.             if ($params{
  157.                 $params .= '&';
  158.             else {
  159.                 $params .= '?';
  160.             }
  161.  
  162.             $params .= $key.'='.$value;
  163.         }
  164.  
  165.         return $params;
  166.     }
  167. }