123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace common\util;
- use JsonSerializable;
- class Tree implements JsonSerializable
- {
- /**
- * 节点ID
- */
- private $id;
- /**
- * 显示节点文本
- */
- private $text;
- /**
- * 节点状态,open closed
- */
- private $state = ["opened" => false];
- /**
- * 节点是否被选中 true false
- */
- private $checked = false;
- /**
- * 节点属性
- */
- private $attributes;
- private $url;
- public function getUrl(): string
- {
- return $this->url;
- }
- /**
- * @param mixed $url
- */
- public function setUrl($url)
- {
- $this->url = $url;
- }
- /**
- * 节点的子节点
- */
- private $children = [];
- /**
- * 父ID
- */
- private $parentId = "";
- /**
- * 是否有父节点
- */
- private $hasParent = false;
- /**
- * 是否有子节点
- */
- private $hasChildren = false;
- /**
- * @return string
- */
- public function getId(): string
- {
- return $this->id;
- }
- /**
- * @param string $id
- */
- public function setId(string $id)
- {
- $this->id = $id;
- }
- /**
- * @return string
- */
- public function getText(): string
- {
- return $this->text;
- }
- /**
- * @param string $text
- */
- public function setText(string $text)
- {
- $this->text = $text;
- }
- /**
- * @return false[]
- */
- public function getState(): array
- {
- return $this->state;
- }
- /**
- * @param false[] $state
- */
- public function setState(array $state)
- {
- $this->state = $state;
- }
- /**
- * @return bool
- */
- public function isChecked(): bool
- {
- return $this->checked;
- }
- /**
- * @param bool $checked
- */
- public function setChecked(bool $checked)
- {
- $this->checked = $checked;
- }
- /**
- * @return array
- */
- public function getAttributes(): array
- {
- return $this->attributes;
- }
- /**
- * @param array $attributes
- */
- public function setAttributes(array $attributes)
- {
- $this->attributes = $attributes;
- }
- /**
- * @return array
- */
- public function getChildren(): array
- {
- return $this->children;
- }
- /**
- * @param array $children
- */
- public function setChildren(array $children)
- {
- $this->children = $children;
- }
- /**
- * @param Tree $children
- */
- public function addChildren(Tree $children)
- {
- $this->children[] = $children;
- }
- /**
- * @return string
- */
- public function getParentId(): string
- {
- return $this->parentId;
- }
- /**
- * @param string $parentId
- */
- public function setParentId(string $parentId)
- {
- $this->parentId = $parentId;
- }
- /**
- * @return bool
- */
- public function isHasParent(): bool
- {
- return $this->hasParent;
- }
- /**
- * @param bool $hasParent
- */
- public function setHasParent(bool $hasParent)
- {
- $this->hasParent = $hasParent;
- }
- /**
- * @return bool
- */
- public function isHasChildren(): bool
- {
- return $this->hasChildren;
- }
- /**
- * @param bool $hasChildren
- */
- public function setHasChildren(bool $hasChildren)
- {
- $this->hasChildren = $hasChildren;
- }
- public function Tree($id, $text, $state, $checked, $attributes, $children, $isParent, $isChildren, $parentID)
- {
- $this->id = $id;
- $this->text = $text;
- $this->state = $state;
- $this->checked = $checked;
- $this->attributes = $attributes;
- $this->children = $children;
- $this->hasParent = $isParent;
- $this->hasChildren = $isChildren;
- $this->parentId = $parentID;
- }
- /**
- * json_encode无法转化私有属性,需要使用jsonSerialize自定义转换私有的属性
- * @return array
- */
- public function jsonSerialize()
- {
- return [
- "id" => $this->id,
- "text" => $this->text,
- "state" => $this->state,
- "checked" => $this->checked,
- "attributes" => $this->attributes,
- "children" => $this->children,
- "hasParent" => $this->hasParent,
- "hasChildren" => $this->hasChildren,
- "parentId" => $this->parentId,
- ];
- }
- }
|