0

0

一些简单的php工具类

php中文网

php中文网

发布时间:2016-07-25 08:48:16

|

1347人浏览过

|

来源于php中文网

原创

平时工作总结的一些常用工具,采用简单的实现方式,功能较为单一,没有复杂的依赖。

托管地址: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. }
复制代码


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
c语言项目php解释器源码分析探索
c语言项目php解释器源码分析探索

共7课时 | 0.3万人学习

ThinkPHP6.x 微实战--十天技能课堂
ThinkPHP6.x 微实战--十天技能课堂

共26课时 | 1.5万人学习

Vue.js 微实战--十天技能课堂
Vue.js 微实战--十天技能课堂

共18课时 | 1.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号