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