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 Input
  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.  * Input Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Input
  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 Input Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Input");
  34. gumbo_load ("Constraint");
  35.  
  36. class Gumbo_Input implements Gumbo_Interface_Input {
  37.     
  38.     /** @var Gumbo_Interface_Constraint $_constraints */
  39.     private $_constraints = array ();
  40.     /** @var array $_constraints_return indication of which Constraints return values */
  41.     private $_constraints_return = array ();
  42.     
  43.     /** @var mixed $_data_raw initial data */
  44.     private $_data_raw;
  45.     /** @var mixed $_data_clean clean data */
  46.     private $_data_clean;
  47.     
  48.     /** @var bool $_clean */
  49.     private $_clean = false;
  50.     /** @var bool $_checked */
  51.     private $_checked = false;
  52.     /** @var bool $_run_thru */
  53.     private $_run_thru = true;
  54.     
  55.     
  56.     
  57.     /**
  58.      * Constructor
  59.      * @param mixed $data data to be checked
  60.      */
  61.     public function __construct ($data=null{
  62.         $this->setData ($data);
  63.     }
  64.     
  65.     /**
  66.      * Destructor
  67.      * @postcondition ensures all Constraint object are deleted
  68.      */
  69.     public function __destruct ({
  70.         $this->reset ();
  71.     }
  72.     
  73.     
  74.     
  75.     /** ACTION METHODS **/
  76.     /**
  77.      * Cleans the Data by running through the Constraints
  78.      * @precondition !isChecked()
  79.      * @return bool 
  80.      * @throws Gumbo_Exception
  81.      */
  82.     public function clean ({
  83.         if ($this->isChecked ()) {
  84.             return $this->isClean ();
  85.         }
  86.         
  87.         $success true;
  88.         if ($this->toRunThru ()) {
  89.             foreach ($this->getConstraints (as $key=>$obj{
  90.                 try {
  91.                     $tmp $obj->test ($this->getData ());
  92.                     if ($this->_constraints_return [$key]{
  93.                         $this->setCleanData ($tmp);
  94.                     }
  95.                 catch (Gumbo_Exception $e{
  96.                     gumbo_trigger ($e);
  97.                     $success false;
  98.                 }
  99.             }
  100.         else {
  101.             try {
  102.                 foreach ($this->getConstraints (as $key=>$obj{
  103.                     $tmp $obj->test ($this->getData ());
  104.                     if ($this->_constraints_return [$key]{
  105.                         $this->setCleanData ($tmp);
  106.                     }
  107.                 }
  108.             catch (Gumbo_Exception $e{
  109.                 gumbo_trigger ($e);
  110.                 $success false;
  111.             }
  112.         }
  113.         
  114.         $this->setClean ($success);
  115.         $this->setChecked (true);
  116.         
  117.         return $this->isClean ();
  118.     }
  119.     
  120.     /**
  121.      * Adds a Constraint against the Data (additional arguments accepted for Constraint parameters)
  122.      * @postcondition !isChecked()
  123.      * @postcondition !isClean()
  124.      * @param Gumbo_Interface_Constraint $obj 
  125.      */
  126.     public function addConstraint (Gumbo_Interface_Constraint $obj{
  127.         $this->_constraints [$obj;
  128.         $this->_constraints_return [false;
  129.         
  130.         $this->setChecked (false);
  131.         $this->setClean (false);
  132.     }
  133.     
  134.     /**
  135.      * Adds a return Constraint, which returns a formatted value
  136.      * @postcondition !isChecked()
  137.      * @postcondition !isClean()
  138.      * @param Gumbo_Interface_Constraint $obj 
  139.      */
  140.     public function addReturnConstraint (Gumbo_Interface_Constraint $obj{
  141.         $this->_constraints [$obj;
  142.         $this->_constraints_return [true;
  143.         
  144.         $this->setChecked (false);
  145.         $this->setClean (false);
  146.     }
  147.     
  148.     /**
  149.      * Resets ALL Constraints
  150.      * @postcondition !getConstraints()
  151.      * @postcondition !isChecked()
  152.      * @postcondition !isClean()
  153.      */
  154.     public function resetConstraints ({
  155.         $this->_constraints = array ();
  156.         $this->_constraints_return;
  157.         
  158.         $this->setChecked (false);
  159.         $this->setClean (false);
  160.     }
  161.     
  162.     
  163.     
  164.     /** MUTATOR METHODS **/
  165.     /**
  166.      * Sets the input data
  167.      * @postcondition !isChecked()
  168.      * @postcondition !isClean()
  169.      * @param mixed $data 
  170.      */
  171.     public function setData ($data{
  172.         if (!$this->getData ()) {
  173.             $this->_data_clean = $data;
  174.         }
  175.         $this->_data_raw = $data;
  176.         
  177.         $this->setClean (false);
  178.         $this->setChecked (false);
  179.     }
  180.     
  181.     /**
  182.      * Sets the Clean Data
  183.      * @param mixed $data 
  184.      */
  185.     protected function setCleanData ($data{
  186.         $this->_data_clean = $data;
  187.     }
  188.     
  189.     /**
  190.      * Sets if ALL Exceptions should be caught, or stop at first Exception
  191.      * @param bool $val 
  192.      * @throws Gumbo_Exception
  193.      */
  194.     public function setRunThru ($val{
  195.         try {
  196.             if (!is_bool ($val)) {
  197.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  198.             }
  199.             
  200.             $this->_run_thru = $val;
  201.         catch (Gumbo_Exception $e{
  202.             $e->setFunction (__METHOD__);
  203.             gumbo_trigger ($e);
  204.         }
  205.     }
  206.     
  207.     /**
  208.      * Sets the Data as Clean
  209.      * @param bool $val 
  210.      * @throws Gumbo_Exception
  211.      */
  212.     protected function setClean ($val{
  213.         try {
  214.             if (!is_bool ($val)) {
  215.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  216.             }
  217.             
  218.             $this->_clean = $val;
  219.         catch (Gumbo_Exception $e{
  220.             $e->setFunction (__METHOD__);
  221.             gumbo_trigger ($e);
  222.         }
  223.     }
  224.     
  225.     /**
  226.      * Sets the Data as Checked
  227.      * @param bool $val 
  228.      * @throws Gumbo_Exception
  229.      */
  230.     protected function setChecked ($val{
  231.         try {
  232.             if (!is_bool ($val)) {
  233.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  234.             }
  235.             
  236.             $this->_checked = $val;
  237.         catch (Gumbo_Exception $e{
  238.             $e->setFunction (__METHOD__);
  239.             gumbo_trigger ($e);
  240.         }
  241.     }
  242.     
  243.     
  244.     
  245.     /** ACCESSOR METHODS **/
  246.     /**
  247.      * Returns the Data
  248.      * @param bool $raw as raw data
  249.      * @return mixed 
  250.      * @throws Gumbo_Exception
  251.      */
  252.     public function getData ($raw=false{
  253.         try {
  254.             // verify precondition
  255.             if (!is_bool ($raw)) {
  256.                 throw new Gumbo_Exception ("Invalid Argument 'raw:bool' => {$raw}:gettype ($raw));
  257.             }
  258.         catch (Gumbo_Exception $e{
  259.             $e->setFunction (__METHOD__);
  260.             gumbo_trigger ($e);
  261.             $raw false;
  262.         }
  263.         
  264.         if ($raw{
  265.             return $this->_data_raw;
  266.         }
  267.         return $this->_data_clean;
  268.     }
  269.     
  270.     /**
  271.      * Returns ALL the Constraints
  272.      * @return Gumbo_Interface_Constraint[] 
  273.      */
  274.     public function getConstraints ({
  275.         return $this->_constraints;
  276.     }
  277.     
  278.     /**
  279.      * Returns if the Data is clean
  280.      * @return bool 
  281.      */
  282.     public function isClean ({
  283.         return $this->_clean;
  284.     }
  285.     
  286.     /**
  287.      * Returns if the Data was checked
  288.      * @return bool 
  289.      */
  290.     public function isChecked ({
  291.         return $this->_checked;
  292.     }
  293.     
  294.     /**
  295.      * Returns if all Exceptions should be caught, or stop at first Exception
  296.      * @return bool 
  297.      */
  298.     public function toRunThru ({
  299.         return $this->_run_thru;
  300.     }
  301.     
  302.     
  303. }
  304.  
  305. ?>