Some simple php Tools

Source: Internet
Author: User
Some simple php Tools
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

  1. /**
  2. * Validators
  3. * @ Author Cao Yong
  4. * @ Example
  5. * $ Data = array ('username' => 'caoyong ', 'password' => '');
  6. * $ Rules = array ('username' => 'Require ', 'password' => 'Require ');
  7. * $ Validator = new Validator ($ data, $ rules );
  8. * $ Is_pass = $ validator-> passed ();
  9. * $ Is_fail = $ validator-> failed ();
  10. * $ Message = $ validator-> messages ();;
  11. */
  12. Class Validator
  13. {
  14. /**
  15. * Data to be verified
  16. * @ Var array
  17. */
  18. Protected $ data;
  19. /**
  20. * Verification rules
  21. * @ Var array
  22. */
  23. Protected $ rule;
  24. /**
  25. * Error message
  26. * @ Var array
  27. */
  28. Protected $ messages;
  29. /**
  30. * Custom error message
  31. * @ Var array
  32. */
  33. Protected $ custom_messages;
  34. /**
  35. * Extended rules
  36. * @ Var array
  37. */
  38. Protected static $ extensions = array ();
  39. Public function _ construct (array $ data, array $ rule, array $ messages = array ())
  40. {
  41. $ This-> setData ($ data );
  42. $ This-> setRule ($ rule );
  43. $ This-> setMessages ($ messages );
  44. }
  45. Public function setData (array $ data)
  46. {
  47. $ This-> data = $ data;
  48. }
  49. Public function setRule (array $ rule)
  50. {
  51. $ This-> rule = $ rule;
  52. }
  53. Public function setMessages (array $ messages)
  54. {
  55. $ This-> custom_messages = $ messages;
  56. }
  57. Protected function validate ($ attr, $ rule)
  58. {
  59. If (is_array ($ rule ))
  60. {
  61. Foreach ($ rule as $ v)
  62. {
  63. If (false ===$ this-> validate ($ attr, $ v ))
  64. Break;
  65. }
  66. }
  67. Else
  68. {
  69. List ($ rule, $ args) = $ this-> parseRule ($ rule );
  70. $ Method = 'validate'. $ rule;
  71. $ Args = array_merge (array ($ attr, $ this-> getValue ($ attr), $ args );
  72. $ Result = call_user_func_array (array ($ this, $ method), $ args );
  73. If (false ===$ result)
  74. {
  75. $ Rule = lcfirst ($ rule );
  76. If (isset ($ this-> custom_messages [$ attr])
  77. {
  78. If (is_array ($ this-> custom_messages [$ attr]) & isset ($ this-> custom_messages [$ attr] [$ rule])
  79. {
  80. $ Message = $ this-> custom_messages [$ attr] [$ rule];
  81. }
  82. Else
  83. If (is_string ($ this-> custom_messages [$ attr])
  84. {
  85. $ Message = $ this-> custom_messages [$ attr];
  86. }
  87. Else
  88. {
  89. $ Message = $ attr. 'return failed in Rule'. $ rule;
  90. }
  91. }
  92. Else
  93. $ Message = $ attr. 'return failed in Rule'. $ rule;
  94. $ This-> messages [$ attr] = $ message;
  95. }
  96. Return $ result;
  97. }
  98. }
  99. Public function passed ()
  100. {
  101. Foreach ($ this-> rule as $ attr => $ rule)
  102. {
  103. $ This-> validate ($ attr, $ rule );
  104. }
  105. Return 0 = count ($ this-> messages );
  106. }
  107. Public function failed ()
  108. {
  109. Return! $ This-> passed ();
  110. }
  111. Public function messages ($ key = false)
  112. {
  113. If ($ key & isset ($ this-> messages [$ key])
  114. Return $ this-> messages [$ key];
  115. Return $ this-> messages;
  116. }
  117. Protected function parseRule ($ rule)
  118. {
  119. If (false! = Strpos ($ rule, '| '))
  120. {
  121. List ($ rulename, $ args) = explode ('|', $ rule );
  122. $ Args = explode (':', $ args );
  123. }
  124. Else
  125. {
  126. $ Rulename = $ rule;
  127. $ Args = array ();
  128. }
  129. Return array (ucfirst ($ rulename), $ args );
  130. }
  131. Protected function getValue ($ attr)
  132. {
  133. If (! Is_null ($ value = $ this-> data [$ attr])
  134. Return $ value;
  135. }
  136. /**
  137. * Extended verification rules
  138. * @ Param string $ name
  139. * @ Param Closure $ rule
  140. */
  141. Public static function addExtension ($ name, Closure $ rule)
  142. {
  143. Static: $ extensions [$ name] = $ rule;
  144. }
  145. /**
  146. * Batch add extension rules
  147. * @ Param $ rules array
  148. */
  149. Public static function addExtensions (array $ rules)
  150. {
  151. Foreach ($ rules as $ k => $ v)
  152. {
  153. Static: addExtenstion ($ k, $ v );
  154. }
  155. }
  156. Public function _ call ($ method, $ args)
  157. {
  158. $ Method = lcfirst (substr ($ method, 8 ));
  159. $ Args = array_merge (array ($ this), $ args );
  160. If (isset (static: $ extensions [$ method])
  161. {
  162. Return call_user_func_array (static: $ extensions [$ method], $ args );
  163. }
  164. Throw new Exception ('rule'. $ method. 'dose not exists ');
  165. }
  166. Protected function validateRequired ($ attr, $ value)
  167. {
  168. Return! Empty ($ value );
  169. }
  170. Protected function validateLength ($ attr, $ value, $ len)
  171. {
  172. Return $ len = $ min;
  173. }
  174. Protected function validateMin ($ attr, $ value, $ len)
  175. {
  176. Return strlen ($ value)> $ len;
  177. }
  178. Protected function validateMax ($ attr, $ value, $ len)
  179. {
  180. Return strlen ($ value) <$ len;
  181. }
  182. Protected function ValidateBetween ($ attr, $ value, $ min, $ max)
  183. {
  184. Return $ this-> validateMin ($ attr, $ value, $ min) & $ this-> validateMax ($ attr, $ value, $ max );
  185. }
  186. Protected function validateEmail ($ attr, $ value)
  187. {
  188. $ Regex = '/[\ w! # $ % & \ '* + \/=? ^ _ '{| }~ -] + (? : \. [\ W! # $ % & \ '* + \/=? ^ _ '{| }~ -] + )*@(? : [\ W] (? : [\ W-] * [\ w])? \.) + [\ W] (? : [\ W-] * [\ w])? /I ';
  189. Return (bool) preg_match ($ regex, $ value );
  190. }
  191. Protected function validateNumber ($ attr, $ value)
  192. {
  193. Return is_numeric ($ value );
  194. }
  195. Protected function validateIn ($ attr, $ value, $ in_data)
  196. {
  197. $ In_data = explode (',', $ in_data );
  198. Return in_array ($ value, $ in_data );
  199. }
  200. Protected function validateNotin ($ attr, $ value, $ in_data)
  201. {
  202. Return! $ This-> validateIn ($ attr, $ value, $ in_data );
  203. }
  204. Protected function validateEq ($ attr, $ value, $ eq)
  205. {
  206. Return $ value = $ eq;
  207. }
  208. Protected function validateConfirm ($ attr, $ value, $ confirm)
  209. {
  210. Return $ this-> validateEq ($ attr, $ value, $ this-> getValue ($ confirm ));
  211. }
  212. Protected function validateUrl ($ attr, $ value)
  213. {
  214. $ Regex = '/[a-zA-z] +: // [^ \ s] */I ';
  215. Return (bool) preg_match ($ regex, $ value );
  216. }
  217. Protected function validateMobile ($ attr, $ value)
  218. {
  219. Return preg_match ('/1 (3 | 4 | 5 | 8}) \ d {9}/', $ value );
  220. }
  221. Protected function validateQQ ($ attr, $ value)
  222. {
  223. Return preg_match ('/\ d {5,}/', $ value );
  224. }
  225. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.