一般的なphp検証関数を構築する

1967 ワード

Webアプリケーションの処理、特にデータベースへの書き込みのたびに、データ検証を行うには必要なステップであり、煩雑なたびに何度も呼び出されます.
普通の関数を書く暇はありません.後でどんな関数を検証するか、自分の関数を入れればいいです.
<?php
/**
 * php       
 * 
 *   :                
 * 
 * 1:            
 * 2:            
 *                   
 * 
 * 
 */
class DataFilter extends Exception{
	
	public function __construct(){
	
	}
	
	
	/**
	*       
	*    :filter
	*     :                   
	*     :array('  ','       ','       ')
	*    : json_decode(array('status'=flase,'message'='','fileds'))
	*     :
	* 		 
	*     :array(
	* 
	* 		array('md'='is_int','field'=>'name','message'=>'  ')
	* )
	* 
	* 
	* 
	*            
	* 
	*   :   
	* @copyright ch.gongchang.com
	* @time:2013-1-16
	* email:[email protected]
	*/
	public function filter($param = array(),$data = array()) {
			//       
			foreach ($param as $key=>$value){
				//          
				$function = new ReflectionMethod($this, $value['md']);
				if (isset($value['arg'])){	
					$arr = $value['arg'];
					$arr[] =$data[$value['field']];
					$status = $function->invokeArgs($this,$arr);
				}else{
					$status = $function->invoke($this,$data[$value['field']]);
				}
				if (!$status){
					throw new Exception($value['message']);
					break;//    
				}
			}
	}
	
	//       
	public  function is_int($input){
		return false;
	}
	/**
                         
      */

}

//     
$argArr       = array(
						array('md'=>'is_int','field'=>'name','message'=>'  '),
						array('md'=>'is_int','field'=>'name','message'=>'  ')
						);
$data['name'] = "ww"; 
$filter = new DataFilter();
try {
 $filter->filter($argArr,$data);
}catch(Exception $e){
	echo $e->getMessage();
}