Location: PHPKode > scripts > Validation Class RoyalHeart > validation.class.php
<?php
/**
 * TomatoPHP
 * 
 * Framework based on MVC design pattern. It is fully compatible with PHP 5.
 * Its advantages are very light weight and flexibility.
 *
 * File:        validation class
 *
 */

class validation {
        
        //field with text to validation
        private $checkString         = array();
        
        //field with bool values; true ? false
        private $_is_valid           = array();
        
        //return array with errors
        public $error                = array();
        
        //allowed method for valid
        private $allowedMethod      = array( 'match', 'maxlenght', 'minlenght', 'required', 'maxtimechar', 'type', 'between');
        
        /**
         * fields with error texts
         *
         */
        public $error_max_lenght                = 'Too long';
        public $error_min_lenght                = 'Too short';
        public $error_max_time_char             = ': occur too many times';
        public $error_match                     = 'Don\'t fulfilled condition';
        public $error_required                  = 'String can\'t empty';
        
        public $error_type_string               = 'Is not a string';
        public $error_type_integer              = 'Is not an integer';
        public $error_type_float                = 'Is not a float';
        public $error_type_numeric              = 'Is not a numeric';
        public $error_type_bool                 = 'Is not a bool';
        public $error_type_array                = 'Is not a array';
        public $error_between                   = 'Does not fit in between';
        
        /**
         * set array with data to check
         *
         * @access      public
         * @param       array @str array with data to check
         *
         */
        
        public function setString( $name, $val)
        {
                
            //set arraz
            $this -> checkString[$name] = $val;
            
            //return obiect
            return $this;
        }
        
        /**
         * check whether input data 
         * and send true or false to array
         * next in isValid method return information, not here
         *
         * @access      public
         * @param       string $val - name
         * @return      obiect
         *
         */
        
        public function setValidations( $val)
        {
            //set array
            $this -> name = $val;
            
            //return obiect
            return $this;
        }
        
        /**
         * obiect chech from setValidation, if method is no allowed, is skipped
         *
         */
        
        public function __call( $m, $a)
        {
            //chech whether is allowed
            if( in_array( $m, $this -> allowedMethod))
            {
                //set
                $this -> obiectString[ $this -> name][$m] = $a[0];
            }
            
            //return obiect
            return $this;
        }
        
