<?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,
        ];
    }
}