<?php



/*
 *  validate($var, $val)
 * PARAMS :
 * $var: valor mixed var a comprobar
 * $val: tipo de validacion:
 * EMAIL,ALPHANUMERIC,MAX_LENGTH,MIN_LENGTH, RANGE, RANGE-ENUM, INT, DATE-DMY
 *
 *         $ret['nombre']= validate($arr['nombre'],
 *               array(
 *                   array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
 *                   array("MIN_LENGTH"=>5,"MESSAGE" => "DEMASIADO CORTO"),
 *                   array("MAX_LENGTH"=>10 ,"MESSAGE" => "DEMASIADO LARGO"),
 *               )
 *       );
 *
 *
 */


function validate($var, $val = array()){
    $ret = array();
    $rtn = "";

    for($i = 0 ; $i < count($val) ; $i++){
        $key = key($val[$i]);
        $ret[$i]['TYPE'] = $key;
        switch($key){
            case "EMAIL":
                $ret[$i]['STATUS'] = filter_var ($var, FILTER_VALIDATE_EMAIL) ? 1 : 0;
                break;
            case "EMAIL-EXIST":
                $ret[$i]['STATUS'] = 1;
                if ($val[$i]['EMAIL-EXIST']){
                    $sql = new sqldata();
                    $result = $sql->get_tabla("clientes", "Email like '".$var."'");
                    $ret[$i]['STATUS'] = 1;
                    if ($result){
                        $ret[$i]['STATUS'] = 0;
                    }
                }
                break;
            case "ALPHANUMERIC":
                $ret[$i]['STATUS'] = 1;
                break;
            case "MAX_LENGTH":
                $ret[$i]['STATUS'] = strlen($var) <= $val[$i]['MAX_LENGTH'] ? 1 : 0;
                break;
            case "MIN_LENGTH":
                $ret[$i]['STATUS'] = strlen($var) >= $val[$i]['MIN_LENGTH'] ? 1 : 0;
                break;

            case "RANGE":
                $ret[$i]['STATUS'] = 0;

                $range = explode("-",$val[$i]['RANGE']);
                if (count($range)==2){
                    $range[0] = $range[0]=="*" ? $var : $range[0];
                    $range[1] = $range[1]=="*" ? $var : $range[1];
                    if ($range[0] <= $var && $var <=$range[1]){
                        $ret[$i]['STATUS'] = 1;
                    }
                }
                break;
            case "RANGE-ENUM":
                $ret[$i]['STATUS'] = 0;

                $range = explode("|",$val[$i]['RANGE-ENUM']);

                $items = array();
                foreach($range as $atom){
                    echo $atoms;
                    $elem = explode("-",$atom);

                    if (count($elem)==1){
                        $items[] = $elem[0];
                    }
                    else
                    if (count($elem) != 2 ) {
                        break;
                        $err = true;
                    }
                    else {
                        $max = $elem[0] > $elem[1] ? $elem[0] : $elem[1];
                        $min = $elem[0] > $elem[1] ? $elem[1] : $elem[0];
                        for($x = $min ; $x <= $max ; $x++){
                            $items[] = $x;
                        }
                    }

                    if (!$err && count($items) ){
                         foreach($items as $item){
                             if ($var==$item){
                                 $ret[$i]['STATUS'] = 1;
                                 break;
                             }
                         }
                    }
                }
                break;
            case "INT":
                $ret[$i]['STATUS'] = filter_var ($var, FILTER_VALIDATE_INT) ? 1 : 0;
                break;
            case "DATE-DMY":
                $var = str_replace("/", "-", $var);
                $date = explode("-", $var);

                $ret[$i]['STATUS'] = 0;
                if (count($date)==3){
                    if (checkdate($date[1], $date[0], $date[2]) ){
                        $ret[$i]['STATUS'] = 1;
                    }
                }
                break;
        }

        $ret[$i]['MESSAGE'] = "";
        if (!$ret[$i]['STATUS']){
            $ret[$i]['MESSAGE'] =  $val[$i]['MESSAGE'];
        }


    }
    return $ret;
}






?>