宏哥不像那些喷宏哥的娘泡们, 随手就上一段代码

宏哥 发布于 2013/12/23 14:22
阅读 356
收藏 1

每当我看到有那么的娘泡唧唧歪歪的时候, 就觉得这世界最荒唐的事情莫过于:

一夫一妻

这段, 写得辛苦, 好像也没有写出多少行来:

<?PHP
/**
 * Composite fields validation,once the composite validate is used. should call check instead of 
 * Contruct with $name as validation name ,$data to validate,optional &$validator to append
 *
 * The fields will be populated in rule and 
 * Filter will be parsed and changing the value of element
 *
 * @package    ROOT
 * @category   Validate
 * @author     Anthony
 * @updated  2013-12-14
 */

class Validate_Composite {
	/**
	 * pseudo input name
	 */
	private $name;

	/**
	 * Validator assoiated to Composite validate
	 */
	public $validate;

	/**
	 * Validate Message file
	 */
	private $file;

	/**
	 * The Error Message 
	 */
	private $_errors;

	/**
	 * Composite rules and data
	 */
	private $_rules = array();

	/**
	 * Setup Composite validate from  name and data
	 *
	 * @param String $name, pseudo input name 
	 * @param Array $data, Data to construct validate, data is linked to name as attribute
	 * @param Validate &$validator, the validator object to process all the validation, default NULL
	 * @param String $file, File name for validation messages
	 */
	public function __construct($name = 'errmsg',$data = array(),&$validator = NULL,$file = 'validate'){
		$this->name = $name;
		$this->file = $file;
		if($validator === NULL){
			$this->validate = new Validate(
				array($name=>$data)
			);
		}else{
			$this->validate = $validator;
			$this->validate[$name] = $data;
		}
	}

	/**
	 * Convert as_array into Json Encoded String
	 *
	 * @return String
	 */
	public function __toString(){
		return (String) $this->validate;
	}

	/**
	 * Returns the array representation of the current object.
	 * @return  array
	 */
	public function as_array(){
		$_array =  $this->validate->as_array();
		unset($_array[$this->name]);
		return $_array;
	}

	/**
	 * Same intterface as Validate
	 * Overwrites or appends rules to a field. Each rule will be executed once.
	 * All rules must be string names of functions method names.
	 *
	 *     // The "username" must not be empty and have a minimum length of 4
	 *     $validation->rule('username', 'not_empty')
	 *                ->rule('username', 'min_length', array(4));
	 *
	 * @param   string  field name
	 * @param   string  function or static method name
	 * @param   array   extra parameters for the rule
	 * @return  $this
	 */

	public function rule($field, $rule, array $params = NULL){
		$this->validate->rule($field,$rule,$params);
	}

	/**
	 * Same intterface as Validate
	 * Overwrites or appends filters to a field. Each filter will be executed once.
	 * All rules must be valid PHP callbacks.
	 *
	 *     // Run trim() on all fields
	 *     $validation->filter(TRUE, 'trim');
	 *
	 * @param   string  field name
	 * @param   mixed   valid PHP callback
	 * @param   array   extra parameters for the filter
	 * @return  $this
	 */

	public function filter($field, $filter, array $params = NULL){
		$this->validate->filter($field,$filter,$params);
	}

	/**
	 * Add composite validation rule
	 *
	 * @param String $rule,unique Class::method name to do the validation 
	 * @param String $fileds,Field list array
	 * @param Mixed $paras, Extra parameter
	 * 
	 * @Sample function data_equals($fields,$data,$paras)
	 * $_v->compositeRule('data_equals',array('t1','t2'),array('errors'=>&$_paras,'validator'=>&$_validator));
	 */
	public function compositeRule($rule,array $fields,array $paras = NULL){
		$this->_rules[$rule] = array( 'fields' =>$fields,'paras' =>$paras);
		return $this->validate->rule($this->name,$rule,array(
			$fields,$paras)
		);
	}

	/**
	 * One step to check composite fields through one or more rules.
	 *
	 * @param String $name, pseudo input name,Generic error message is show here.
	 * @param Array $data, Data to construct validate, could be multiple
	 * @param Mixed $rules,Rule or Rules of unique Class::method name to do the validation 
	 * @param String $fileds,Fields or group of fields
	 * @param Mixed $paras, Paras or group of Paras
	 *
	 * @param Validate &$validator, Add validator if initialized.
	 *
	 * @return True for success, Consolidated error messeges
	 *
	 * @sample, the extra error message could be return from 
	 *
	 * function data_equals($fields,$data,$paras){
	 * 		$para['errors']['name'] = 'Error Message';
	 * 		return false;
	 * }
	 *
	 * $_ret = Validate_Composite::quickCheck('errmsg', $data,array('data_equals'),array(array('t1','t2')),
	 * array(array('errors'=>&$_paras,'validator'=>&$_validator)),$_validator);
	 */
	public static function quickCheck($name='errmsg',$data = array(),$rules,$fields,$paras = NULL,
	&$validator = NULL){
		$_v = new Validate_Composite($name,$data,$validator);
		if(is_array($rules)){
			foreach ($rules as $i => $rule){
				$_v->compositeRule($rule,$fields[$i],$paras[$i]);
			}
		}else{
			$_v->compositeRule($rule,$fields,$paras);
		}

		if($_v->check() === true){
			return true;
		}
		return $_v->errors();
	}	

	/**
	 * Returns the error messages. If no file is specified, the error message
	 * will be the name of the rule that failed. When a file is specified, the
	 * message will be loaded from $field.$rule, or if no rule-specific message
	 * exists, $field.default will be used. If neither is set, the returned
	 * message will be validate.$rule. If validate.$rule is empty,
	 * then $file.$field.$rule will be returned.
	 *
	 * By default all messages are translated using the default language.
	 * A string can be used as the second parameter to specified the language
	 * that the message was written in.
	 *
	 *     // Get errors from messages/forms/login.php
	 *     $errors = $validate->errors();
	 *
	 * @uses    Pexcel::message
	 *
	 * @param   mixed   translate the message
	 * @return  array
	 */
	public function errors(){
		return $this->_errors;
	}

	/**
	 * Executes all validation filters, rules, and callbacks. This should
	 * typically be called within an if/else block.
	 *
	 *     if ($validation->check())
	 *     {
	 *          // The data is valid, do something here
	 *     }
	 *
	 * @param   boolean   allow empty array?
	 * @return  boolean
	 */
	public function check($allow_empty = TRUE){
		$_errors = array();
		if($this->validate->check($allow_empty)){
			unset($this->validate[$this->name]);
			return True;
		}else{
			foreach( $this->_rules as $rule => $ruleData){
				$_errors = array_merge($_errors,Arr::get($ruleData['paras'],'errors',array()));
			}

			$this->_errors = array_merge($_errors,$this->validate->errors($this->file));
			return false;
		}
	}
}



加载中
0
首席安全砖家
首席安全砖家

我看到的尽是英文注释。。php 不会。 也看不出来哪里高深= =。 话说, @宏哥  , 我到发现你英语能力强悍。。 

返回顶部
顶部