5
5
class Collection extends BaseCollection {
6
6
7
7
/**
8
- * Convert list of nodes to dictionary with specified key .
8
+ * Build tree from node list. Each item will have set children relation .
9
9
*
10
- * If no key is specified then "parent_id" is used.
10
+ * To succesfully build tree "id", "_lft" and "parent_id" keys must present.
11
+ *
12
+ * If {@link rootNodeId} is provided, the tree will contain only descendants
13
+ * of the node with such primary key value.
11
14
*
12
- * @param string $key
15
+ * @param integer $rootNodeId
13
16
*
14
- * @return array
17
+ * @return Collection
15
18
*/
16
- public function toDictionary ( $ key = null )
19
+ public function toTree ( $ rootNodeId = null )
17
20
{
18
- if (empty ($ this ->items )) {
19
- return array ();
20
- }
21
+ $ result = new static ();
22
+
23
+ if (empty ($ this ->items )) return $ result ;
24
+
25
+ $ key = $ this ->first ()->getParentIdName ();
26
+ $ dictionary = $ this ->groupBy ($ key );
21
27
22
- if ($ key === null ) {
23
- $ key = $ this ->first ()->getParentIdName ();
28
+ $ rootNodeId = $ this ->getRootNodeId ($ rootNodeId );
29
+
30
+ if (!isset ($ dictionary [$ rootNodeId ]))
31
+ {
32
+ return $ result ;
24
33
}
25
34
26
- $ result = array ();
35
+ $ result ->items = $ dictionary [$ rootNodeId ];
36
+
37
+ foreach ($ this ->items as $ item )
38
+ {
39
+ $ key = $ item ->getKey ();
40
+
41
+ $ children = new BaseCollection (isset ($ dictionary [$ key ]) ? $ dictionary [$ key ] : array ());
27
42
28
- foreach ($ this ->items as $ item ) {
29
- $ result [$ item ->$ key ][] = $ item ;
43
+ $ item ->setRelation ('children ' , $ children );
30
44
}
31
45
32
46
return $ result ;
33
47
}
34
48
35
49
/**
36
- * Build tree from node list.
37
- *
38
- * To succesfully build tree "id", "_lft" and "parent_id" keys must present.
39
- *
40
- * If {@link rootNodeId} is provided, the tree will contain only descendants
41
- * of the node with such primary key value.
42
- *
43
- * @param integer $rootNodeId
50
+ * @param null|int $rootNodeId
44
51
*
45
- * @return Collection
52
+ * @return int
46
53
*/
47
- public function toTree ($ rootNodeId = null )
54
+ public function getRootNodeId ($ rootNodeId = null )
48
55
{
49
- $ dictionary = $ this ->toDictionary ();
50
- $ result = new static ();
51
-
52
56
// If root node is not specified we take parent id of node with
53
57
// least lft value as root node id.
54
- if ($ rootNodeId === null )
58
+ if ($ rootNodeId === null )
55
59
{
56
60
$ leastValue = null ;
57
61
58
- foreach ($ this ->items as $ item ) {
62
+ foreach ($ this ->items as $ item )
63
+ {
59
64
if ($ leastValue === null || $ item ->getLft () < $ leastValue )
60
65
{
61
66
$ leastValue = $ item ->getLft ();
@@ -64,21 +69,6 @@ public function toTree($rootNodeId = null)
64
69
}
65
70
}
66
71
67
- $ result ->items = isset ($ dictionary [$ rootNodeId ]) ? $ dictionary [$ rootNodeId ] : array ();
68
-
69
- if (empty ($ result ->items ))
70
- {
71
- return $ result ;
72
- }
73
-
74
- foreach ($ this ->items as $ item )
75
- {
76
- $ key = $ item ->getKey ();
77
-
78
- $ children = new BaseCollection (isset ($ dictionary [$ key ]) ? $ dictionary [$ key ] : array ());
79
- $ item ->setRelation ('children ' , $ children );
80
- }
81
-
82
- return $ result ;
72
+ return $ rootNodeId ;
83
73
}
84
74
}
0 commit comments