ChildrenRule.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. protected function parseChildren($child)
  73. {
  74. return Util::isComponent($child) ? $child->build() : $child;
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function parseChildrenRule()
  80. {
  81. if (!count($this->children)) return [];
  82. $children = [];
  83. foreach ($this->children as $child) {
  84. $children[] = $this->parseChildren($child);
  85. }
  86. return compact('children');
  87. }
  88. }