|
15 | 15 | * 树结点的结构 array('结点编号', '父结点编号', ... )
|
16 | 16 | *
|
17 | 17 | * 术语:
|
18 |
| - * tree 树 |
19 |
| - * subTree 子数 |
20 |
| - * node 结点 |
21 |
| - * degree 结点的度 |
22 |
| - * leaf 叶子,度为0的结点 |
23 |
| - * child 孩子,结点子树的根 |
24 |
| - * parent 结点的上层结点 |
25 |
| - * level 结点的层次 |
26 |
| - * depth 数的深度,树中结点最大层次数 |
27 |
| - * ancestor 祖先,树叶的顶层父结点 |
| 18 | + * tree 树 |
| 19 | + * subTree 子树 |
| 20 | + * node 结点 |
| 21 | + * degree 结点的度 |
| 22 | + * leaf 叶子,度为0的结点 |
| 23 | + * child 孩子,结点子树的根 |
| 24 | + * parent 结点的上层结点 |
| 25 | + * level 结点的层次 |
| 26 | + * depth 数的深度,树中结点最大层次数 |
| 27 | + * ancestor 祖先,树叶的顶层父结点 |
28 | 28 | * descendant 树的子孙,结点的祖先和子孙不包含结点 本身
|
29 |
| - * path 路径,一个结点到达另一个结点经过的结点 |
| 29 | + * path 路径,一个结点到达另一个结点经过的结点 |
30 | 30 | *
|
31 |
| - * 这里用不上sibling、forest |
| 31 | + * 我们这个类没用上sibling、forest |
32 | 32 | *
|
33 | 33 | * @package wf.util
|
34 | 34 | * @author cm <cmpan@qq.com>
|
35 | 35 | * @since 0.1.0
|
36 | 36 | */
|
37 |
| -class Tree { |
| 37 | +class Tree |
| 38 | +{ |
38 | 39 | /**
|
39 | 40 | * 要处理的结点的数组
|
40 | 41 | *
|
41 | 42 | * @var array
|
42 | 43 | */
|
43 |
| - protected $inNodes = array(); |
| 44 | + protected $inNodes = []; |
44 | 45 |
|
45 | 46 | /**
|
46 | 47 | * 处理后的结点的数组
|
47 | 48 | *
|
48 | 49 | * @var array
|
49 | 50 | */
|
50 |
| - protected $outNodes = array(); |
| 51 | + protected $outNodes = []; |
51 | 52 |
|
52 | 53 | /**
|
53 | 54 | * 结点中表示该结点的父结点id的属性名(下标)
|
@@ -76,59 +77,41 @@ class Tree {
|
76 | 77 | * @var int
|
77 | 78 | */
|
78 | 79 | public $depth = 0;
|
79 |
| - |
80 |
| - //public function __construct() {} |
81 |
| - |
82 |
| - public function setParentNodeIdKey($key) { |
83 |
| - $this->nodeParentIdKey = $key; |
84 |
| - } |
85 |
| - |
86 |
| - public function setNodeIdKey($key) { |
87 |
| - $this->nodeIdKey = $key; |
88 |
| - } |
89 | 80 |
|
90 | 81 | /**
|
91 |
| - * 填充要计算的数组 |
| 82 | + * 在构造设置要计算的数组 |
92 | 83 | *
|
93 | 84 | * @param array $var
|
94 | 85 | * @param string $id
|
95 | 86 | * @param string $parentId
|
96 | 87 | */
|
97 |
| - public function set($vars, $id = 'id', $parentId = 'parent_id') { |
98 |
| - if(!is_array($vars)) throw new \Exception("错误的数据类型"); |
| 88 | + public function __construct(array $vars, $id = 'id', $parentId = 'parent_id') |
| 89 | + { |
99 | 90 | foreach ($vars as $var) {
|
100 | 91 | $this->inNodes[$var[$id]] = $var;
|
101 | 92 | }
|
102 | 93 |
|
103 | 94 | $this->nodeIdKey = $id;
|
104 | 95 | $this->nodeParentIdKey = $parentId;
|
105 | 96 | }
|
106 |
| - |
107 |
| - /** |
108 |
| - * 添加结点 |
109 |
| - * |
110 |
| - * @param array $node 结点 |
111 |
| - * @param string $key = null 结点的下标 |
112 |
| - */ |
113 |
| - public function add($node, $key = null) { |
114 |
| - $node = $key ? array($key => $node) : array($node); |
115 |
| - $this->inNodes = array_merge($this->inNodes, $node); |
116 |
| - } |
117 | 97 |
|
118 | 98 | /**
|
119 | 99 | * 获取子结点,子结点将按父结点id和结点id来排序
|
120 | 100 | *
|
121 | 101 | * @param int $id 结点的id
|
122 | 102 | * @param bool $isReturnSelf 是否返回自己
|
123 |
| - * @param array $unReturnFields 不返回的属性,可设置:[isLeaf, ancestorIdArr, descendantIdArr, descendantId, childArr, isLastNode, icon] |
| 103 | + * @param array $unReturnFields 设置不返回的属性,可设置:[isLeaf, ancestorIdArr, descendantIdArr, descendantId, childArr, isLastNode, icon] |
124 | 104 | * @return array
|
125 | 105 | */
|
126 |
| - public function get($id = 0, $isReturnSelf = true, array $unReturnFields = []) { |
| 106 | + public function get($id = 0, $isReturnSelf = true, array $unReturnFields = []) |
| 107 | + { |
127 | 108 | // 层次排序
|
128 |
| - $this->levelOrder(0); |
| 109 | + $this->parse(0); |
129 | 110 |
|
130 | 111 | // 设置缩进图标/形状
|
131 |
| - empty($unReturnFields['icon']) && $this->setLevelIcon(); |
| 112 | + if(empty($unReturnFields['icon'])) { |
| 113 | + $this->parseLevelIcon(); |
| 114 | + } |
132 | 115 |
|
133 | 116 | $cats = $this->outNodes;
|
134 | 117 | if($id && isset($cats[$id])) {
|
@@ -157,10 +140,11 @@ public function get($id = 0, $isReturnSelf = true, array $unReturnFields = []) {
|
157 | 140 | }
|
158 | 141 |
|
159 | 142 | /**
|
160 |
| - * 设置树的层次缩进图标/形状 |
| 143 | + * 提取树的层次缩进图标/形状 |
161 | 144 | *
|
162 | 145 | */
|
163 |
| - private function setLevelIcon() { |
| 146 | + private function parseLevelIcon() |
| 147 | + { |
164 | 148 | $nodes = $this->outNodes;
|
165 | 149 | $fidName = $this->nodeParentIdKey;
|
166 | 150 |
|
@@ -208,7 +192,8 @@ private function setLevelIcon() {
|
208 | 192 | *
|
209 | 193 | * @param int|string $id 结点id
|
210 | 194 | */
|
211 |
| - protected function levelOrder($id) { |
| 195 | + protected function parse($id) |
| 196 | + { |
212 | 197 | foreach($this->inNodes as $node) {
|
213 | 198 | $nodeId = $node[$this->nodeIdKey]; // 结点ID
|
214 | 199 | $nodeFid = $node[$this->nodeParentIdKey]; // 结点的父结点的id
|
@@ -253,16 +238,11 @@ protected function levelOrder($id) {
|
253 | 238 | //$this->addChildMark($nodeId, $nodeFid);
|
254 | 239 | $this->depth = ($this->depth > $this->outNodes[$nodeId]['level']) ? $this->depth : $this->outNodes[$nodeId]['level'];
|
255 | 240 | if($nodeId) {
|
256 |
| - $this->levelOrder($nodeId); |
| 241 | + $this->parse($nodeId); |
257 | 242 | }
|
258 | 243 | //unset($this->inNodes[$nodeId]);
|
259 | 244 | }
|
260 | 245 | }
|
261 | 246 | }
|
262 |
| - |
263 |
| - public function __destruct() { |
264 |
| - unset($this->inNodes); |
265 |
| - unset($this->outNodes); |
266 |
| - } |
267 | 247 | }
|
268 | 248 |
|
0 commit comments