        //start validation
        public function start()
        {

            //loop array, return array from inside
            foreach( $this -> obiectString as $arrays => $valueArrays) {
                
                //here too must be array
                if( is_array( $valueArrays)) {
                    
                    //in loop
                    //data from $valueArrays(array) loop and get names of field with strings to validation
                    foreach( $valueArrays as $valData => $value) {
                        
                        //here start validate parser
                        switch( $valData) {
                            
                            /**
                             * case math
                             * here developer can use youself regex
                             *
                             */
                            case 'match':
                                
                                if( preg_match( $value, $this -> checkString[$arrays])) {
                                    
                                    $this -> _is_valid[$arrays][$valData] = preg_match( $value, $this -> checkString[$arrays]);
                                    
                                } else {
                                
                                    $this -> _is_valid[$arrays][$valData] = false;
                                    
                                    //set errors array
                                    $this -> error[$arrays][] = $this -> error_match;	
                                }
                            break;
                            
                            /**
                             * case maxlenght
                             * maximum lenght of string
                             *
                             */
                            case 'maxlenght':
                                
                                if( strlen( $this -> checkString[$arrays]) <= $value) {
                                    
                                    $this -> _is_valid[$arrays][$valData] = true;
                                } else {
                                    
                                    $this -> _is_valid[$arrays][$valData] = false;
                                    
                                    //set errors array
                                    $this -> error[$arrays][] = $this -> error_max_lenght;
                                }
                            break;
                            
                            /**
                             * case minlenght
                             * minimum lenght of string
                             *
                             */
                            case 'minlenght':
                                
                                if( strlen( $this -> checkString[$arrays]) >= $value) {
                                    
                                    $this -> _is_valid[$arrays][$valData] = true;
                                } else {
                                    
                                    $this -> _is_valid[$arrays][$valData] = false;
                                    
                                    //set errors array
                                    $this -> error[$arrays][] = $this -> error_min_lenght;
                                }
                            break;
                            
                            /**
                             * case required
                             * check whether string is empty
                             *
                             */
                            
                            case 'required':
                                
                                //check value
                                if( $value === true) {
                                    
                                    if( strlen( trim( $this -> checkString[$arrays]))) {
                                        
                                        $this -> _is_valid[$arrays][$valData] = TRUE;
                                    } else {
                                        
                                        $this -> _is_valid[$arrays][$valData] = FALSE;
                                        
                                        //set errors array
                                        $this -> error[$arrays][] = $this -> error_required;
                                    }
                                    
                                }
                                
                            break;
                            
                            /**
                             * case maxtimechar
                             * maximum number of occurrences in a string
                             * parser explode string and return occurrence and number of repeation
                             *
                             */
                            case 'maxtimechar':
                                
                                //explode string, return word and number of repeation
                                $r = explode('[spilit]', $value);
                                
                                //regex
                                if( preg_match('/([a-z]+)/', $r[0])) {
                                    
                                    preg_match_all( '/'. $r[0] .'/', $this -> checkString[$arrays], $match);
                                } else {
                                    
                                    preg_match_all( '/\\'. $r[0] .'/', $this -> checkString[$arrays], $match);
                                }
                                
                                //count chars
                                if( count($match[0]) <= $r[1]) {
                                    
                                    $this -> _is_valid[$arrays][$valData] = true;
                                } else {
                                    
                                    $this -> _is_valid[$arrays][$valData] = false;
                                    
                                    //set errors array
                                    $this -> error[$arrays][] = $r[0] . $this -> error_max_time_char;
                                }
                            break;
                            
                            /**
                             * case type
                             * checks the type of string
                             * string; integer, float; numeric; bool, array
                             *
                             */
                             
                            case 'type':
                                
                                //next switch
                                switch( $value) {
                                    
                                    /**
                                     * check whether is no a string
                                     *
                                     */
                                    case 'string':
                                        
                                        if( is_string($this -> checkString[$arrays])) {
                                        
                                            $this -> _is_valid[$arrays][$valData] = TRUE;
                                            
                                        } else {
                                            
                                            $this -> _is_valid[$arrays][$valData] = FALSE;
                                            
                                            //set errors array
                                            $this -> error[$arrays][] = $this -> error_type_string;
                                        }
                                    break;
                                    
                                    /**
                                     * check whether is no a integer
                                     *
                                     */
                                    case 'integer':
                                        
                                        if( is_integer($this -> checkString[$arrays])) {
                                        
                                            $this -> _is_valid[$arrays][$valData] = TRUE;
                                            
                                        } else {
                                            
                                            $this -> _is_valid[$arrays][$valData] = FALSE;
                                            
                                            //set errors array
                                            $this -> error[$arrays][] = $this -> error_type_integer;
                                        }
                                    break;
                                    
                                    /**
                                     * check whether is no a float
                                     *
                                     */
                                    case 'float':
                                        
                                        if( is_float($this -> checkString[$arrays])) {
                                        
                                            $this -> _is_valid[$arrays][$valData] = TRUE;
                                            
                                        } else {
                                            
                                            $this -> _is_valid[$arrays][$valData] = FALSE;
                                            
                                            //set errors array
                                            $this -> error[$arrays][] = $this -> error_type_float;
                                        }
                                    break;
                                    
                                    /**
                                     * check whether is no a numeric
                                     *
                                     */
                                    case 'numeric':
                                        
                                        if( is_numeric($this -> checkString[$arrays])) {
                                        
                                            $this -> _is_valid[$arrays][$valData] = TRUE;
                                            
                                        } else {
                                            
                                            $this -> _is_valid[$arrays][$valData] = FALSE;
                                            
                                            //set errors array
                                            $this -> error[$arrays][] = $this -> error_type_numeric;
                                        }
                                    break;
                                    
                                    /**
                                     * check whether is no a bool
                                     *
                                     */
                                    case 'bool':
                                        
                                        if( is_bool($this -> checkString[$arrays])) {
                                        
                                            $this -> _is_valid[$arrays][$valData] = TRUE;
                                            
                                        } else {
                                            
                                            $this -> _is_valid[$arrays][$valData] = FALSE;
                                            
                                            //set errors array
                                            $this -> error[$arrays][] = $this -> error_type_bool;
                                        }
                                    break;
                                    
                                    /**
                                     * check whether is no a array
                                     *
                                     */
                                    case 'array':
                                        
                                        if( is_array($this -> checkString[$arrays])) {
                                        
                                            $this -> _is_valid[$arrays][$valData] = TRUE;
                                            
                                        } else {
                                            
                                            $this -> _is_valid[$arrays][$valData] = FALSE;
                                            
                                            //set errors array
                                            $this -> error[$arrays][] = $this -> error_type_array;
                                        }
                                    break;
                                    
                                }
                            break;
                            
                            /**
                             * case between
                             * return true whether string fit it
                             *
                             */								
                            case 'between':
                                
                                //must be in array
                                if( is_array( $value)) {
                                    
                                    //must be two keys
                                    if( count( $value) === 2 and is_integer( $value[0]) and is_integer( $value[1])) {
                                        
                                        //key first must be higher
                                        if( $value[0] < $value[1]) {
                                            
                                            //cnt
                                            if( strlen( $this -> checkString[$arrays]) >= $value[0] and strlen( $this -> checkString[$arrays]) <= $value[1]) {
                                                
                                                $this -> _is_valid[$arrays][$valData] = TRUE;
                                            } else {
                                                
                                                $this -> _is_valid[$arrays][$valData] = FALSE;
                                                
                                                //set errors array
                                                $this -> error[$arrays][] = $this -> error_between;
                                            }
                                            
                                        } else {
                                            
                                            //throw exception
                                            throw new Exception( 'Second value must be higher');
                                        }
                                    //must be one key
                                    } elseif( count( $value) === 1 and is_integer( $value[0])) {
                                        
                                            //cnt
                                            if( strlen( $this -> checkString[$arrays]) === $value[0]) {
                                                
                                                $this -> _is_valid[$arrays][$valData] = TRUE;
                                            } else {
                                                
                                                $this -> _is_valid[$arrays][$valData] = FALSE;
                                                
                                                //set errors array
                                                $this -> error[$arrays][] = $this -> error_between;
                                            }
                                        
                                    } else {
                                        
                                        //throw exception
                                        throw new Exception( 'In array must by one or two keys. If one, the number of characters is equal to the value.<br /> Any key must be integer.');
                                    }
                                    
                                } else {
                                    
                                    //throw exception
                                    throw new Exception( 'Input data must be in array on value of between');
                                }
                            break;
                        }
                    }
                } else {
                    
                    //throw exception
                    throw new Exception( 'Input data must be in array<br />Method: setValidations; first paramert');
                }
            }
        }
        
