Some common tools used in daily work summary adopt simple implementation methods, with a single function and no complex dependencies.
Managed address: http://git.oschina.net/caoyong2619/php-utils.git
- /**
- * Validators
- * @ Author Cao Yong
- * @ 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 verified
- * @ Var array
- */
- Protected $ data;
- /**
- * Verification rules
- * @ Var array
- */
- Protected $ rule;
- /**
- * Error message
- * @ Var array
- */
- Protected $ messages;
- /**
- * Custom error message
- * @ 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 verification rules
- * @ Param string $ name
- * @ Param Closure $ rule
- */
- Public static function addExtension ($ name, Closure $ rule)
- {
- Static: $ extensions [$ name] = $ rule;
- }
- /**
- * Batch add 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 exists ');
- }
- 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 );
- }
- }
|