php验证类

WBOY
Freigeben: 2016-07-25 09:12:12
Original
1017 Leute haben es durchsucht
一个可扩展的php验证类,
类里面可以的各类验证可自行调整实现,现在为基本实现方式。
需要添加规则的话, 直接定义方法,方法名即为规则名称。具体参考使用方法。
  1. require_once('./Validator.class.php');
  2. $data = array(
  3. 'nickname' => 'heno' ,
  4. 'realname' => 'steven',
  5. 'age' => 25,
  6. 'mobile' => '1521060426');
  7. $validator = new Validator($data);
  8. $validator->setRule('nickname', 'required');
  9. $validator->setRule('realname', array('length' => array(1,6), 'required'));
  10. $validator->setRule('age', array('required', 'digit'));
  11. $validator->setRule('mobile', array('mobile'));
  12. $result = $validator->validate();
  13. var_dump($result);
  14. var_dump($validator->getResultInfo());
复制代码
  1. /**
  2. * Validator 数据验证类
  3. * @package library
  4. * @category library
  5. * @author Steven
  6. * @version 1.0
  7. */
  8. /**
  9. * Validator 数据验证类
  10. * @package library
  11. * @category library
  12. * @author Steven
  13. * @version 1.0
  14. */
  15. class Validator {
  16. /**
  17. * 待校验数据
  18. * @var array
  19. */
  20. private $_data;
  21. /**
  22. * 校验规则
  23. * @var array
  24. */
  25. private $_ruleList = null;
  26. /**
  27. * 校验结果
  28. * @var bool
  29. */
  30. private $_result = null;
  31. /**
  32. * 校验数据信息
  33. * @var array
  34. */
  35. private $_resultInfo = array();
  36. /**
  37. * 构造函数
  38. * @param array $data 待校验数据
  39. */
  40. public function __construct($data = null)
  41. {
  42. if ($data) {
  43. $this->_data = $data;
  44. }
  45. }
  46. /**
  47. * 设置校验规则
  48. * @param string $var 带校验项key
  49. * @param mixed $rule 校验规则
  50. * @return void
  51. */
  52. public function setRule($var, $rule)
  53. {
  54. $this->_ruleList[$var] = $rule;
  55. }
  56. /**
  57. * 检验数据
  58. * @param array $data
  59. * <li> * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);</li> <li> * $validator = new Validator($data);</li> <li> * $validator->setRule('nickname', 'required');</li> <li> * $validator->setRule('realname', array('lenght' => array(1,4), 'required'));</li> <li> * $validator->setRule('age', array('required', 'digit'));</li> <li> * $result = $validator->validate();</li> <li> * var_dump($validator->getResultInfo());</li> <li> * </li>
  60. * @return bool
  61. */
  62. public function validate($data = null)
  63. {
  64. $result = true;
  65. /* 如果没有设置校验规则直接返回 true */
  66. if ($this->_ruleList === null || !count($this->_ruleList)) {
  67. return $result;
  68. }
  69. /* 已经设置规则,则对规则逐条进行校验 */
  70. foreach ($this->_ruleList as $ruleKey => $ruleItem) {
  71. /* 如果检验规则为单条规则 */
  72. if (!is_array($ruleItem)) {
  73. $ruleItem = trim($ruleItem);
  74. if (method_exists($this, $ruleItem)) {
  75. /* 校验数据,保存校验结果 */
  76. $tmpResult = $this->$ruleItem($ruleKey);
  77. if (!$tmpResult) {
  78. $this->_resultInfo[$ruleKey][$ruleItem] = $tmpResult;
  79. $result = false;
  80. }
  81. }
  82. continue;
  83. }
  84. /* 校验规则为多条 */
  85. foreach ($ruleItem as $ruleItemKey => $rule) {
  86. if (!is_array($rule)) {
  87. $rule = trim($rule);
  88. if (method_exists($this, $rule)) {
  89. /* 校验数据,设置结果集 */
  90. $tmpResult = $this->$rule($ruleKey);
  91. if (!$tmpResult) {
  92. $this->_resultInfo[$ruleKey][$rule] = $tmpResult;
  93. $result = false;
  94. }
  95. }
  96. } else {
  97. if (method_exists($this, $ruleItemKey)) {
  98. /* 校验数据,设置结果集 */
  99. $tmpResult = $this->$ruleItemKey($ruleKey, $rule);
  100. if (!$tmpResult) {
  101. $this->_resultInfo[$ruleKey][$ruleItemKey] = $tmpResult;
  102. $result = false;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return $result;
  109. }
  110. /**
  111. * 获取校验结果数据
  112. * @return [type] [description]
  113. */
  114. public function getResultInfo()
  115. {
  116. return $this->_resultInfo;
  117. }
  118. /**
  119. * 校验必填参数
  120. * @param string $varName 校验项
  121. * @return bool
  122. */
  123. public function required($varName)
  124. {
  125. $result = false;
  126. if (is_array($this->_data) && isset($this->_data[$varName])) {
  127. $result = true;
  128. }
  129. return $result;
  130. }
  131. /**
  132. * 校验参数长度
  133. *
  134. * @param string $varName 校验项
  135. * @param array $lengthData array($minLen, $maxLen)
  136. * @return bool
  137. */
  138. public function length($varName, $lengthData)
  139. {
  140. $result = true;
  141. /* 如果该项没有设置,默认为校验通过 */
  142. if ($this->required($varName)) {
  143. $varLen = mb_strlen($this->_data[$varName]);
  144. $minLen = $lengthData[0];
  145. $maxLen = $lengthData[1];
  146. if ($varLen $maxLen) {
  147. $result = true;
  148. }
  149. }
  150. return $result;
  151. }
  152. /**
  153. * 校验邮件
  154. * @param string $varName 校验项
  155. * @return bool
  156. */
  157. public function email($varName)
  158. {
  159. $result = true;
  160. /* 如果该项没有设置,默认为校验通过 */
  161. if ($this->required($varName)) {
  162. $email = trim($this->_data[$varName]);
  163. if (preg_match('/^[-\w]+?@[-\w.]+?$/', $email)) {
  164. $result = false;
  165. }
  166. }
  167. return $result;
  168. }
  169. /**
  170. * 校验手机
  171. * @param string $varName 校验项
  172. * @return bool
  173. */
  174. public function mobile($varName)
  175. {
  176. $result = true;
  177. /* 如果该项没有设置,默认为校验通过 */
  178. if ($this->required($varName)) {
  179. $mobile = trim($this->_data[$varName]);
  180. if (!preg_match('/^1[3458]\d{10}$/', $mobile)) {
  181. $result = false;
  182. }
  183. }
  184. return $result;
  185. }
  186. /**
  187. * 校验参数为数字
  188. * @param string $varName 校验项
  189. * @return bool
  190. */
  191. public function digit($varName)
  192. {
  193. $result = false;
  194. if ($this->required($varName) && is_numeric($this->_data[$varName])) {
  195. $result = true;
  196. }
  197. return $result;
  198. }
  199. /**
  200. * 校验参数为身份证
  201. * @param string $varName 校验项
  202. * @return bool
  203. */
  204. public function ID($ID)
  205. {
  206. }
  207. /**
  208. * 校验参数为URL
  209. * @param string $varName 校验项
  210. * @return bool
  211. */
  212. public function url($url)
  213. {
  214. $result = true;
  215. /* 如果该项没有设置,默认为校验通过 */
  216. if ($this->required($varName)) {
  217. $url = trim($this->_data[$varName]);
  218. if(!preg_match('/^(http[s]?::)?\w+?(\.\w+?)$/', $url)) {
  219. $result = false;
  220. }
  221. }
  222. return $result;
  223. }
  224. }
  225. ?>
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!