ValidateRule.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. * @document http://php.form-create.com
  11. */
  12. namespace FormBuilder\Rule;
  13. use FormBuilder\Contract\ValidateInterface;
  14. trait ValidateRule
  15. {
  16. /**
  17. * 组件验证规则
  18. *
  19. * @var array
  20. */
  21. protected $validate = [];
  22. public function validate(array $validates)
  23. {
  24. $this->validate = $validates;
  25. return $this;
  26. }
  27. /**
  28. * @param array|ValidateInterface $validate
  29. * @return $this
  30. */
  31. public function appendValidate($validate)
  32. {
  33. $this->validate[] = $validate;
  34. return $this;
  35. }
  36. public function appendValidates(array $validates)
  37. {
  38. $this->validate = array_merge($this->validate, $validates);
  39. return $this;
  40. }
  41. protected function getValidate()
  42. {
  43. return $this->validate;
  44. }
  45. protected function parseValidate()
  46. {
  47. $validate = [];
  48. foreach ($this->validate as $value) {
  49. $validate[] = $value instanceof ValidateInterface ? $value->getValidate() : $value;
  50. }
  51. return $validate;
  52. }
  53. public function parseValidateRule()
  54. {
  55. if (!count($this->validate))
  56. return [];
  57. return ['validate' => $this->parseValidate()];
  58. }
  59. }