Ver Mensaje Individual
  #6 (permalink)  
Antiguo 30/07/2011, 01:36
GrupoC
 
Fecha de Ingreso: noviembre-2009
Mensajes: 226
Antigüedad: 14 años, 4 meses
Puntos: 19
Respuesta: [aporte] sistema de validación de datos

Hola a todos!

Código PHP:
<?php

/*   This script remains free until these lines are changed or removed
// +------------------------------------------------------------------------------------+
// | class.Validation.php    :    Allows you to validate any type of data sent by users   |
// |------------------------------------------------------------------------------------|
// |   This program is free software; you can redistribute it and/or                    |
// | modify it under the terms of the GNU General Public License                          |
// | as published by the Free Software Foundation of the License                         |
// |                                                                                    |
// |   This program is distributed in the hope that it will be useful,                  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                       |
// | GNU General Public License for more details.                                        |
// |------------------------------------------------------------------------------------|  
// | Name of the script        :    class.Validate.php                                        |
// | Author                    :    Magd Kudama <[email protected]>                        |
// | Version                :    v1.0                                                    |
// | URL                    :    magdkudama.com/blog                                        |
// +------------------------------------------------------------------------------------+
*/

class Validation

{

    var 
$error_level;
    
    var 
$enviornment;
    
    var 
$count_elements=0;
    
    var 
$accnum="0";
    
    var 
$accsp="0";
    
    var 
$add_called=false;
        
    var 
$dec="<strong>ERROR:</strong> ";
    
    var 
$elements=array();
    
    var 
$errors_in_validation="";
    
    var 
$count;
    
    var 
$field;
    
    var 
$type;
    
    var 
$min;
    
    var 
$max;
    
    var 
$numdec;
    
    var 
$sql="0";
    
    var 
$sep;
    
    var 
$only;
    
    var 
$minchr;
    
    var 
$maxchr;
        
    var 
$tfh;
    
    var 
$like;
    
    var 
$minsize;
    
    var 
$maxsize;
    
    var 
$accformats;
    
    var 
$mindate;
    
    var 
$maxdate;
    
    var 
$element_separator="|";
    
    function 
is_assoc($arr)
    {
        return 
array_keys($arr)!==range(0,count($arr)-1);
    }
    
    private function 
showErrors($error)
    {
        die(
$error);
    }
    
    private function 
showValidation($text="")
    {
        
$sep=$this->element_separator;
        return empty(
$text) ? "1".$sep : ($this->error_level=="specific" $text.$sep "0".$sep);
    }
    
    public function 
__construct($error_level,$enviornment="development")
    {
        
$this->setErrorLevel($error_level);
        
$this->setEnviornment($enviornment);
    }
    
    private function 
setErrorLevel($error_level)
    {
        if(
in_array($error_level,array("specific","general")))
            
$this->error_level=$error_level;
        else
            
$this->showErrors($this->dec."You are not assigning a correct <strong>ERROR_LEVEL</strong>!");
    }
    
    private function 
setEnviornment($enviornment)
    {
        if(
in_array($enviornment,array("development","production")))
            
$this->enviornment=$enviornment;
        else
            
$this->showErrors($this->dec."You are not assigning a correct <strong>ENVIORNMENT</strong>!");
    }
    
    public function 
numberOfElements()
    {
        if(
$this->add_called)
            return 
$this->count;
        else
            
$this->showErrors($this->dec."You must call <em>addArray</em> before you call <em>numberOfElements</em>");
    }
    
    private function 
checkProperties($array)
    {
        
$required_properties=array("type","force","name","value");
        
        if(!
is_array($array))
            
$this->showErrors($this->dec."You must give an <em>array</em> of elements in order to check properties correctly");
        
        if(!
$this->is_assoc($array))
            
$this->showErrors($this->dec."You must give an <em>array</em> of properties!");
        
        
$array_keys=array_keys($array);
        
        if(
in_array("valid",$array_keys) && count($array_keys)!=4) {
            if(
count($array_keys)!=4)
                
$this->showErrors($this->dec."If you declare an array of valid values, you must only specify <em>name</em>, <em>force</em>, <em>valid</em> and the <em>value</em>");
            if(!
is_array($array["valid"]))
                
$this->showErrors($this->dec."The propertie <em>value</em> must be an array");
        }
        
        if(
in_array("valid",$array_keys))
            
array_shift($required_properties);
        else {
            if(
$array["type"]=="hour" && !isset($array["sep"]))
                
$this->showErrors($this->dec."You must give the propertie <em>sep</em> with an hour");
        }
        
        for(
$i=0;$i<count($required_properties);$i++)
            if(!
in_array($required_properties[$i],$array_keys))
                
$this->showErrors($this->dec."An element of the <em>array</em> has no ".$required_properties[$i]." declared");
        
        if(
count($array_keys)>8)
            
$this->showErrors($this->dec."Maximum of 8 properties per element exceeded");
        
        foreach(
$array as $key => $propertie)
        {
            switch(
$key)
            {
                case (
$key=="min" || $key=="max" || $key=="numdec"):
                    if(!
is_numeric($propertie))
                        
$this->showErrors($this->dec."The properties <em>min</em>, <em>max</em> and <em>numdec</em> must be numeric");
                    break;
                case 
"sep":
                    if(
strlen($propertie)!=1)
                        
$this->showErrors($this->dec."The properties <em>sep</em> must only have 1 character");
                    break;
                case (
$key=="sql" || $key=="tfh" || $key=="acc_num" || $key=="force"):
                    if(!
in_array($propertie,array("1","0")))
                        
$this->showErrors($this->dec."The properties <em>sql</em>, <em>acc_num</em>, <em>force</em> and <em>24h</em> must be in (0,1)");
                case (
$key=="minchr" || $key=="maxchr"):
                    if(
$propertie<0)
                        
$this->showErrors($this->dec."The properties <em>$key</em> must be higher than 0");
                    if(
$key=="minchr")
                        if((isset(
$array["maxchr"]) && $array["maxchr"]<$propertie))
                            
$this->showErrors($this->dec."The properties <em>minchr</em> must be smaller than <em>maxchr</em>");
                    else
                        if(isset(
$array["minchr"]) && $array["minchr"]>$propertie)
                            
$this->showErrors($this->dec."The properties <em>maxchr</em> must be greater than <em>minchr</em>");                        
                    break;
                case (
$key=="minsize" || $key=="maxsize"):
                    if(
$propertie<0)
                        
$this->showErrors($this->dec."The properties <em>$key</em> must be higher than 0");
                    if(
$key=="minsize")
                        if((isset(
$array["maxsize"]) && $array["maxsize"]<$propertie))
                            
$this->showErrors($this->dec."The propertie <em>minsize</em> must be smaller than <em>maxsize</em>");
                    else
                        if(isset(
$array["minsize"]) && $array["minsize"]>$propertie)
                            
$this->showErrors($this->dec."The propertie <em>maxsize</em> must be greater than <em>minsize</em>");                        
                    break;
                case (
$key=="mindate" || $key=="maxdate"):
                    if(
$key=="mindate")
                        if((isset(
$array["maxdate"]) && $array["maxdate"]<$propertie))
                            
$this->showErrors($this->dec."The propertie <em>mindate</em> must be smaller than <em>maxdate</em>");
                    else
                        if(isset(
$array["mindate"]) && $array["mindate"]>$propertie)
                            
$this->showErrors($this->dec."The propertie <em>maxdate</em> must be greater than <em>mindate</em>");                        
                    break;
                case 
"like":
                    if(
substr($propertie,0,1)!="%" && substr($propertie,-1)!="%")
                        
$this->showErrors($this->dec."The propertie <em>like</em> must have at least 1 character '%'");
                    break;
                case 
"only":
                    if(!
in_array(strtolower($propertie),array("ipv4","ipv6")))
                        
$this->showErrors($this->dec."The propertie <em>only</em> must be in (ipv4,ipv6)");
                    break;
                case 
"type":
                    
$types=array("integer","float","date","dni","email","url","string","file","ip","pass","hour");
                    if(!
in_array($propertie,$types))
                        
$this->showErrors($this->dec."The type <em>".$propertie."</em> is unknown!");                    
                    break;
                case 
"name":
                    if(!
ctype_alnum($propertie))
                        
$this->showErrors($this->dec."The propertie <em>name</em> must be alphanumerical");
                    break;
                case 
"value":
                    break;
                case 
"valid":
                    if(!
is_array($propertie))
                        
$this->showErrors($this->dec."The propertie <em>valid</em> must be an array");
                    if(
$this->is_assoc($propertie))
                        
$this->showErrors($this->dec."The propertie <em>valid</em> must not be an associative array");
                    break;
                case 
"accformats":    
                    if(!
is_array($array["accformats"]))
                        
$this->showErrors($this->dec."The propertie <em>accformats</em> must be an array");
                    break;
                default:
                    
$this->showErrors($this->dec."The propertie <em>$key</em> is unknown!");
            }
        }
    }
    
    public function 
addArray($array)
    {
        if(!
is_array($array))
            
$this->showErrors($this->dec."You must give an <em>array</em> as parameter");

        if(!
$this->add_called) {
            
$this->count=count($array);
            if(
$this->enviornment=="development") {
                for(
$i=0;$i<$this->count;$i++)
                    
$this->checkProperties($array[$i]);
            }
            
$this->elements=$array;
            
$this->add_called=true;
        }
        else
            
$this->showErrors($this->dec."The method <em>addArray</em> can only be called once");
    }
Esta es la primera parte del code (no me deja subir más de 10000 caracteres).
__________________
Mi blog: magdkudama.com

Mi clase de validación de datos: magdkudama.com/validation