ChildrenRule.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 ChildrenRule
  15. {
  16. /**
  17. * 组件的插槽名称,如果组件是其它组件的子组件,需为插槽指定名称
  18. *
  19. * @var
  20. */
  21. protected $slot;
  22. /**
  23. * 设置父级组件的插槽,默认为default.可配合 slot 配置项使用
  24. *
  25. * @var
  26. */
  27. protected $children = [];
  28. /**
  29. * @param $slot
  30. * @return $this
  31. */
  32. public function slot($slot)
  33. {
  34. $this->slot = (string)$slot;
  35. return $this;
  36. }
  37. /**
  38. * @param array $children
  39. * @return $this
  40. */
  41. public function children(array $children)
  42. {
  43. $this->children = $children;
  44. return $this;
  45. }
  46. /**
  47. * @param string|array|self $child
  48. * @return $this
  49. */
  50. public function appendChild($child)
  51. {
  52. $this->children[] = $child;
  53. return $this;
  54. }
  55. /**
  56. * @param array $children
  57. * @return $this
  58. */
  59. public function appendChildren($children)
  60. {
  61. $this->children = array_merge($this->children, $children);
  62. return $this;
  63. }
  64. public function getSlot()
  65. {
  66. return $this->slot;
  67. }
  68. public function getChildren()
  69. {
  70. return $this->children;
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function parseChildrenRule()
  76. {
  77. if (!count($this->children)) return [];
  78. $children = [];
  79. foreach ($this->children as $child) {
  80. $children[] = Util::isComponent($child) ? $child->build() : $child;
  81. }
  82. return compact('children');
  83. }
  84. }