8
8
class Collection extends BaseCollection
9
9
{
10
10
/**
11
- * Fill `parent` and `children` relationships for every node in collection.
11
+ * Fill `parent` and `children` relationships for every node in the collection.
12
12
*
13
13
* This will overwrite any previously set relations.
14
14
*
@@ -18,15 +18,15 @@ public function linkNodes()
18
18
{
19
19
if ($ this ->isEmpty ()) return $ this ;
20
20
21
- $ groupedChildren = $ this ->groupBy ($ this ->first ()->getParentIdName ());
21
+ $ groupedNodes = $ this ->groupBy ($ this ->first ()->getParentIdName ());
22
22
23
23
/** @var NodeTrait|Model $node */
24
24
foreach ($ this ->items as $ node ) {
25
- if ( ! isset ( $ node ->parent )) {
25
+ if ( ! $ node ->getParentId ( )) {
26
26
$ node ->setRelation ('parent ' , null );
27
27
}
28
28
29
- $ children = $ groupedChildren ->get ($ node ->getKey (), [ ]);
29
+ $ children = $ groupedNodes ->get ($ node ->getKey (), [ ]);
30
30
31
31
/** @var Model|NodeTrait $child */
32
32
foreach ($ children as $ child ) {
@@ -40,29 +40,32 @@ public function linkNodes()
40
40
}
41
41
42
42
/**
43
- * Build tree from node list. Each item will have set children relation.
43
+ * Build a tree from a list of nodes . Each item will have set children relation.
44
44
*
45
45
* To successfully build tree "id", "_lft" and "parent_id" keys must present.
46
46
*
47
- * If `$rootNodeId` is provided, the tree will contain only descendants
48
- * of the node with such primary key value.
47
+ * If `$root` is provided, the tree will contain only descendants of that node.
49
48
*
50
49
* @param int|Model|null $root
51
50
*
52
51
* @return Collection
53
52
*/
54
53
public function toTree ($ root = null )
55
54
{
56
- $ items = [ ];
55
+ if ($ this ->isEmpty ()) {
56
+ return new static ;
57
+ }
57
58
58
- if ( ! $ this ->isEmpty ()) {
59
- $ this ->linkNodes ();
59
+ $ this ->linkNodes ();
60
60
61
- $ root = $ this -> getRootNodeId ( $ root ) ;
61
+ $ items = [ ] ;
62
62
63
- /** @var Model|NodeTrait $node */
64
- foreach ($ this ->items as $ node ) {
65
- if ($ node ->getParentId () == $ root ) $ items [] = $ node ;
63
+ $ root = $ this ->getRootNodeId ($ root );
64
+
65
+ /** @var Model|NodeTrait $node */
66
+ foreach ($ this ->items as $ node ) {
67
+ if ($ node ->getParentId () == $ root ) {
68
+ $ items [] = $ node ;
66
69
}
67
70
}
68
71
@@ -76,21 +79,23 @@ public function toTree($root = null)
76
79
*/
77
80
protected function getRootNodeId ($ root = null )
78
81
{
79
- if (NodeTrait:: hasTrait ($ root )) {
82
+ if (NestedSet:: isNode ($ root )) {
80
83
return $ root ->getKey ();
81
84
}
82
85
86
+ if ($ root !== null ) {
87
+ return $ root ;
88
+ }
89
+
83
90
// If root node is not specified we take parent id of node with
84
91
// least lft value as root node id.
85
- if ($ root === null ) {
86
- $ leastValue = null ;
87
-
88
- /** @var Model|NodeTrait $node */
89
- foreach ($ this ->items as $ node ) {
90
- if ($ leastValue === null || $ node ->getLft () < $ leastValue ) {
91
- $ leastValue = $ node ->getLft ();
92
- $ root = $ node ->getParentId ();
93
- }
92
+ $ leastValue = null ;
93
+
94
+ /** @var Model|NodeTrait $node */
95
+ foreach ($ this ->items as $ node ) {
96
+ if ($ leastValue === null || $ node ->getLft () < $ leastValue ) {
97
+ $ leastValue = $ node ->getLft ();
98
+ $ root = $ node ->getParentId ();
94
99
}
95
100
}
96
101
0 commit comments