CommonRule.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\form;
  12. /**
  13. * Class CommonRule
  14. * @package crmeb\form
  15. */
  16. class CommonRule implements BuildInterface
  17. {
  18. /**
  19. * 验证类型
  20. */
  21. const VALIDATE_TYPE = ['string', 'number', 'boolean', 'method', 'regexp', 'integer', 'float', 'array', 'object', 'enum', 'date', 'url', 'hex', 'email'];
  22. /**
  23. * @var string
  24. */
  25. protected $type = '';
  26. /**
  27. * @var bool
  28. */
  29. protected $required = false;
  30. /**
  31. * @var string
  32. */
  33. protected $pattern = '';
  34. /**
  35. * 枚举值
  36. * @var array
  37. */
  38. protected $enum = [];
  39. /**
  40. * 提示语
  41. * @var string
  42. */
  43. protected $message = '';
  44. /**
  45. * 深度验证对象
  46. * @var array
  47. */
  48. protected $fields = [];
  49. /**
  50. * 验证触发方式
  51. * @var string
  52. */
  53. protected $trigger = 'blur';
  54. /**
  55. * @param string $type
  56. * @return $this
  57. */
  58. public function type(string $type)
  59. {
  60. $this->type = in_array($type, self::VALIDATE_TYPE) ? $type : null;
  61. if (!$this->type) {
  62. throw new FormValidate('验证类型错误');
  63. }
  64. return $this;
  65. }
  66. /**
  67. * 是否必填
  68. * @return $this
  69. */
  70. public function required()
  71. {
  72. $this->required = true;
  73. return $this;
  74. }
  75. /**
  76. * 设置错误提示
  77. * @param string $message
  78. * @return $this
  79. */
  80. public function message(string $message)
  81. {
  82. $this->message = $message;
  83. return $this;
  84. }
  85. /**
  86. * 提示语
  87. * @return string
  88. */
  89. public function getMessage()
  90. {
  91. return $this->message;
  92. }
  93. /**
  94. * 枚举数据
  95. * @param array $enum
  96. * @return $this
  97. */
  98. public function enum(array $enum)
  99. {
  100. $this->enum = $enum;
  101. return $this;
  102. }
  103. /**
  104. * 正则表达式
  105. * @param string $pattern
  106. * @return $this
  107. */
  108. public function pattern(string $pattern)
  109. {
  110. $this->pattern = $pattern;
  111. return $this;
  112. }
  113. /**
  114. * 验证规则
  115. * @param string|array|BuildInterface $field
  116. * @param array $rule
  117. * @return $this
  118. */
  119. public function field($field, array $rule = [])
  120. {
  121. if (!in_array($this->type, ['array', 'object'])) {
  122. throw new BuildException('无效规则,类型只能在array或者object情况下才可设置');
  123. }
  124. if ($this->type === 'array') {
  125. $rules = [];
  126. if ($field instanceof BuildInterface) {
  127. $rules = $field->toArray();
  128. }
  129. $this->fields[] = $rules;
  130. } else {
  131. $rules = [];
  132. foreach ($rule as $item) {
  133. if ($item instanceof BuildInterface) {
  134. $rules[] = $item->toArray();
  135. }
  136. }
  137. $this->fields[$field] = $rules;
  138. }
  139. return $this;
  140. }
  141. /**
  142. * 数据转换
  143. * @return array
  144. */
  145. public function toArray(): array
  146. {
  147. $data = [
  148. 'required' => $this->required,
  149. 'message' => $this->message,
  150. 'trigger' => $this->trigger,
  151. 'pattern' => $this->pattern,
  152. 'enum' => $this->enum,
  153. 'type' => $this->type
  154. ];
  155. $res = [];
  156. foreach ($data as $key => $value) {
  157. if (is_bool($value) || $value) {
  158. $res[$key] = $value;
  159. }
  160. }
  161. return $res;
  162. }
  163. }