TreeData.php 2.3 KB

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