        /**
         * isValid, here return true or false
         *
         * @access      public
         * @param       string @val here set name of validation string
         *
         */
         
        public function isValid( $val) {
                
                //clean buffor
                $unset = $this -> checkString;
                unset( $unset);
                
                //multiple valid
                if( is_array( $val)) {
                        
                        foreach( $val as $item) {
                                
                                $check[] = $this -> isValid( $item);
                        }
                        
                        //check whether is minimum one time false, if is return false
                        if( in_array( false, $check)) {
                        
                                return false;
                                
                        //to return true, must be all times true
                        } else {
                        
                                return true;
                        }
                        
                } else {
                
                        //check whether is minimum one time false, if is return false
                        if( in_array( 0, $this -> _is_valid[$val])) {
                        
                                return false;
                                
                        //to return true, must be all times true
                        } else {
                        
                                return true;
                        }
                }
        }
        
        /**
         * add error
         *
         * @access      public static
         * @param       string or array $input
         * @param       string or bool $output
         *
         */
        
        public function addError( $input, $output = false) {
                
                if( is_array( $input) and $output == false) {
                        
                        foreach( $input as $item => $val) {
                                
                                $this -> error[$item][] = $val;
                        }
                } else {
                        
                        $this -> error[$input][] = $output;
                }
        }
}
// END Validation class

/* End of file validation.php */
/* Location: ./system/libraries/validation.php */
?>
Return current item: Validation Class RoyalHeart