TreeData.php 2.3 KB

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