Tree.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace common\util;
  3. use JsonSerializable;
  4. class Tree implements JsonSerializable
  5. {
  6. /**
  7. * 节点ID
  8. */
  9. private $id;
  10. /**
  11. * 显示节点文本
  12. */
  13. private $text;
  14. /**
  15. * 节点状态,open closed
  16. */
  17. private $state = ["opened" => false];
  18. /**
  19. * 节点是否被选中 true false
  20. */
  21. private $checked = false;
  22. /**
  23. * 节点属性
  24. */
  25. private $attributes;
  26. private $url;
  27. public function getUrl(): string
  28. {
  29. return $this->url;
  30. }
  31. /**
  32. * @param mixed $url
  33. */
  34. public function setUrl($url)
  35. {
  36. $this->url = $url;
  37. }
  38. /**
  39. * 节点的子节点
  40. */
  41. private $children = [];
  42. /**
  43. * 父ID
  44. */
  45. private $parentId = "";
  46. /**
  47. * 是否有父节点
  48. */
  49. private $hasParent = false;
  50. /**
  51. * 是否有子节点
  52. */
  53. private $hasChildren = false;
  54. /**
  55. * @return string
  56. */
  57. public function getId(): string
  58. {
  59. return $this->id;
  60. }
  61. /**
  62. * @param string $id
  63. */
  64. public function setId(string $id)
  65. {
  66. $this->id = $id;
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function getText(): string
  72. {
  73. return $this->text;
  74. }
  75. /**
  76. * @param string $text
  77. */
  78. public function setText(string $text)
  79. {
  80. $this->text = $text;
  81. }
  82. /**
  83. * @return false[]
  84. */
  85. public function getState(): array
  86. {
  87. return $this->state;
  88. }
  89. /**
  90. * @param false[] $state
  91. */
  92. public function setState(array $state)
  93. {
  94. $this->state = $state;
  95. }
  96. /**
  97. * @return bool
  98. */
  99. public function isChecked(): bool
  100. {
  101. return $this->checked;
  102. }
  103. /**
  104. * @param bool $checked
  105. */
  106. public function setChecked(bool $checked)
  107. {
  108. $this->checked = $checked;
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function getAttributes(): array
  114. {
  115. return $this->attributes;
  116. }
  117. /**
  118. * @param array $attributes
  119. */
  120. public function setAttributes(array $attributes)
  121. {
  122. $this->attributes = $attributes;
  123. }
  124. /**
  125. * @return array
  126. */
  127. public function getChildren(): array
  128. {
  129. return $this->children;
  130. }
  131. /**
  132. * @param array $children
  133. */
  134. public function setChildren(array $children)
  135. {
  136. $this->children = $children;
  137. }
  138. /**
  139. * @param Tree $children
  140. */
  141. public function addChildren(Tree $children)
  142. {
  143. $this->children[] = $children;
  144. }
  145. /**
  146. * @return string
  147. */
  148. public function getParentId(): string
  149. {
  150. return $this->parentId;
  151. }
  152. /**
  153. * @param string $parentId
  154. */
  155. public function setParentId(string $parentId)
  156. {
  157. $this->parentId = $parentId;
  158. }
  159. /**
  160. * @return bool
  161. */
  162. public function isHasParent(): bool
  163. {
  164. return $this->hasParent;
  165. }
  166. /**
  167. * @param bool $hasParent
  168. */
  169. public function setHasParent(bool $hasParent)
  170. {
  171. $this->hasParent = $hasParent;
  172. }
  173. /**
  174. * @return bool
  175. */
  176. public function isHasChildren(): bool
  177. {
  178. return $this->hasChildren;
  179. }
  180. /**
  181. * @param bool $hasChildren
  182. */
  183. public function setHasChildren(bool $hasChildren)
  184. {
  185. $this->hasChildren = $hasChildren;
  186. }
  187. public function Tree($id, $text, $state, $checked, $attributes, $children, $isParent, $isChildren, $parentID)
  188. {
  189. $this->id = $id;
  190. $this->text = $text;
  191. $this->state = $state;
  192. $this->checked = $checked;
  193. $this->attributes = $attributes;
  194. $this->children = $children;
  195. $this->hasParent = $isParent;
  196. $this->hasChildren = $isChildren;
  197. $this->parentId = $parentID;
  198. }
  199. /**
  200. * json_encode无法转化私有属性,需要使用jsonSerialize自定义转换私有的属性
  201. * @return array
  202. */
  203. public function jsonSerialize()
  204. {
  205. return [
  206. "id" => $this->id,
  207. "text" => $this->text,
  208. "state" => $this->state,
  209. "checked" => $this->checked,
  210. "attributes" => $this->attributes,
  211. "children" => $this->children,
  212. "hasParent" => $this->hasParent,
  213. "hasChildren" => $this->hasChildren,
  214. "parentId" => $this->parentId,
  215. ];
  216. }
  217. }