一些简单的php工具类

WBOY
Lepaskan: 2016-07-25 08:48:16
asal
1102 orang telah melayarinya
平时工作总结的一些常用工具,采用简单的实现方式,功能较为单一,没有复杂的依赖。

托管地址:http://git.oschina.net/caoyong2619/php-utils.git
  1. /**
  2. * 验证器
  3. * @author 曹勇
  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. * 待验证数据
  16. * @var array
  17. */
  18. protected $data;
  19. /**
  20. * 验证规则
  21. * @var array
  22. */
  23. protected $rule;
  24. /**
  25. * 错误信息
  26. * @var array
  27. */
  28. protected $messages;
  29. /**
  30. * 自定义错误信息
  31. * @var array
  32. */
  33. protected $custom_messages;
  34. /**
  35. * 扩展规则
  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. * 扩展验证规则
  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. * 批量增加扩展规则
  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) }
  181. protected function ValidateBetween($attr,$value,$min,$max)
  182. {
  183. return $this->validateMin($attr, $value, $min) && $this->validateMax($attr, $value, $max);
  184. }
  185. protected function validateEmail($attr,$value)
  186. {
  187. $regex = '/[\w!#$%&\'*+\/=?^_`{|}~-]+(?:\.[\w!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/i';
  188. return (bool)preg_match($regex, $value);
  189. }
  190. protected function validateNumber($attr,$value)
  191. {
  192. return is_numeric($value);
  193. }
  194. protected function validateIn($attr,$value,$in_data)
  195. {
  196. $in_data = explode(',', $in_data);
  197. return in_array($value, $in_data);
  198. }
  199. protected function validateNotin($attr,$value,$in_data)
  200. {
  201. return !$this->validateIn($attr, $value, $in_data);
  202. }
  203. protected function validateEq($attr,$value,$eq)
  204. {
  205. return $value == $eq;
  206. }
  207. protected function validateConfirm($attr,$value,$confirm)
  208. {
  209. return $this->validateEq($attr, $value, $this->getValue($confirm));
  210. }
  211. protected function validateUrl($attr,$value)
  212. {
  213. $regex = '/[a-zA-z]+://[^\s]*/i';
  214. return (bool)preg_match($regex, $value);
  215. }
  216. protected function validateMobile($attr,$value)
  217. {
  218. return preg_match('/1(3|4|5|8})\d{9}/',$value);
  219. }
  220. protected function validateQQ($attr,$value)
  221. {
  222. return preg_match('/\d{5,}/', $value);
  223. }
  224. }
复制代码


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan