Source for file Input.class.php

Documentation is available at Input.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Http
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Http User Input Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Http
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Http User Input Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Singleton");
  34. gumbo_load ("Interface_Http_Input");
  35. gumbo_load ("Input");
  36.  
  37. class Gumbo_Http_Input implements Gumbo_Interface_Http_InputGumbo_Interface_Singleton {
  38.  
  39.     /** @var Gumbo_Interface_Http_Input $_instance */
  40.     private static $_instance null;
  41.     
  42.     /** @var Gumbo_Interface_Input[] $_input Input object associated with a key string */
  43.     private $_input = array ();
  44.     /** @var boolean $_ignore tells the system to ignore any input constraints */
  45.     private $_ignore = false;
  46.     
  47.     
  48.     
  49.     /**
  50.      * Constructor
  51.      */
  52.     private function __construct ({}
  53.     
  54.     /**
  55.      * Singleton Method
  56.      * @return Gumbo_Interface_Http_Input 
  57.      */
  58.     public static function instance ({
  59.         if (self::$_instance == null{
  60.             self::$_instance new Gumbo_Http_Input ();
  61.         }
  62.         return self::$_instance;
  63.     }
  64.     
  65.     
  66.     
  67.     /**
  68.      * Returns the Input object for a given field
  69.      * @param string $field 
  70.      * @return Gumbo_Interface_Input 
  71.      */
  72.     public function __get ($field{
  73.         return $this->getInput ($field);
  74.     }
  75.     
  76.     /**
  77.      * Adds|Resets a Constraint for the supplied REQUEST field name
  78.      * @param string $field 
  79.      * @param Gumbo_Interface_Constraint $obj 
  80.      * @throws Gumbo_Exception
  81.      */
  82.     public function __set ($field$obj=null{
  83.         try {
  84.             // verify precondition
  85.             if ($this->toIgnoreConstraints ()) return}
  86.             if (!is_string ($fld)) {
  87.                 throw new Gumbo_Exception ("Invalid Argument 'field:str' => {$field}:gettype ($field));
  88.             }
  89.             if (!is_null && !is_object ($obj)) {
  90.                 throw new Gumbo_Exception ("Invalid Argument 'obj:StdClass|null' => {$obj}:gettype ($obj));
  91.             }
  92.             
  93.             if (is_null ($obj)) {
  94.                 $this->resetInput ($field);
  95.             else {
  96.                 if (!($obj instanceof Gumbo_Interface_Constraint)) {
  97.                     throw new Gumbo_Exception ("Invalid Argument 'obj:Gumbo_Interface_Constraint' => {$obj}:gettype ($obj));
  98.                 }
  99.                 $this->addConstraint ($field$obj);
  100.             }
  101.             
  102.         catch (Gumbo_Exception $e{
  103.             $e->setFunction (__METHOD__);
  104.             gumbo_trigger ($e);
  105.         }
  106.     }
  107.     
  108.     
  109.     
  110.     /** ACTION METHODS **/
  111.     /**
  112.      * Adds a Constraint to a given field
  113.      * @param string $field 
  114.      * @param Gumbo_Interface_Constraint $obj 
  115.      * @param bool $return_val if Constraint returns a value
  116.      * @throws Gumbo_Exception
  117.      */
  118.     public function addConstraint ($fieldGumbo_Interface_Constraint $obj$return_val=false{
  119.         try {
  120.             // verify precondition
  121.             if ($this->toIgnoreConstraints ()) return}
  122.             if (!is_string ($field)) {
  123.                 throw new Gumbo_Exception ("Invalid Argument 'field:str' => {$field}:gettype ($field));
  124.             }
  125.             if (!is_bool ($return_val)) {
  126.                 throw new Gumbo_Exception ("Invalid Argument 'return_val:bool' => {$return_val}:gettype ($return_val));
  127.             }
  128.             
  129.             if (!$this->hasInput ($field)) {
  130.                 $this->_input [$fieldnew Gumbo_Input ();
  131.             }
  132.             
  133.             if ($return_val{
  134.                 $this->_input [$field]->addReturnConstraint ($obj);
  135.             else {
  136.                 $this->_input [$field]->addConstraint ($obj);
  137.             }
  138.         catch (Gumbo_Exception $e{
  139.             $e->setFunction (__METHOD__);
  140.             gumbo_trigger ($e);
  141.         }
  142.     }
  143.     
  144.     /**
  145.      * Resets Constraints
  146.      * @param string $field 
  147.      * @throws Gumbo_Exception
  148.      */
  149.     public function resetInput ($field{
  150.         try {
  151.             // verify precondition
  152.             if ($this->toIgnoreConstraints ()) return}
  153.             if (!is_string ($field)) {
  154.                 throw new Gumbo_Exception ("Invalid Argument 'field:str' => {$field}:gettype ($field));
  155.             }
  156.             if (!$this->hasInput ($field)) {
  157.                 throw new Gumbo_Exception ("No Constraint(sDefined: {$field}");
  158.             }
  159.             
  160.             unset ($this->_input [$field]);
  161.         catch (Gumbo_Exception $e{
  162.             $e->setFunction (__METHOD__);
  163.             gumbo_trigger ($e);
  164.         }
  165.     }
  166.     
  167.     
  168.     
  169.     /** MUTATOR METHODS **/
  170.     /**
  171.      * Sets if the object should ignore the status code and return raw data
  172.      * @param bool $val 
  173.      * @throws Gumbo_Exception
  174.      */
  175.     public function setIgnoreConstraints ($val{
  176.         try {
  177.             // verify precondition
  178.             if (!is_bool ($val)) {
  179.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  180.             }
  181.             
  182.             $this->_ignore = $val;
  183.         catch (Gumbo_Exception $e{
  184.             $e->setFunction (__METHOD__);
  185.             gumbo_trigger ($e);
  186.         }
  187.     }
  188.     
  189.     
  190.     
  191.     /** ACCESSOR METHODS **/
  192.     /**
  193.      * Returns the Constaints of the supplied field
  194.      * @param string $field 
  195.      * @return Gumbo_Interface_Input 
  196.      * @throws Gumbo_Exception
  197.      */
  198.     public function getInput ($field{
  199.         try {
  200.             // verify precondition
  201.             if ($this->toIgnoreConstraints ()) return}
  202.             if (!is_string ($field)) {
  203.                 throw new Gumbo_Exception ("Invalid Argument 'field:str' => {$field}:gettype ($field));
  204.             }
  205.             if (!$this->hasInput ($field)) {
  206.                 throw new Gumbo_Exception ("No Constraint(sDefined: {$field}");
  207.             }
  208.             
  209.             return $this->_input [$field];
  210.         catch (Gumbo_Exception $e{
  211.             $e->setFunction (__METHOD__);
  212.             gumbo_trigger ($e);
  213.         }
  214.     }
  215.     
  216.     /**
  217.      * Returns if the field has a constraint
  218.      * @param string $field 
  219.      * @return bool 
  220.      * @throws Gumbo_Exception
  221.      */
  222.     public function hasInput ($field{
  223.         try {
  224.             // verify precondition
  225.             if ($this->toIgnoreConstraints ()) return false}
  226.             if (!is_string ($field)) {
  227.                 throw new Gumbo_Exception ("Invalid Argument 'field:str' => {$field}:gettype ($field));
  228.             }
  229.             
  230.             return isset ($this->_input [$field]);
  231.         catch (Gumbo_Exception $e{
  232.             $e->setFunction (__METHOD__);
  233.             gumbo_trigger ($e);
  234.         }
  235.         return false;
  236.     }
  237.     
  238.     /**
  239.      * Returns if data should be returned raw
  240.      * @return bool 
  241.      */
  242.     public function toIgnoreConstraints ({
  243.         return $this->_ignore;
  244.     }
  245.     
  246. }
  247.  
  248. ?>