TreeData.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder\components;
  8. use FormBuilder\interfaces\FormComponentInterFace;
  9. use FormBuilder\traits\component\CallPropsTrait;
  10. /**
  11. * Class TreeData
  12. *
  13. * @package FormBuilder\components
  14. * @method $this id(String $id) Id, 必须唯一
  15. * @method $this title(String $title) 标题
  16. * @method $this expand(Boolean $bool) 是否展开直子节点, 默认为false
  17. * @method $this disabled(Boolean $bool) 禁掉响应, 默认为false
  18. * @method $this disableCheckbox(Boolean $bool) 禁掉 checkbox
  19. * @method $this selected(Boolean $bool) 是否选中子节点
  20. * @method $this checked(Boolean $bool) 是否勾选(如果勾选,子节点也会全部勾选)
  21. */
  22. class TreeData implements FormComponentInterFace
  23. {
  24. use CallPropsTrait;
  25. /**
  26. * @var array
  27. */
  28. protected $children = [];
  29. /**
  30. * @var array
  31. */
  32. protected $props = [];
  33. /**
  34. * @var array
  35. */
  36. protected static $propsRule = [
  37. 'id' => 'string',
  38. 'title' => 'string',
  39. 'expand' => 'boolean',
  40. 'disabled' => 'boolean',
  41. 'disableCheckbox' => 'boolean',
  42. 'selected' => 'boolean',
  43. 'checked' => 'boolean',
  44. ];
  45. /**
  46. * TreeData constructor.
  47. *
  48. * @param $id
  49. * @param $title
  50. * @param array $children
  51. */
  52. public function __construct($id, $title, array $children = [])
  53. {
  54. $this->props['id'] = $id;
  55. $this->props['title'] = $title;
  56. $this->children = $children;
  57. }
  58. /**
  59. * @param array $children
  60. * @return $this
  61. */
  62. public function children(array $children)
  63. {
  64. $this->children = array_merge($this->children, $children);
  65. return $this;
  66. }
  67. /**
  68. * @param TreeData $child
  69. * @return $this
  70. */
  71. public function child(TreeData $child)
  72. {
  73. $this->children[] = $child;
  74. return $this;
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function build()
  80. {
  81. $children = [];
  82. foreach ($this->children as $child) {
  83. $children[] = $child instanceof TreeData
  84. ? $child->build()
  85. : $child;
  86. }
  87. $this->props['children'] = $children;
  88. return $this->props;
  89. }
  90. }