Validator.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. class LtValidator
  3. {
  4. public $configHandle;
  5. protected $errorMessages;
  6. public function __construct()
  7. {
  8. if (! $this->configHandle instanceof LtConfig)
  9. {
  10. if (class_exists("LtObjectUtil", false))
  11. {
  12. $this->configHandle = LtObjectUtil::singleton("LtConfig");
  13. }
  14. else
  15. {
  16. $this->configHandle = new LtConfig;
  17. }
  18. }
  19. }
  20. public function init()
  21. {
  22. $this->errorMessages = $this->configHandle->get('validator.error_messages');
  23. }
  24. /**
  25. * Validate an element
  26. *
  27. * @param mixed $value
  28. * @param array $dtd
  29. * @return array
  30. */
  31. public function validate($value, $dtd)
  32. {
  33. $errorMessages = array();
  34. $label = $dtd->label;
  35. if (is_array($dtd->rules) && count($dtd->rules))
  36. {
  37. $messages = isset($dtd->messages) ? $dtd->messages : array();
  38. foreach ($dtd->rules as $key => $val)
  39. {
  40. // callback_user_function
  41. if ('callback_' == substr($key, 0, 9))
  42. {
  43. $method = substr($key, 9);
  44. // 定义了过程函数
  45. if (function_exists($method))
  46. {
  47. if (!$method($value, $dtd->rules[$key]))
  48. {
  49. if (isset($this->errorMessages[$key]))
  50. {
  51. $messages[$key] = $this->errorMessages[$key];
  52. }
  53. else
  54. {
  55. $messages[$key] = "validator.error_messages[$key] empty";
  56. }
  57. $errorMessages[$key] = sprintf($messages[$key], $label, $dtd->rules[$key]);
  58. }
  59. continue;
  60. }
  61. // 定义了类方法
  62. $rc = new ReflectionClass($val);
  63. if ($rc->hasMethod($method))
  64. {
  65. $rcMethod = $rc->getMethod($method);
  66. if ($rcMethod->isStatic())
  67. {
  68. $ret = $rcMethod->invoke(null, $value, $dtd->rules[$key]);
  69. }
  70. else
  71. {
  72. // 非静态方法需要一个实例 有待考虑单例
  73. $rcInstance = $rc->newInstance();
  74. $ret = $rcMethod->invoke($rcInstance, $value, $dtd->rules[$key]);
  75. }
  76. if (!$ret)
  77. {
  78. if (isset($this->errorMessages[$key]))
  79. {
  80. $messages[$key] = $this->errorMessages[$key];
  81. }
  82. else
  83. {
  84. $messages[$key] = "validator.error_messages[$key] empty";
  85. }
  86. $errorMessages[$key] = sprintf($messages[$key], $label, $dtd->rules[$key]);
  87. }
  88. continue;
  89. }
  90. continue;
  91. }
  92. // end callback_user_function
  93. $validateFunction = '_' . $key;
  94. if ((is_bool($dtd->rules[$key]) || 0 < strlen($dtd->rules[$key])) && !$this->$validateFunction($value, $dtd->rules[$key]))
  95. {
  96. if (empty($messages[$key]))
  97. {
  98. if (isset($this->errorMessages[$key]))
  99. {
  100. $messages[$key] = $this->errorMessages[$key];
  101. }
  102. else
  103. {
  104. $messages[$key] = "validator.error_messages[$key] empty";
  105. }
  106. }
  107. $errorMessages[$key] = sprintf($messages[$key], $label, $dtd->rules[$key]);
  108. }
  109. }
  110. }
  111. return $errorMessages;
  112. }
  113. protected function _ban($value, $ruleValue)
  114. {
  115. return !preg_match($ruleValue, $value);
  116. }
  117. protected function _mask($value, $ruleValue)
  118. {
  119. return preg_match($ruleValue, $value);
  120. }
  121. protected function _equal_to($value, $ruleValue)
  122. {
  123. return $value === $ruleValue;
  124. }
  125. protected function _max_length($value, $ruleValue)
  126. {
  127. return mb_strlen($value) <= $ruleValue;
  128. }
  129. protected function _min_length($value, $ruleValue)
  130. {
  131. return mb_strlen($value) >= $ruleValue;
  132. }
  133. protected function _max_value($value, $ruleValue)
  134. {
  135. return $value <= $ruleValue;
  136. }
  137. protected function _min_value($value, $ruleValue)
  138. {
  139. return $value >= $ruleValue;
  140. }
  141. protected function _min_selected($value, $ruleValue)
  142. {
  143. return count($value) >= $ruleValue;
  144. }
  145. protected function _max_selected($value, $ruleValue)
  146. {
  147. return count($value) <= $ruleValue;
  148. }
  149. protected function _required($value, $ruleValue)
  150. {
  151. if (false == $ruleValue)
  152. {
  153. return true;
  154. }
  155. else
  156. {
  157. return is_array($value) && count($value) || strlen($value);
  158. }
  159. }
  160. }