PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/firstrend/src/core/service/validate.service.php

http://ownerpress.googlecode.com/
PHP | 200 lines | 148 code | 27 blank | 25 comment | 8 complexity | e2d039db5a58aa88fe08ce932a34b51c MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ?????????? (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * validate.service
  9. *
  10. * ???????
  11. *
  12. * @package service
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. class ValidateService
  16. {
  17. private $error = '';
  18. /**
  19. * ????
  20. * @param array $validate ????
  21. * @param array $data ??
  22. * @return bool
  23. */
  24. public function validation($validate,$data)
  25. {
  26. $is_check = true;
  27. foreach($validate as $val)
  28. {
  29. $key = $val[0];
  30. switch($val[1])
  31. {
  32. case 'min_length': // ?????????
  33. $is_check = $this->minLength($data[$key] ,$val[3]);
  34. break;
  35. case 'max_length': // ?????????
  36. $is_check = $this->maxLength($data[$key] ,$val[3]);
  37. break;
  38. case 'range_length': // ??????????????
  39. $is_check = $this->rangeLength($data[$key] ,$val[3],$val[4]);
  40. break;
  41. case 'min': // ???????
  42. $is_check = $this->min($data[$key] ,$val[3]);
  43. break;
  44. case 'max': // ???????
  45. $is_check = $this->max($data[$key] ,$val[3]);
  46. break;
  47. case 'range': // ??????????????
  48. $is_check = $this->range($data[$key] ,$val[3],$val[4]);
  49. break;
  50. case 'confirm': // ??????????
  51. $is_check = $data[$key] == $data[$val[3]];
  52. break;
  53. case 'in': // ?????????????
  54. $is_check = in_array($data[$key] ,$val[3]);
  55. break;
  56. case 'equal': // ?????????
  57. $is_check = $data[$key] == $val[3];
  58. break;
  59. case 'qq_msn': // ??QQ?MSN????
  60. $is_check = $this->qqMsn($data[$key]);
  61. break;
  62. case 'regex':
  63. default: // ???????? ???????????????
  64. // ??????
  65. $is_check = $this->regex($data[$key],$val[1]);
  66. break;
  67. }
  68. if(!$is_check)
  69. {
  70. $this->error = $val[2];
  71. break;
  72. }
  73. }
  74. return $is_check;
  75. }
  76. /**
  77. * ??????
  78. * @return string
  79. */
  80. public function getError()
  81. {
  82. return $this->error;
  83. }
  84. public function required($value)
  85. {
  86. return $this->regex($value,'required');
  87. }
  88. public function minLength($value,$length)
  89. {
  90. return $length <= getStrLen($value);
  91. }
  92. public function maxLength($value,$length)
  93. {
  94. return $length >= getStrLen($value);
  95. }
  96. public function rangeLength($value,$min_length,$max_length)
  97. {
  98. return $min_length <= getStrLen($value) && $max_length >= getStrLen($value);
  99. }
  100. public function min($value,$num)
  101. {
  102. return $num <= $value;
  103. }
  104. public function max($value,$num)
  105. {
  106. return $num >= $value;
  107. }
  108. public function range($value,$min_num,$max_num)
  109. {
  110. return $min_num <= $value && $max_num >= $value;
  111. }
  112. public function email($value)
  113. {
  114. return $this->regex($value,'email');
  115. }
  116. public function url($value)
  117. {
  118. return $this->regex($value,'url');
  119. }
  120. public function date($value)
  121. {
  122. return $this->regex($value,'date');
  123. }
  124. public function currency($value)
  125. {
  126. return $this->regex($value,'currency');
  127. }
  128. public function digits($value)
  129. {
  130. return $this->regex($value,'digits');
  131. }
  132. public function double($value)
  133. {
  134. return $this->regex($value,'double');
  135. }
  136. public function number($value)
  137. {
  138. return $this->regex($value,'number');
  139. }
  140. public function equal($value,$value1)
  141. {
  142. return $value == $value1;
  143. }
  144. public function qqMsn($value)
  145. {
  146. return $this->regex($value,'digits') || $this->regex($value,'email');
  147. }
  148. public function english($value)
  149. {
  150. return $this->regex($value,'english');
  151. }
  152. public function regex($value,$rule)
  153. {
  154. $validate = array(
  155. 'required'=> '/.+/',
  156. 'email' => "/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i",
  157. 'url' => "/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/i",
  158. 'date' => '/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}(?:|\s\d{1,2}:\d{1,2}(?:|:\d{1,2}))$/',
  159. 'currency' => '/^\d+(\.\d+)?$/',
  160. 'digits' => '/^\d+$/',
  161. 'number' => '/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/',
  162. 'zip' => '/^[1-9]\d{5}$/',
  163. 'integer' => '/^[-\+]?\d+$/',
  164. 'double' => '/^[-\+]?\d+(\.\d+)?$/',
  165. 'english' => '/^[A-Za-z]+$/',
  166. );
  167. // ?????????????
  168. if(isset($validate[strtolower($rule)]))
  169. $rule = $validate[strtolower($rule)];
  170. return preg_match($rule,$value)===1;
  171. }
  172. }
  173. ?>