Location: PHPKode > scripts > Leopard Form > leopardValidator.php
<?php

/**
 * Description of leopardValidator
 *
 * @author Jakub Kuřitka
 */
abstract class leopardValidator {
    private static $withArgs = array("min", "max", "mini", "maxi", "mins", "maxs");
    /**
     *
     * @param array $rules
     * @param type $text
     * @param array $optional
     * @return array or true./netbeans-7.0beta2-ml-linux.sh
     */
    public static function validate(array $rules, $text) {
        $errors = leopardValidator::validateText($text, $rules);
        
        if (count($errors) == 0)
            return true;

        return $errors;
    }

    /**
     *
     * @param array $rules
     * @param array $rules
     * @param array $errors 
     */
    private static function validateText($text, array $rules) {
        $errors = array();
        foreach ($rules as $rule => $argument) {
            if(!is_numeric($rule))
            {
                if(!leopardValidator::runWithArguments($rule, $argument, $text))
                        $errors[] = array("rule" => $rule, "argument" => $argument);
            } else {
                if(!leopardValidator::runNormal($argument, $text))
                        $errors[] = array("rule" => $argument);  
            }          
        }
        return $errors;
    }
    
    private static function runNormal($rule, $text)
    {
        $rule = strtolower($rule);
        if(in_array($rule, leopardValidator::$withArgs))
                return true;
        if(!method_exists("leopardValidator", $rule))
                return true;
        
        return leopardValidator::$rule($text);
    }
    
    private static function runWithArguments($rule, $args, $text){
        $rule = strtolower($rule);
        if(!in_array($rule, leopardValidator::$withArgs))
                return true;
        if(!method_exists("leopardValidator", $rule))
                return true;
        
        return leopardValidator::$rule($text, $args);
    }

    private static function string($text) {
        return is_string($text);
    }

    private static function number($text) {
        return is_numeric($text);
    }

    private static function float($text) {
        return is_float($text);
    }

    private static function boolean($text) {
        return is_bool($text);
    }

    private static function notEmpty($text) {
        return !empty($text);
    }

    private static function email($text) {
        $validate = filter_var($text, FILTER_VALIDATE_EMAIL);
        return $validate === $text;
    }

    private static function ip($text) {
        $validate = filter_var($text, FILTER_VALIDATE_IP);
        return $validate === $text;
    }

    private static function ipv4($text) {
        $validate = filter_var($text, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
        return $validate === $text;
    }

    private static function ipv6($text) {
        $validate = filter_var($text, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
        return $validate === $text;
    }
    
    private static function url($text){
        $validate = filter_var($text, FILTER_VALIDATE_URL);
        return $validate === $text;
    }

    //with arguments
    private static function minS($text, $min) {
        return mb_strlen($text) >= $min;
    }

    private static function maxS($text, $max) {
        return mb_strlen($text) <= $max;
    }

    private static function minI($text, $min) {
        if (leopardValidator::number($text))
            return $text >= $min;
        return false;
    }

    private static function maxI($text, $max) {
        if (leopardValidator::number($text))
            return $text <= $max;
        return false;
    }

    private static function min($text, $min) {
        return leopardValidator::minS($text, $min);
    }

    private static function max($text, $max) {
        return leopardValidator::maxS($text, $max);
    }

}

class rules {
    const STRING = "STRING";
    const NUMBER = "NUMBER";
    const FLOAT = "FLOAT";
    const BOOLEAN = "BOOLEAN";
    const NOTEMPTY = "NOTEMPTY";
    const EMAIL = "EMAIL";
    const IP = "IP";
    const IPV4 = "IPV4";
    const IPV6 = "IPV6";
    const URL = "URL";

    //options
    const MIN = "MIN";
    const MAX = "MAX";
    const MINI = "MINI";
    const MAXI = "MAXI";
    const MINS = "MINS";
    const MAXS = "MAXS";
}

?>
Return current item: Leopard Form