Php verification class

Source: Internet
Author: User
Php verification class
A scalable php verification class,
You can adjust the implementation of various types of verification in the class. now it is the basic implementation method.
To add a rule, define the method directly. the method name is the rule name. For details, refer to usage.

  1. Require_once ('./Validator. class. php ');
  2. $ Data = array (
  3. 'Nickname' => 'heno ',
  4. 'Realname' => 'Steven ',
  5. 'Age' => 25,
  6. 'Mobile' => '123 ');
  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 ('requestred', 'digit '));
  11. $ Validator-> setRule ('mobile', array ('mobile '));
  12. $ Result = $ validator-> validate ();
  13. Var_dump ($ result );
  14. Var_dump ($ validator-> getResultInfo ());

  1. /**
  2. * Validator data verification class
  3. * @ Package library
  4. * @ Category library
  5. * @ Author Steven
  6. * @ Version 1.0
  7. */
  8. /**
  9. * Validator data verification class
  10. * @ Package library
  11. * @ Category library
  12. * @ Author Steven
  13. * @ Version 1.0
  14. */
  15. Class Validator {
  16. /**
  17. * Data to be verified
  18. * @ Var array
  19. */
  20. Private $ _ data;
  21. /**
  22. * Validation rules
  23. * @ Var array
  24. */
  25. Private $ _ ruleList = null;
  26. /**
  27. * Verification result
  28. * @ Var bool
  29. */
  30. Private $ _ result = null;
  31. /**
  32. * Verify data information
  33. * @ Var array
  34. */
  35. Private $ _ resultInfo = array ();
  36. /**
  37. * Constructor
  38. * @ Param array $ data to be verified
  39. */
  40. Public function _ construct ($ data = null)
  41. {
  42. If ($ data ){
  43. $ This-> _ data = $ data;
  44. }
  45. }
  46. /**
  47. * Set verification rules
  48. * @ Param string $ var with the verification key
  49. * @ Param mixed $ rule verification rule
  50. * @ Return void
  51. */
  52. Public function setRule ($ var, $ rule)
  53. {
  54. $ This-> _ ruleList [$ var] = $ rule;
  55. }
  56. /**
  57. * Test data
  58. * @ Param array $ data
  59. *
  60. * $data = array('nickname' => 'heno' , 'realname' => 'steven', 'age' => 25);
  61. * $validator = new Validator($data);
  62. * $validator->setRule('nickname', 'required');
  63. * $validator->setRule('realname', array('lenght' => array(1,4), 'required'));
  64. * $validator->setRule('age', array('required', 'digit'));
  65. * $result = $validator->validate();
  66. * var_dump($validator->getResultInfo());
  67. *
  68. * @ Return bool
  69. */
  70. Public function validate ($ data = null)
  71. {
  72. $ Result = true;
  73. /* Return true if no validation rule is set */
  74. If ($ this-> _ ruleList === null |! Count ($ this-> _ ruleList )){
  75. Return $ result;
  76. }
  77. /* If rules have been set, check the rules one by one */
  78. Foreach ($ this-> _ ruleList as $ ruleKey => $ ruleItem ){
  79. /* If the validation rule is a single rule */
  80. If (! Is_array ($ ruleItem )){
  81. $ RuleItem = trim ($ ruleItem );
  82. If (method_exists ($ this, $ ruleItem )){
  83. /* Verify the data and save the verification result */
  84. $ TmpResult = $ this-> $ ruleItem ($ ruleKey );
  85. If (! $ TmpResult ){
  86. $ This-> _ resultInfo [$ ruleKey] [$ ruleItem] = $ tmpResult;
  87. $ Result = false;
  88. }
  89. }
  90. Continue;
  91. }
  92. /* Multiple validation rules */
  93. Foreach ($ ruleItem as $ ruleItemKey => $ rule ){
  94. If (! Is_array ($ rule )){
  95. $ Rule = trim ($ rule );
  96. If (method_exists ($ this, $ rule )){
  97. /* Verify the data and set the result set */
  98. $ TmpResult = $ this-> $ rule ($ ruleKey );
  99. If (! $ TmpResult ){
  100. $ This-> _ resultInfo [$ ruleKey] [$ rule] = $ tmpResult;
  101. $ Result = false;
  102. }
  103. }
  104. } Else {
  105. If (method_exists ($ this, $ ruleItemKey )){
  106. /* Verify the data and set the result set */
  107. $ TmpResult = $ this-> $ ruleItemKey ($ ruleKey, $ rule );
  108. If (! $ TmpResult ){
  109. $ This-> _ resultInfo [$ ruleKey] [$ ruleItemKey] = $ tmpResult;
  110. $ Result = false;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. Return $ result;
  117. }
  118. /**
  119. * Obtain verification result data
  120. * @ Return [type] [description]
  121. */
  122. Public function getResultInfo ()
  123. {
  124. Return $ this-> _ resultInfo;
  125. }
  126. /**
  127. * Required parameters for verification
  128. * @ Param string $ varName verification item
  129. * @ Return bool
  130. */
  131. Public function required ($ varName)
  132. {
  133. $ Result = false;
  134. If (is_array ($ this-> _ data) & isset ($ this-> _ data [$ varName]) {
  135. $ Result = true;
  136. }
  137. Return $ result;
  138. }
  139. /**
  140. * Verification parameter length
  141. *
  142. * @ Param string $ varName verification item
  143. * @ Param array $ lengthData array ($ minLen, $ maxLen)
  144. * @ Return bool
  145. */
  146. Public function length ($ varName, $ lengthData)
  147. {
  148. $ Result = true;
  149. /* If this item is not set, it is verified by default */
  150. If ($ this-> required ($ varName )){
  151. $ VarLen = mb_strlen ($ this-> _ data [$ varName]);
  152. $ MinLen = $ lengthData [0];
  153. $ MaxLen = $ lengthData [1];
  154. If ($ varLen <$ minLen | $ varLen> $ maxLen ){
  155. $ Result = true;
  156. }
  157. }
  158. Return $ result;
  159. }
  160. /**
  161. * Verify the email
  162. * @ Param string $ varName verification item
  163. * @ Return bool
  164. */
  165. Public function email ($ varName)
  166. {
  167. $ Result = true;
  168. /* If this item is not set, it is verified by default */
  169. If ($ this-> required ($ varName )){
  170. $ Email = trim ($ this-> _ data [$ varName]);
  171. If (preg_match ('/^ [-\ w] +? @ [-\ W.] +? $/', $ Email )){
  172. $ Result = false;
  173. }
  174. }
  175. Return $ result;
  176. }
  177. /**
  178. * Verify mobile phone number
  179. * @ Param string $ varName verification item
  180. * @ Return bool
  181. */
  182. Public function mobile ($ varName)
  183. {
  184. $ Result = true;
  185. /* If this item is not set, it is verified by default */
  186. If ($ this-> required ($ varName )){
  187. $ Mobile = trim ($ this-> _ data [$ varName]);
  188. If (! Preg_match ('/^ 1 [3458] \ d {10} $/', $ mobile )){
  189. $ Result = false;
  190. }
  191. }
  192. Return $ result;
  193. }
  194. /**
  195. * The verification parameter is a number.
  196. * @ Param string $ varName verification item
  197. * @ Return bool
  198. */
  199. Public function digit ($ varName)
  200. {
  201. $ Result = false;
  202. If ($ this-> required ($ varName) & is_numeric ($ this-> _ data [$ varName]) {
  203. $ Result = true;
  204. }
  205. Return $ result;
  206. }
  207. /**
  208. * The verification parameter is ID card.
  209. * @ Param string $ varName verification item
  210. * @ Return bool
  211. */
  212. Public function ID ($ ID)
  213. {
  214. }
  215. /**
  216. * The verification parameter is URL.
  217. * @ Param string $ varName verification item
  218. * @ Return bool
  219. */
  220. Public function url ($ url)
  221. {
  222. $ Result = true;
  223. /* If this item is not set, it is verified by default */
  224. If ($ this-> required ($ varName )){
  225. $ Url = trim ($ this-> _ data [$ varName]);
  226. If (! Preg_match ('/^ (http [s]? ::)? \ W +? (\. \ W + ?) $/', $ Url )){
  227. $ Result = false;
  228. }
  229. }
  230. Return $ result;
  231. }
  232. }
  233. ?>

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.