Advertisement
Guest User

class falidate fuelphp

a guest
Sep 4th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. class ValidateRules
  5. {
  6.    
  7.     static private $folderName = 'rules';
  8.  
  9.     /**
  10.      * get external validation rules and instance of Validation() object
  11.      *  
  12.      * @param string/array name of file rules or send array of validate rules
  13.      * @param string instanceName name of instance Validatio::forge()
  14.      *
  15.      * @return instance of Validation::forge()
  16.      */
  17.     public static function getRules($name, $instanceName = null)
  18.     {
  19.         if($instanceName)
  20.         {
  21.             $instance = \Validation::forge($instanceName);
  22.         } else {
  23.             $instance = \Validation::forge(\Str::random());
  24.         }
  25.        
  26.         if(is_array($name))
  27.         {
  28.             $storeRules = array();
  29.             foreach($name as $filename)
  30.             {
  31.                 $storeRules[] = include(self::getFileRules($filename));
  32.             }
  33.            
  34.             $rules = array();
  35.             foreach($storeRules as $arrayRule)
  36.             {
  37.                 $rules = array_merge($rules, $arrayRule);
  38.             }
  39.                      
  40.         } else {
  41.            
  42.             $rules = include(self::getFileRules($name));
  43.            
  44.         }
  45.        
  46.         foreach($rules as $field => $item)
  47.         {
  48.             $instance->add_field( $field, $item['label'], self::convertToString($item['rules']) );
  49.         }
  50.        
  51.         return $instance;
  52.     }
  53.  
  54.  
  55.     /**
  56.      * @param string $name filename rules
  57.      * @return string path file
  58.      */
  59.     private static function getFileRules($name)
  60.     {
  61.         $fileName = explode('_', $name);
  62.        
  63.         $file = APPPATH. DS .$fileName[0]. DS .self::$folderName. DS .$name.'.php';
  64.        
  65.         return $file;
  66.     }
  67.    
  68.    
  69.     /**
  70.      * @param array $rules
  71.      * @return string
  72.      */
  73.     private static function convertToString($rules = array())
  74.     {
  75.         $string = '';
  76.         foreach($rules as $name => $rule)
  77.         {
  78.            
  79.             if(is_array($rule))
  80.             {
  81.                 $string .= $name.'[';
  82.                 $string .= implode(',', $rule);
  83.                 $string .= ']|';
  84.             }
  85.             elseif($name == $rule)
  86.             {
  87.                 $string .= $rule . '|';
  88.             } else {
  89.                 $string .= $name.'['.$rule.']|';
  90.             }
  91.            
  92.         }
  93.        
  94.         $string = trim(rtrim($string, '|'));
  95.        
  96.         return $string;
  97.     }
  98.    
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement