• 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نحوه استفاده از یک کلاس
#1
سلام بر همگی
یه کلاس validation دارم اما نمی دونم چه طوری باید از پارامترهاش استفاده کنم ممنونتون میشم راهنماییم کنید.

سورس کلاس
کد پی‌اچ‌پی:
<?php
    
if (!defined('security')) @exit('No direct script access allowed');
    
    class 
Validation{
        private 
$source = array();
        private 
$validation_rules = array();
        
        public 
$errors = array();
        public 
$sanitized = array();
        
        final public function 
__construct(){}
        
        final public function 
__clone(){
            return(@exit(
show_error('Cloning is not allowed.')));
        }
        
        final public function 
addSource($source$trim=false){
            
$this->source $source;
        }
    
        final public function 
run(){
            foreach (new 
ArrayIterator($this->validation_rules) as $var => $opt){
                if (
$opt['required'] == true$this->is_set($var);
                if (
array_key_exists('trim'$opt) && $opt['trim'] == true
                    
$this->source[$var] = trim$this->source[$var] );
    
                switch(
$opt['type']){
                    case 
'email':
                        
$this->validateEmail($var$opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeEmail($var);
                        break;
    
                    case 
'url':
                        
$this->validateUrl($var);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeUrl($var);
                        break;
    
                    case 
'numeric':
                        
$this->validateNumeric($var$opt['min'], $opt['max'], $opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeNumeric($var);
                        break;
    
                    case 
'string':
                        
$this->validateString($var$opt['min'], $opt['max'], $opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeString($var);
                        break;
    
                    case 
'float':
                        
$this->validateFloat($var$opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeFloat($var);
                        break;
    
                    case 
'ipv4':
                        
$this->validateIpv4($var$opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeIpv4($var);
                        break;
    
                    case 
'ipv6':
                        
$this->validateIpv6($var$opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitizeIpv6($var);
                        break;
    
                    case 
'bool':
                        
$this->validateBool($var$opt['required']);
                        if (!
array_key_exists($var$this->errors)) $this->sanitized[$var] = (bool) $this->source[$var];
                        break;
                }
            }
        }
    
        final public function 
addRule($varname$type$required false$min 0$max 0$trim false){
            
$this->validation_rules[$varname] = array(
                                                        
'type' => $type
                                                        
'required' => $required
                                                        
'min' => $min
                                                        
'max' => $max
                                                        
'trim' => $trim);
            return 
$this;
        }
    
        final public function 
AddRules(array $rules_array){
            
$this->validation_rules array_merge($this->validation_rules$rules_array);
        }
    
        final private function 
is_set($var){
            if (!isset(
$this->source[$var])) $this->errors[$var] = $var ' is not set';
        }
    
        final private function 
validateIpv4($var$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;
            if (
filter_var($this->source[$var], FILTER_VALIDATE_IPFILTER_FLAG_IPV4) === FALSE$this->errors[$var] = $var ' is not a valid IPv4';
        }
    
        final private function 
validateIpv6($var$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;    
            if (
filter_var($this->source[$var], FILTER_VALIDATE_IPFILTER_FLAG_IPV6) === FALSE$this->errors[$var] = $var ' is not a valid IPv6';
        }
    
        final private function 
validateFloat($var$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;
            if (
filter_var($this->source[$var], FILTER_VALIDATE_FLOAT) === false$this->errors[$var] = $var ' is an invalid float';
        }
    
        final private function 
validateString($var$min 0$max 0$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;    
            if (isset(
$this->source[$var])){
                if(
strlen($this->source[$var]) < $min$this->errors[$var] = $var ' is too short';
                elseif(
strlen($this->source[$var]) > $max$this->errors[$var] = $var ' is too long';
                elseif(!
is_string($this->source[$var])) $this->errors[$var] = $var ' is invalid';
            };
        }
    
        final private function 
validateNumeric($var$min 0$max 0$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;
            if (
filter_var($this->source[$var], FILTER_VALIDATE_INT, array("options" => array("min_range" => $min"max_range" => $max)))===FALSE$this->errors[$var] = $var ' is an invalid number';
        }
    
        final private function 
validateUrl($var$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;
            if (
filter_var($this->source[$var], FILTER_VALIDATE_URL) === FALSE$this->errors[$var] = $var ' is not a valid URL';
        }
    
        final private function 
validateEmail($var$required false){
            if (
$required==false && strlen($this->source[$var]) == 0) return true;
            if (
filter_var($this->source[$var], FILTER_VALIDATE_EMAIL) === FALSE$this->errors[$var] = $var ' is not a valid email address';
        }
    
        final private function 
validateBool($var$required=false){
            if(
$required==false && strlen($this->source[$var]) == 0) return true;
            
filter_var($this->source[$var], FILTER_VALIDATE_BOOLEAN);
            {
                
$this->errors[$var] = $var ' is Invalid';
            }
        }
    
        final private function 
sanitizeEmail($var){
            
$email preg_replace'((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' ''$this->source[$var] );
            
$this->sanitized[$var] = (string) filter_var($emailFILTER_SANITIZE_EMAIL);
        }
    
        final private function 
sanitizeUrl($var){
            
$this->sanitized[$var] = (string) filter_var($this->source[$var],  FILTER_SANITIZE_URL);
        }
    
        final private function 
sanitizeNumeric($var){
            
$this->sanitized[$var] = (int) filter_var($this->source[$var], FILTER_SANITIZE_NUMBER_INT);
        }
    
        final private function 
sanitizeString($var){
            
$this->sanitized[$var] = (string) filter_var($this->source[$var], FILTER_SANITIZE_STRING);
        }
    
    }
?>
  پاسخ
تشکر شده توسط :
#2
کسی نیست راهنماییم کنه ConfusedConfusedConfused
  پاسخ
تشکر شده توسط :


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان