Source for file Constraint.class.php

Documentation is available at Constraint.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 Constraint 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 Constraint Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Constraint");
  34.  
  35. class Gumbo_Constraint implements Gumbo_Interface_Constraint {
  36.     
  37.     /** @var string|array$_method */
  38.     private $_method;
  39.     /** @var int $_key */
  40.     private $_key = 0;
  41.     /** @var array $_cases extra arguments to pass */
  42.     private $_cases = array ();
  43.     
  44.     
  45.     
  46.     /**
  47.      * Constructor (extra arguments are the various Case parameters)
  48.      * @param string $func function/method name
  49.      * @param string|StdClass$cls class name or object
  50.      * @param int $key location to place data in argument of function/method
  51.      */
  52.     public function __construct ($func=null$cls=null$key=0{
  53.         if (!is_null ($func)) $this->setMethod ($func$cls)}
  54.         $this->setKey ($key);
  55.     }
  56.     
  57.     
  58.     
  59.     
  60.     /** ACTION METHODS **/
  61.     /**
  62.      * Tests the Constraint, returning either boolean or formatted data
  63.      * @param mixed $data 
  64.      * @return mixed 
  65.      */
  66.     public function test ($data{
  67.         try {
  68.             if (!$this->getMethod ()) {
  69.                 throw new Gumbo_Exception ("Constraint Function/Method Undefined");
  70.             }
  71.         catch (Gumbo_Exception $e{
  72.             $e->setFunction ($e);
  73.             gumbo_trigger ($e);
  74.             return false;
  75.         }
  76.         
  77.         // setup the arguments
  78.         $args array ();
  79.         foreach ($this->getCases (as $key=>$val{
  80.             if ($key === $this->getKey ()) {
  81.                 $args [$data;
  82.             }
  83.             $args [$val;
  84.         }
  85.         
  86.         return call_user_func_array ($this->getMethod ()$args);
  87.     }
  88.     
  89.     /**
  90.      * Adds a Case to the Constraint Method (accepts additional arguments)
  91.      * @param mixed $val 
  92.      */
  93.     public function addCase ($val{
  94.         foreach (func_get_args (as $val{
  95.             $this->_cases [$val;
  96.         }
  97.     }
  98.     
  99.     /**
  100.      * Resets all Case Argument
  101.      * @postcondition !getCases()
  102.      */
  103.     public function resetCases ({
  104.         $this->_cases = array ();
  105.     }
  106.     
  107.     
  108.     
  109.     /** MUTATOR METHODS **/
  110.     /**
  111.      * Sets the Function|Method
  112.      * @param string $func 
  113.      * @param string|StdClass$obj 
  114.      * @throws Gumbo_Exception
  115.      */
  116.     public function setMethod ($func$obj=null{
  117.         try {
  118.             // verify precondition
  119.             if (!is_string ($func)) {
  120.                 throw new Gumbo_Exception ("Invalid Argument 'func:str' => {$func}:gettype ($func));
  121.             }
  122.             if (!is_null ($obj&& !is_string ($obj&& !is_object ($obj)) {
  123.                 throw new Gumbo_Exception ("Invalid Argument 'obj:str|StrClass|null' => {$obj}:gettype ($obj));
  124.             }
  125.             
  126.             if (is_null ($obj)) {
  127.                 if (!function_exists ($func)) {
  128.                     throw new Gumbo_Exception ("Function Undefined: {$func}");
  129.                 }
  130.             else {
  131.                 if (!method_exists ($obj$func)) {
  132.                     if (is_object ($obj)) $obj get_class ($obj)}
  133.                     throw new Gumbo_Exception ("Class Method Undefined: {$cls}::{$func}");
  134.                 }
  135.                 $func array ($obj$func);
  136.             }
  137.             
  138.             $this->_method = $func;
  139.         catch (Gumbo_Exception $e{
  140.             $e->setFunction (__METHOD__);
  141.             gumbo_trigger ($e);
  142.         }
  143.     }
  144.     
  145.     /**
  146.      * Sets the Key, which indicates the location the 'data' argument should be placed (starts at 0 for 1st position)
  147.      * @precondition $key>0
  148.      * @param int $key 
  149.      */
  150.     public function setKey ($key{
  151.         try {
  152.             // verify precondition
  153.             if (!is_int ($key)) {
  154.                 throw new Gumbo_Exception ("Invalid Argument 'key:int' => {$key}:gettype ($key));
  155.             }
  156.             if ($key 0{
  157.                 throw new Gumbo_Exception ("Out Of Range 'key >= 0'");
  158.             }
  159.             
  160.             $this->_key = (int) $key;
  161.         catch (Gumbo_Exception $e{
  162.             $e->setFunction (__METHOD__);
  163.             gumbo_trigger ($e);
  164.         }
  165.     }
  166.     
  167.     
  168.     
  169.     
  170.     /** ACCESSOR METHODS **/
  171.     /**
  172.      * Returns the Function|Method
  173.      * @return string|array
  174.      */
  175.     public function getMethod ({
  176.         return $this->_method;
  177.     }
  178.     
  179.     /**
  180.      * Returns the Key
  181.      * @return int 
  182.      */
  183.     public function getKey ({
  184.         return $this->_key;
  185.     }
  186.     
  187.     /**
  188.      * Returns the Case Arguments (extra parameters sent to the function)
  189.      * @return array 
  190.      */
  191.     public function getCases ({
  192.         return $this->_cases;
  193.     }
  194.     
  195. }
  196.  
  197. ?>