Usually the summary of some common tools, the use of simple implementation, the function of a single, no complex dependence.
Managed Address: Http://git.oschina.net/caoyong2619/php-utils.git
- /**
- * Authenticator
- * @author Cao
- * @example
- * $data = Array (' username ' = ' caoyong ', ' password ' = ');
- * $rules = Array (' username ' = ' require ', ' password ' = ' require ');
- * $validator = new Validator ($data, $rules);
- * $is _pass = $validator->passed ();
- * $is _fail = $validator->failed ();
- * $message = $validator->messages ();
- */
- Class Validator
- {
- /**
- * Data to be validated
- * @var Array
- */
- protected $data;
- /**
- * Validation rules
- * @var Array
- */
- protected $rule;
- /**
- * Error message
- * @var Array
- */
- protected $messages;
- /**
- * Custom Error messages
- * @var Array
- */
- protected $custom _messages;
- /**
- * Extended Rules
- * @var Array
- */
- protected static $extensions = Array ();
- Public function __construct (array $data, array $rule, array $messages = Array ())
- {
- $this->setdata ($data);
- $this->setrule ($rule);
- $this->setmessages ($messages);
- }
- Public function SetData (array $data)
- {
- $this->data = $data;
- }
- Public function Setrule (array $rule)
- {
- $this->rule = $rule;
- }
- Public function setmessages (array $messages)
- {
- $this->custom_messages = $messages;
- }
- protected function Validate ($attr, $rule)
- {
- if (Is_array ($rule))
- {
- foreach ($rule as $v)
- {
- if (false = = = $this->validate ($attr, $v))
- Break
- }
- }
- 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[$attr]))
- {
- $message = $this->custom_messages[$attr];
- }
- Else
- {
- $message = $attr. ' Return failed in rule '. $rule;
- }
- }
- Else
- $message = $attr. ' Return failed in rule '. $rule;
- $this->messages[$attr] = $message;
- }
- return $result;
- }
- }
- Public function passed ()
- {
- foreach ($this->rule as $attr = $rule)
- {
- $this->validate ($attr, $rule);
- }
- return 0 = = = Count ($this->messages);
- }
- Public Function failed ()
- {
- Return! $this->passed ();
- }
- Public function messages ($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) = Explode (' | ', $rule);
- $args = Explode (': ', $args);
- }
- Else
- {
- $rulename = $rule;
- $args = Array ();
- }
- Return Array (Ucfirst ($rulename), $args);
- }
- protected function GetValue ($attr)
- {
- if (!is_null ($value = $this->data[$attr]))
- return $value;
- }
- /**
- * Extended Validation rules
- * @param string $name
- * @param Closure $rule
- */
- public static function AddExtension ($name, Closure $rule)
- {
- Static:: $extensions [$name] = $rule;
- }
- /**
- * Bulk Increase extension rules
- * @param $rules Array
- */
- public static function Addextensions (array $rules)
- {
- foreach ($rules as $k = $v)
- {
- Static::addextenstion ($k, $v);
- }
- }
- Public Function __call ($method, $args)
- {
- $method = Lcfirst (substr ($method, 8));
- $args = Array_merge (Array ($this), $args);
- if (Isset (static:: $extensions [$method]))
- {
- Return Call_user_func_array (static:: $extensions [$method], $args);
- }
- throw new Exception (' rule '. $method. ' Dose not exits ');
- }
- protected function validaterequired ($attr, $value)
- {
- Return!empty ($value);
- }
- protected function Validatelength ($attr, $value, $len)
- {
- return $len = = $min;
- }
- protected function Validatemin ($attr, $value, $len)
- {
- return strlen ($value) > $len;
- }
- protected function Validatemax ($attr, $value, $len)
- {
- return strlen ($value) < $len;
- }
- protected function Validatebetween ($attr, $value, $min, $max)
- {
- return $this->validatemin ($attr, $value, $min) && $this->validatemax ($attr, $value, $max);
- }
- protected function 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);
- }
- protected function Validatein ($attr, $value, $in _data)
- {
- $in _data = Explode (', ', $in _data);
- Return In_array ($value, $in _data);
- }
- protected function Validatenotin ($attr, $value, $in _data)
- {
- Return! $this->validatein ($attr, $value, $in _data);
- }
- protected function Validateeq ($attr, $value, $eq)
- {
- return $value = = $eq;
- }
- protected function validateconfirm ($attr, $value, $confirm)
- {
- return $this->validateeq ($attr, $value, $this->getvalue ($confirm));
- }
- protected function Validateurl ($attr, $value)
- {
- $regex = '/[a-za-z]+://[^\s]*/i ';
- return (BOOL) Preg_match ($regex, $value);
- }
- protected function 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);
- }
- }
Copy Code |