-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChildren.php
91 lines (74 loc) · 2.13 KB
/
Children.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* 孩子列表类
*/
class Children {
private $list = [];
public function getSize() {
return count($this->list);
}
public function addChild(Node $node) {
$this->list[] = $node;
}
// 拼接孩子节点的JSON字符串
public function toString() {
$result = "[";
foreach ($this->list as $value) {
if ($ret = $value->toString()) {
$result .= $ret . ',';
}
}
$result = trim($result, ',') . ']';
return $result;
}
// 孩子节点排序
public function sortChildren() {
// 对本层节点进行排序
// 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
usort($this->list, array("NodeIDComparator", "compare"));
// 对每个节点的下一层节点进行排序
foreach ($this->list as $value) {
$value->sortChildren();
}
}
// 搜索菜单节点,同时进行功能路径过滤
public function searchTreeNode($keyWord) {
foreach ($this->list as $node) {
$node->searchTreeNode($keyWord);
}
}
// 设置孩子节点为不可见
public function setTreeNotVisible() {
foreach ($this->list as $node) {
$node->setTreeNotVisible();
}
}
// 设置孩子节点为可见
public function setTreeVisible() {
foreach ($this->list as $node) {
$node->setTreeVisible();
}
}
// 在孩子节点中寻找功能叶子节点
public function initializeLeafList(&$leafList) {
foreach ($this->list as $node) {
$node->initializeLeafList($leafList);
}
}
}
/**
* 节点比较器
*/
class NodeIDComparator {
// 按照节点编号比较
static public function compare(Node $a, Node $b) {
$a_w = strtolower($a->weight);
$b_w = strtolower($b->weight);
if ($a_w == $b_w) {
$a_id = strtolower($a->id);
$b_id = strtolower($b->id);
return $a_id < $b_id ? -1 : ($a_id == $b_id ? 0 : 1);
}
return ($a_w > $b_w) ? -1 : +1;
}
}