ValidateRule.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. */
  11. namespace FormBuilder\Rule;
  12. use FormBuilder\Contract\ValidateInterface;
  13. trait ValidateRule
  14. {
  15. /**
  16. * 组件验证规则
  17. *
  18. * @var array
  19. */
  20. protected $validate = [];
  21. public function validate(array $validates)
  22. {
  23. $this->validate = $validates;
  24. return $this;
  25. }
  26. /**
  27. * @param array|ValidateInterface $validate
  28. * @return $this
  29. */
  30. public function appendValidate($validate)
  31. {
  32. $this->validate[] = $validate;
  33. return $this;
  34. }
  35. public function appendValidates(array $validates)
  36. {
  37. $this->validate = array_merge($this->validate, $validates);
  38. return $this;
  39. }
  40. protected function getValidate()
  41. {
  42. return $this->validate;
  43. }
  44. protected function parseValidate()
  45. {
  46. $validate = [];
  47. foreach ($this->validate as $value) {
  48. $validate[] = $value instanceof ValidateInterface ? $value->getValidate() : $value;
  49. }
  50. return $validate;
  51. }
  52. public function parseValidateRule()
  53. {
  54. if (!count($this->validate))
  55. return [];
  56. return ['validate' => $this->parseValidate()];
  57. }
  58. }