ValidateRule.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\validate;
  12. /**
  13. * Class ValidateRule
  14. * @package think\validate
  15. * @method ValidateRule confirm(mixed $rule, string $msg = '') static 验证是否和某个字段的值一致
  16. * @method ValidateRule different(mixed $rule, string $msg = '') static 验证是否和某个字段的值是否不同
  17. * @method ValidateRule egt(mixed $rule, string $msg = '') static 验证是否大于等于某个值
  18. * @method ValidateRule gt(mixed $rule, string $msg = '') static 验证是否大于某个值
  19. * @method ValidateRule elt(mixed $rule, string $msg = '') static 验证是否小于等于某个值
  20. * @method ValidateRule lt(mixed $rule, string $msg = '') static 验证是否小于某个值
  21. * @method ValidateRule eg(mixed $rule, string $msg = '') static 验证是否等于某个值
  22. * @method ValidateRule in(mixed $rule, string $msg = '') static 验证是否在范围内
  23. * @method ValidateRule notIn(mixed $rule, string $msg = '') static 验证是否不在某个范围
  24. * @method ValidateRule between(mixed $rule, string $msg = '') static 验证是否在某个区间
  25. * @method ValidateRule notBetween(mixed $rule, string $msg = '') static 验证是否不在某个区间
  26. * @method ValidateRule length(mixed $rule, string $msg = '') static 验证数据长度
  27. * @method ValidateRule max(mixed $rule, string $msg = '') static 验证数据最大长度
  28. * @method ValidateRule min(mixed $rule, string $msg = '') static 验证数据最小长度
  29. * @method ValidateRule after(mixed $rule, string $msg = '') static 验证日期
  30. * @method ValidateRule before(mixed $rule, string $msg = '') static 验证日期
  31. * @method ValidateRule expire(mixed $rule, string $msg = '') static 验证有效期
  32. * @method ValidateRule allowIp(mixed $rule, string $msg = '') static 验证IP许可
  33. * @method ValidateRule denyIp(mixed $rule, string $msg = '') static 验证IP禁用
  34. * @method ValidateRule regex(mixed $rule, string $msg = '') static 使用正则验证数据
  35. * @method ValidateRule token(mixed $rule='__token__', string $msg = '') static 验证表单令牌
  36. * @method ValidateRule is(mixed $rule, string $msg = '') static 验证字段值是否为有效格式
  37. * @method ValidateRule isRequire(mixed $rule = null, string $msg = '') static 验证字段必须
  38. * @method ValidateRule isNumber(mixed $rule = null, string $msg = '') static 验证字段值是否为数字
  39. * @method ValidateRule isArray(mixed $rule = null, string $msg = '') static 验证字段值是否为数组
  40. * @method ValidateRule isInteger(mixed $rule = null, string $msg = '') static 验证字段值是否为整形
  41. * @method ValidateRule isFloat(mixed $rule = null, string $msg = '') static 验证字段值是否为浮点数
  42. * @method ValidateRule isMobile(mixed $rule = null, string $msg = '') static 验证字段值是否为手机
  43. * @method ValidateRule isIdCard(mixed $rule = null, string $msg = '') static 验证字段值是否为身份证号码
  44. * @method ValidateRule isChs(mixed $rule = null, string $msg = '') static 验证字段值是否为中文
  45. * @method ValidateRule isChsDash(mixed $rule = null, string $msg = '') static 验证字段值是否为中文字母及下划线
  46. * @method ValidateRule isChsAlpha(mixed $rule = null, string $msg = '') static 验证字段值是否为中文和字母
  47. * @method ValidateRule isChsAlphaNum(mixed $rule = null, string $msg = '') static 验证字段值是否为中文字母和数字
  48. * @method ValidateRule isDate(mixed $rule = null, string $msg = '') static 验证字段值是否为有效格式
  49. * @method ValidateRule isBool(mixed $rule = null, string $msg = '') static 验证字段值是否为布尔值
  50. * @method ValidateRule isAlpha(mixed $rule = null, string $msg = '') static 验证字段值是否为字母
  51. * @method ValidateRule isAlphaDash(mixed $rule = null, string $msg = '') static 验证字段值是否为字母和下划线
  52. * @method ValidateRule isAlphaNum(mixed $rule = null, string $msg = '') static 验证字段值是否为字母和数字
  53. * @method ValidateRule isAccepted(mixed $rule = null, string $msg = '') static 验证字段值是否为yes, on, 或是 1
  54. * @method ValidateRule isEmail(mixed $rule = null, string $msg = '') static 验证字段值是否为有效邮箱格式
  55. * @method ValidateRule isUrl(mixed $rule = null, string $msg = '') static 验证字段值是否为有效URL地址
  56. * @method ValidateRule activeUrl(mixed $rule, string $msg = '') static 验证是否为合格的域名或者IP
  57. * @method ValidateRule ip(mixed $rule, string $msg = '') static 验证是否有效IP
  58. * @method ValidateRule fileExt(mixed $rule, string $msg = '') static 验证文件后缀
  59. * @method ValidateRule fileMime(mixed $rule, string $msg = '') static 验证文件类型
  60. * @method ValidateRule fileSize(mixed $rule, string $msg = '') static 验证文件大小
  61. * @method ValidateRule image(mixed $rule, string $msg = '') static 验证图像文件
  62. * @method ValidateRule method(mixed $rule, string $msg = '') static 验证请求类型
  63. * @method ValidateRule dateFormat(mixed $rule, string $msg = '') static 验证时间和日期是否符合指定格式
  64. * @method ValidateRule unique(mixed $rule, string $msg = '') static 验证是否唯一
  65. * @method ValidateRule behavior(mixed $rule, string $msg = '') static 使用行为类验证
  66. * @method ValidateRule filter(mixed $rule, string $msg = '') static 使用filter_var方式验证
  67. * @method ValidateRule requireIf(mixed $rule, string $msg = '') static 验证某个字段等于某个值的时候必须
  68. * @method ValidateRule requireCallback(mixed $rule, string $msg = '') static 通过回调方法验证某个字段是否必须
  69. * @method ValidateRule requireWith(mixed $rule, string $msg = '') static 验证某个字段有值的情况下必须
  70. * @method ValidateRule must(mixed $rule = null, string $msg = '') static 必须验证
  71. */
  72. class ValidateRule
  73. {
  74. // 验证字段的名称
  75. protected $title;
  76. // 当前验证规则
  77. protected $rule = [];
  78. // 验证提示信息
  79. protected $message = [];
  80. /**
  81. * 添加验证因子
  82. * @access protected
  83. * @param string $name 验证名称
  84. * @param mixed $rule 验证规则
  85. * @param string $msg 提示信息
  86. * @return $this
  87. */
  88. protected function addItem($name, $rule = null, $msg = '')
  89. {
  90. if ($rule || 0 === $rule) {
  91. $this->rule[$name] = $rule;
  92. } else {
  93. $this->rule[] = $name;
  94. }
  95. $this->message[] = $msg;
  96. return $this;
  97. }
  98. /**
  99. * 获取验证规则
  100. * @access public
  101. * @return array
  102. */
  103. public function getRule()
  104. {
  105. return $this->rule;
  106. }
  107. /**
  108. * 获取验证字段名称
  109. * @access public
  110. * @return string
  111. */
  112. public function getTitle()
  113. {
  114. return $this->title;
  115. }
  116. /**
  117. * 获取验证提示
  118. * @access public
  119. * @return array
  120. */
  121. public function getMsg()
  122. {
  123. return $this->message;
  124. }
  125. /**
  126. * 设置验证字段名称
  127. * @access public
  128. * @return $this
  129. */
  130. public function title($title)
  131. {
  132. $this->title = $title;
  133. return $this;
  134. }
  135. public function __call($method, $args)
  136. {
  137. if ('is' == strtolower(substr($method, 0, 2))) {
  138. $method = substr($method, 2);
  139. }
  140. array_unshift($args, lcfirst($method));
  141. return call_user_func_array([$this, 'addItem'], $args);
  142. }
  143. public static function __callStatic($method, $args)
  144. {
  145. $rule = new static();
  146. if ('is' == strtolower(substr($method, 0, 2))) {
  147. $method = substr($method, 2);
  148. }
  149. array_unshift($args, lcfirst($method));
  150. return call_user_func_array([$rule, 'addItem'], $args);
  151. }
  152. }