Location: PHPKode > scripts > PHP Typification Class > php-typification-class/typification.class.php
<?PHP
/* 
* @package User defining typification in PHP 
* @author $Author: sheiko $  
* @version $Id: typification.php,v 1.1 2005/10/26 15:58:15 sheiko Exp $ 
* @since v.1.0 
* @copyright (c) Dmitry Sheiko http://www.cmsdevelopment.com 
*/ 

class FuncParams { 

    var $patternCounter; 

    /** 
    * Class constructor 
    * @param string $Commands - a string with validation patterns 
    * @return object 
    */ 
    function FuncParams($Commands) { 
        $this->patternCounter = 0; 
		$backtrace = debug_backtrace();
        $this->Validate($Commands, $backtrace[1]["args"]); 
        return $this; 
    } 

    /** 
    * Set error triggers 
    * @param string $ErrorMessage error message 
    * @return boolean
    */ 

    function trigger_error($ErrorMessage) { 
		$backtrace = debug_backtrace();
		$index = 4;
		die("<b>PHP Typification Error:</b> ".$ErrorMessage." in the <b>".($backtrace[$index]["class"]?$backtrace[$index]["class"]."::":"").$backtrace[$index]["function"]."</b> function in <b>".$backtrace[$index]["file"]."</b> on line <b>".$backtrace[$index]["line"]."</b>");
		return true;

    } 

    /** 
    * String type validation 
    * @param string $a_pattern pattern for validation 
    * @param string $a_value value for validation 
    * @return boolean
    */ 
        function is_string( $a_pattern, $a_value ) { 
        $range = array(); 
        if( !is_string($a_value)) $this->trigger_error("Parameter No.".$this->patternCounter." must be a String"); 
        if( preg_match("/^.*\{.*\}/is", trim($a_pattern)) ) { 
            list($range["left"], $range["right"]) = split(",", preg_replace("/^.*\{(.*)\}.*?$/is", "\\1", $a_pattern)); 
            if(strlen($a_value)<$range["left"] OR strlen($a_value)>$range["right"])  $this->trigger_error("Value of parameter No.".$this->patternCounter." is out of allowed bounds <i>".$a_pattern."</i>"); 
        } 
        if( preg_match("/^.*\[.*\]/is", trim($a_pattern)) ) { 
            $range = split("\|", preg_replace("/^.*\[(.*)\]$/is", "\\1", $a_pattern)); 
            if( !in_array($a_value,$range) ) $this->trigger_error("Value of parameter No.".$this->patternCounter." is out of allowed bounds <i>".$a_pattern."</i>"); 
        } 
         
    } 

    /** 
    * Integer type validation 
    * @param string $a_pattern pattern for validation 
    * @param string $a_value value for validation 
    * @return boolean
    */ 
    function is_integer( $a_pattern, $a_value) { 
        $range = array(); 
        if( !is_integer($a_value)) $this->trigger_error("Parameter No.".$this->patternCounter." must be an Integer"); 
        if( preg_match("/^int.*?\{.*\}/is", trim($a_pattern)) ) { 
            list($range["left"], $range["right"]) = split(",", preg_replace("/^int.*?\{(.*)\}$/is", "\\1", $a_pattern)); 
            if($a_value<$range["left"] OR $a_value>$range["right"])  $this->trigger_error ("Value of parameter No.".$this->patternCounter." is out of allowed bounds <i>".$a_pattern."</i>"); 
        } 
        if( preg_match("/^.*\(.*\)/is", trim($a_pattern)) ) { 
            $range = split("\|", preg_replace("/^.*\((.*)\)$/is", "\\1", $a_pattern)); 
            if( !in_array($a_value,$range) ) $this->trigger_error("Value of parameter No.".$this->patternCounter." is out of allowed bounds  <i>".$a_pattern."</i>"); 
        }         
    } 
     
    /** 
    * Array type validation 
    * @param string $a_pattern pattern for validation 
    * @param string $a_value value for validation 
    * @return boolean
    */ 
    function is_array( $a_pattern, $a_value ) { 
        $range = array(); 
        if( !is_array($a_value)) $this->trigger_error ("Parameter No.".$this->patternCounter." must be an Array"); 
        if( preg_match("/^array\{.*\}/is", trim($a_pattern)) ) { 
            $range = split(",", preg_replace("/^array\{(.*)\}$/is", "\\1", $a_pattern)); 
            if(count($a_value)!=$range[0])  $this->trigger_error("Value of parameter No.".$this->patternCounter." is out of allowed bounds <i>".$a_pattern."</i>"); 
        } 
    } 
     
    /** 
    * ArrayOfStrings type validation 
    * @param string $a_pattern pattern for validation 
    * @param string $a_value value for validation 
    * @return boolean
    */ 
    function is_ArrayOfStrings( $a_pattern, $a_value) { 
        $range = array(); 
        if( !is_array($a_value)) $this->trigger_error("Parameter No.".$this->patternCounter." must be an array of strings"); 
        if($a_value) { 
            foreach($a_value as $val) { 
                if(gettype($val)!="string")  $this->trigger_error("Parameter No.".$this->patternCounter." contains an illegal value (not string)"); 
            } 
        } 
        if( preg_match("/^ArrayOfStrings\{.*\}/is", trim($a_pattern)) ) { 
            $range = split(",", preg_replace("/^ArrayOfStrings\{(.*)\}$/is", "\\1", $a_pattern)); 
            if(count($a_value)!=$range[0])  $this->trigger_error("Value of parameter No.".$this->patternCounter." is out of allowed bounds <i>".$a_pattern."<i>"); 
        } 
    }     

    /** 
    * Validator 
    * @param string $Commands - a string with validation patterns 
    * @param string $ParamValuesArray parameters values array 
    * @return boolean
    */ 
    function Validate($Commands, $ParamValuesArray) { 
        $patternsArr = array(); 
        $a_pattern = ""; 
        $a_value = 0; 
        $key = ""; 

        if( !is_string($Commands) OR !$Commands)    trigger_error ("Incorrect FuncParams parameter", E_USER_ERROR); 
        if( !is_array($ParamValuesArray) )    trigger_error ("Incorrect ParamValuesArray parameter", E_USER_ERROR); 

        $patternsArr = split(";", trim($Commands)); 

        if($patternsArr) { 
            foreach($patternsArr as $a_pattern) { 
                $a_value = $ParamValuesArray[(++$this->patternCounter-1)]; 
                if(!$a_pattern) break; 
                if( preg_match("/^\*/is", trim($a_pattern)) ) break; 
                if( preg_match("/^ArrayOfStrings/is", trim($a_pattern)) )  $this->is_ArrayOfStrings(trim($a_pattern), $a_value); 
                if( preg_match("/^string/is", trim($a_pattern)) ) $this->is_string(trim($a_pattern), $a_value); 
                if( preg_match("/^int/is", trim($a_pattern)) )  $this->is_integer(trim($a_pattern), $a_value); 
                if( preg_match("/^array/is", trim($a_pattern)) )  $this->is_array(trim($a_pattern), $a_value); 
            } 
        } 
        return True; 
    } 

} 

?>
Return current item: PHP Typification Class