Some Simple PHP tool classes

Source: Internet
Author: User
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
  1. /**
  2. * Authenticator
  3. * @author Cao
  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 validated
  16. * @var Array
  17. */
  18. protected $data;
  19. /**
  20. * Validation rules
  21. * @var Array
  22. */
  23. protected $rule;
  24. /**
  25. * Error message
  26. * @var Array
  27. */
  28. protected $messages;
  29. /**
  30. * Custom Error messages
  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 Validation 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. * Bulk Increase 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 exits ');
  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. }
Copy Code
  • 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.