@@ -9,76 +9,85 @@ class Collection extends BaseCollection {
99 *
1010 * If no key is specified then "parent_id" is used.
1111 *
12- * @param string $key
12+ * @param string $key
1313 *
1414 * @return array
15+ * @deprecated since 1.1
1516 */
1617 public function toDictionary ($ key = null )
1718 {
18- if (empty ($ this ->items )) {
19- return array ();
20- }
21-
22- if ($ key === null ) {
23- $ key = $ this ->first ()->getParentIdName ();
24- }
25-
26- $ result = array ();
27-
28- foreach ($ this ->items as $ item ) {
29- $ result [$ item ->$ key ][] = $ item ;
30- }
19+ if ($ key === null ) $ key = $ this ->first ()->getParentIdName ();
3120
32- return $ result ;
21+ return $ this -> groupBy ( $ key )-> all () ;
3322 }
3423
3524 /**
36- * Build tree from node list.
25+ * Build tree from node list. Each item will have set children relation.
3726 *
3827 * To succesfully build tree "id", "_lft" and "parent_id" keys must present.
39- *
28+ *
4029 * If {@link rootNodeId} is provided, the tree will contain only descendants
4130 * of the node with such primary key value.
42- *
31+ *
4332 * @param integer $rootNodeId
4433 *
4534 * @return Collection
4635 */
4736 public function toTree ($ rootNodeId = null )
4837 {
49- $ dictionary = $ this ->toDictionary ();
5038 $ result = new static ();
5139
52- // If root node is not specified we take parent id of node with
53- // least lft value as root node id.
54- if ($ rootNodeId === null )
55- {
56- $ leastValue = null ;
40+ if (empty ($ this ->items )) return $ result ;
5741
58- foreach ($ this ->items as $ item ) {
59- if ($ leastValue === null || $ item ->getLft () < $ leastValue )
60- {
61- $ leastValue = $ item ->getLft ();
62- $ rootNodeId = $ item ->getParentId ();
63- }
64- }
65- }
42+ $ key = $ this ->first ()->getParentIdName ();
43+ $ dictionary = $ this ->groupBy ($ key );
6644
67- $ result -> items = isset ( $ dictionary [ $ rootNodeId]) ? $ dictionary [ $ rootNodeId ] : array ( );
45+ $ rootNodeId = $ this -> getRootNodeId ( $ rootNodeId );
6846
69- if (empty ( $ result -> items ))
47+ if (! $ dictionary -> has ( $ rootNodeId ))
7048 {
7149 return $ result ;
7250 }
7351
74- foreach ($ this ->items as $ item )
52+ $ result ->items = $ dictionary ->get ($ rootNodeId );
53+
54+ foreach ($ this ->items as $ item )
7555 {
7656 $ key = $ item ->getKey ();
7757
78- $ children = new BaseCollection (isset ($ dictionary [$ key ]) ? $ dictionary [$ key ] : array ());
79- $ item ->setRelation ('children ' , $ children );
58+ $ children = $ dictionary ->has ($ key )
59+ ? $ dictionary ->get ($ key )
60+ : array ();
61+
62+ $ item ->setRelation ('children ' , new BaseCollection ($ children ));
8063 }
8164
8265 return $ result ;
8366 }
67+
68+ /**
69+ * @param null|int $rootNodeId
70+ *
71+ * @return int
72+ */
73+ public function getRootNodeId ($ rootNodeId = null )
74+ {
75+ // If root node is not specified we take parent id of node with
76+ // least lft value as root node id.
77+ if ($ rootNodeId === null )
78+ {
79+ $ leastValue = null ;
80+
81+ foreach ($ this ->items as $ item )
82+ {
83+ if ($ leastValue === null || $ item ->getLft () < $ leastValue )
84+ {
85+ $ leastValue = $ item ->getLft ();
86+ $ rootNodeId = $ item ->getParentId ();
87+ }
88+ }
89+ }
90+
91+ return $ rootNodeId ;
92+ }
8493}
0 commit comments