ControlRule.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Util;
  14. trait ControlRule
  15. {
  16. /**
  17. * 根据组件的值显示不同的组件
  18. *
  19. * @var
  20. */
  21. protected $control = [];
  22. /**
  23. * @param array $control
  24. * @return $this
  25. */
  26. public function control(array $control)
  27. {
  28. $this->control = $control;
  29. return $this;
  30. }
  31. /**
  32. * @param string|int|float $value
  33. * @param array $rule
  34. * @return $this
  35. */
  36. public function appendControl($value, array $rule)
  37. {
  38. $this->control[] = compact('value', 'rule');
  39. return $this;
  40. }
  41. /**
  42. * @param array $controls
  43. * @return $this
  44. */
  45. public function appendControls(array $controls)
  46. {
  47. $this->control = array_merge($this->control, $controls);
  48. return $this;
  49. }
  50. public function getControl()
  51. {
  52. return $this->control;
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function parseControlRule()
  58. {
  59. if (!count($this->control)) return [];
  60. $control = [];
  61. foreach ($this->control as $child) {
  62. foreach ($child['rule'] as $k => $rule) {
  63. $child['rule'][$k] = Util::isComponent($rule) ? $rule->build() : $rule;
  64. }
  65. $control[] = $child;
  66. }
  67. return compact('control');
  68. }
  69. }