55class Collection extends BaseCollection {
66
77 /**
8- * Convert list of nodes to dictionary with specified key .
8+ * Build tree from node list. Each item will have set children relation .
99 *
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.
1114 *
12- * @param string $key
15+ * @param integer $rootNodeId
1316 *
14- * @return array
17+ * @return Collection
1518 */
16- public function toDictionary ( $ key = null )
19+ public function toTree ( $ rootNodeId = null )
1720 {
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 );
2127
22- if ($ key === null ) {
23- $ key = $ this ->first ()->getParentIdName ();
28+ $ rootNodeId = $ this ->getRootNodeId ($ rootNodeId );
29+
30+ if (!isset ($ dictionary [$ rootNodeId ]))
31+ {
32+ return $ result ;
2433 }
2534
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 ());
2742
28- foreach ($ this ->items as $ item ) {
29- $ result [$ item ->$ key ][] = $ item ;
43+ $ item ->setRelation ('children ' , $ children );
3044 }
3145
3246 return $ result ;
3347 }
3448
3549 /**
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
4451 *
45- * @return Collection
52+ * @return int
4653 */
47- public function toTree ($ rootNodeId = null )
54+ public function getRootNodeId ($ rootNodeId = null )
4855 {
49- $ dictionary = $ this ->toDictionary ();
50- $ result = new static ();
51-
5256 // If root node is not specified we take parent id of node with
5357 // least lft value as root node id.
54- if ($ rootNodeId === null )
58+ if ($ rootNodeId === null )
5559 {
5660 $ leastValue = null ;
5761
58- foreach ($ this ->items as $ item ) {
62+ foreach ($ this ->items as $ item )
63+ {
5964 if ($ leastValue === null || $ item ->getLft () < $ leastValue )
6065 {
6166 $ leastValue = $ item ->getLft ();
@@ -64,21 +69,6 @@ public function toTree($rootNodeId = null)
6469 }
6570 }
6671
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 ;
8373 }
8474}
0 commit comments