平時工作總結的一些常用工具,採用簡單的實作方式,功能較為單一,沒有複雜的依賴。
主機網址:http://git.oschina.net/caoyong2619/php-utils.git
-
/**
- * 驗證器
- * @author 曹勇
- * @example
- * $data = array('用戶名' => 'caoyong','密碼' => '');
- * $rules = array('使用者名稱' => 'require','password' => 'require');
- * $validator = new Validator($data,$rules);
- * $ is_pass = * $ is_pass = * $ is_pass = $validator->passed();
- * $is_fail = $validator->failed();
- * $message = $validator->messages();;
- */
-
- 類別驗證器
- {
- /**
- * 待驗證資料
- * @var array
- */
- 受保護的$data;
-
- /**
- * 驗證規則
- * @var array
- */
- 受保護的$rule;
-
- /**
- * 錯誤訊息
- * @var array
- */
- 受受保護>
- /**
- * 自訂錯誤訊息
- * @var array
- */
- 受受保護> 受保護受保護的$messages;
-
- /**
- * 擴充規則
- * @var array
- */
- protected $custom_messages;
-
- /**
- * 擴充驗證規則
- * @param string $name
- * @param Closure $rule
- */
- protected static $exterions = array( );
-
- public function __construct(array $data,array $rule,array $messages = array())
- {
- $this->setData($data);
- this $this $this $this $this->setData($data);
- this $this $this ->setRule( $rule);
- $this->setMessages($messages);
- }
-
- public function setData(array $data)
- {
- $this-> data = $data;
- }
-
- 公用函數setRule(array $rule)
- {
- $this->rule = $rule;
- }
-
- 公有函數setMessages(array $messages)
- {
- $this->custom_messages = $messages;
- }
-
- 受保護函數validate($attr,$rule)
- { { > if (is_array($rule))
- {
- foreach ($rule as $v)
- {
- if(false === $this->validate($attr, $v))
- 中斷;
- }
- }
- else
- {
- list($rule,$args) = $this->parseRule($rule);
-
- $ method = 'validate'.$rule;
-
- $args = array_merge(array($attr,$this->getValue($attr)),$args);
-
- $result = call_user_func_array (array($this,$method), $args);
-
- if (false === $result)
- {
- $rule = lcfirst($rule);
- if ( isset($this->custom_messages[$attr]))
- {
- if (is_array($this->custom_messages[$attr]) && isset($this->custom_messages[$attr] ][$rule ]))
- {
- $message = $this->custom_messages[$attr][$rule];
- }
- else
- if (is_string($this- >custom_messages[$ratt ]))
- {
- $message = $this->custom_messages[$attr];
- }
- else
- {
- $message = $attr .'規則'.$rule ;
- }
- }
- else
- $message = $attr.' 中回傳失敗。規則'.$rule;
- $this->messages[$attr] = $message;
- }
- 回傳$result;
- }
- }
- public function pass()
- {
- foreach ($this->rule as $attr => $rule)
- {
- $this->validate($attr, $rule) ;
- }
- return 0 === count($this->messages);
- }
-
- public function failed()
- {
- return !$this- >passed();
- {
- return !$this- >passed();
- }
-
- 公用函數訊息($key = false)
- {
- if ($key && isset($this->messages[$key]) )
- return $this-> messages[$key];
- return $this->messages;
- }
-
- protected function parseRule($rule)
- {
- if (false !== strpos($rule ,'|'))
- {
- list($rulename,$args) = 爆炸('|', $rule);
- $args = 爆炸(':', $args);
- }
- else
- {
- $rulename = $rule;
- $args = array();
- }
- 傳回陣列( ucfirst($rulename),$args);
- }
-
- protected function getValue($attr)
- {
- if(!is_null($value = $this->data[$ attr]))
- return $value;
- }
-
- /**
- * 批次增加擴充規則
- * @param $rules array
- */
- public static function addExtension($name,Closure $rule)
- {
- static::$extensions[$name] = $$name] = $ rule;
- }
-
- /***/
-
- public static function addExtensions(array $rules)
- {
- foreach ($rules as $k = >; $v)
- {
- static::addExtension($k, $v);
- }
- }
-
- public function __call($method,$args)
- {
- $method = lcfirst(substr($method, 8));
-
- $args = array_merge(array($this),$args);
-
- if (isset($this),$args);
-
- if (isset($this),$args);
-
- if (isset($this),$args);
-
- if (isset($this),$args);
-
- if (isset($this),$args);
-
- if (isset( static: :$extensions[$method]))
- {
- return call_user_func_array(static::$extensions[$method], $args);
- }
-
- throw new Exception(' rule ' .$method.' 不退出');
- }
-
- protected function validateRequired($attr,$value) { return !empty($value); { return !empty($value);; } 受保護函數validateLength($attr,$value,$len) { return $len == $min; } 受保護函數validateMin ($attr ,$value,$len) { return strlen($value) >; $len; }
-
- {
- return strlen($value)
- $len;
- }
-
- 受保護函數ValidateBetween($attr,$value,$min,$max)
- {
- return $this->validateMin($attr, $value, $min) && $this->validateMax($attr, $ value, $max);
- }
-
- 受保護函數validateEmail($attr,$value)
- {
- $regex = '/[w!#$%&'* /= ?^_`{|}~-] (?:.[w!#$%&'* /=?^_`{|}~-] ) *@(?:[w](?:[w- ]*[w])?.) [w](?:[w-]*[w])?/i';
-
- return (bool)preg_match($regex, $value);
- }
-
- protected function validateNumber($attr,$value)
- {
- return is_numeric($value);
- }
- {
- $in_data =explode(',', $in_data);
- return in_array($value, $in_data) );
- }
-
- 受🎜> 受保護函數validateNotin($attr,$value,$in_data)
- {
- return !$this->validateIn($attr, $value, $in_data) ;
- }
-
- protected 函數validateEq($attr,$value,$eq)
- {
- return $value == $eq;
- }
-
- protected function validateConfirm($attr,$value,$value,$ 🎜> {
- return $this->validateEq($attr, $value, $this->getValue($confirm));
- }
-
- 受保護函數validateUrl($attr,$value )
- {
- $regex = '/[a-zA-z] ://[^s]*/i';
- return (bool)preg_match($regex, $value);
- }
-
- 受保護函數validateMobile($attr,$value)
- {
- return preg_match('/1(3|4| 5|8})d{9}/',$ value);
- }
-
- protected function validateQQ($attr,$value)
- {
- return preg_match('/d{ 5,}/', $value);
- }
- }
-
-
複製程式碼
|