|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BaoPham\TreeParser; |
|
4
|
|
|
|
|
5
|
|
|
class TreeNode implements \JsonSerializable, \ArrayAccess |
|
6
|
|
|
{ |
|
7
|
|
|
const ROOT_LEVEL = 0; |
|
8
|
|
|
|
|
9
|
|
|
const ROOT_ORDER = 0; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
public $name; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
public $isRoot = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
public $order; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
public $level; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var TreeNode |
|
33
|
|
|
*/ |
|
34
|
|
|
public $parent; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var TreeNode[] |
|
38
|
|
|
*/ |
|
39
|
|
|
public $children = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public function hasChildren() |
|
45
|
|
|
{ |
|
46
|
|
|
return !empty($this->children); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Specify data which should be serialized to JSON |
|
51
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
52
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
53
|
|
|
* which is a value of any type other than a resource. |
|
54
|
|
|
* @since 5.4.0 |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function jsonSerialize() |
|
57
|
|
|
{ |
|
58
|
3 |
|
$properties = get_object_vars($this); |
|
59
|
|
|
|
|
60
|
3 |
|
unset($properties['parent']); |
|
61
|
|
|
|
|
62
|
3 |
|
return $properties; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Whether a offset exists |
|
67
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
68
|
|
|
* @param mixed $offset <p> |
|
69
|
|
|
* An offset to check for. |
|
70
|
|
|
* </p> |
|
71
|
|
|
* @return boolean true on success or false on failure. |
|
72
|
|
|
* </p> |
|
73
|
|
|
* <p> |
|
74
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
75
|
|
|
* @since 5.0.0 |
|
76
|
|
|
*/ |
|
77
|
|
|
public function offsetExists($offset) |
|
78
|
|
|
{ |
|
79
|
|
|
return isset($this->$offset); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Offset to retrieve |
|
84
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
85
|
|
|
* @param mixed $offset <p> |
|
86
|
|
|
* The offset to retrieve. |
|
87
|
|
|
* </p> |
|
88
|
|
|
* @return mixed Can return all value types. |
|
89
|
|
|
* @since 5.0.0 |
|
90
|
|
|
*/ |
|
91
|
|
|
public function offsetGet($offset) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->$offset; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Offset to set |
|
98
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
99
|
|
|
* @param mixed $offset <p> |
|
100
|
|
|
* The offset to assign the value to. |
|
101
|
|
|
* </p> |
|
102
|
|
|
* @param mixed $value <p> |
|
103
|
|
|
* The value to set. |
|
104
|
|
|
* </p> |
|
105
|
|
|
* @return void |
|
106
|
|
|
* @since 5.0.0 |
|
107
|
|
|
*/ |
|
108
|
|
|
public function offsetSet($offset, $value) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->$offset = $value; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Offset to unset |
|
115
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
116
|
|
|
* @param mixed $offset <p> |
|
117
|
|
|
* The offset to unset. |
|
118
|
|
|
* </p> |
|
119
|
|
|
* @return void |
|
120
|
|
|
* @since 5.0.0 |
|
121
|
|
|
*/ |
|
122
|
|
|
public function offsetUnset($offset) |
|
123
|
|
|
{ |
|
124
|
|
|
unset($this->$offset); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|