Skip to content

Commit 0ef31ae

Browse files
committed
Refactored
1 parent 1bed9b3 commit 0ef31ae

File tree

4 files changed

+100
-140
lines changed

4 files changed

+100
-140
lines changed

src/Collection.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Collection extends BaseCollection
99
{
1010
/**
11-
* Fill `parent` and `children` relationships for every node in collection.
11+
* Fill `parent` and `children` relationships for every node in the collection.
1212
*
1313
* This will overwrite any previously set relations.
1414
*
@@ -18,15 +18,15 @@ public function linkNodes()
1818
{
1919
if ($this->isEmpty()) return $this;
2020

21-
$groupedChildren = $this->groupBy($this->first()->getParentIdName());
21+
$groupedNodes = $this->groupBy($this->first()->getParentIdName());
2222

2323
/** @var NodeTrait|Model $node */
2424
foreach ($this->items as $node) {
25-
if ( ! isset($node->parent)) {
25+
if ( ! $node->getParentId()) {
2626
$node->setRelation('parent', null);
2727
}
2828

29-
$children = $groupedChildren->get($node->getKey(), [ ]);
29+
$children = $groupedNodes->get($node->getKey(), [ ]);
3030

3131
/** @var Model|NodeTrait $child */
3232
foreach ($children as $child) {
@@ -40,29 +40,32 @@ public function linkNodes()
4040
}
4141

4242
/**
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.
4444
*
4545
* To successfully build tree "id", "_lft" and "parent_id" keys must present.
4646
*
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.
4948
*
5049
* @param int|Model|null $root
5150
*
5251
* @return Collection
5352
*/
5453
public function toTree($root = null)
5554
{
56-
$items = [ ];
55+
if ($this->isEmpty()) {
56+
return new static;
57+
}
5758

58-
if ( ! $this->isEmpty()) {
59-
$this->linkNodes();
59+
$this->linkNodes();
6060

61-
$root = $this->getRootNodeId($root);
61+
$items = [ ];
6262

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;
6669
}
6770
}
6871

@@ -76,21 +79,23 @@ public function toTree($root = null)
7679
*/
7780
protected function getRootNodeId($root = null)
7881
{
79-
if (NodeTrait::hasTrait($root)) {
82+
if (NestedSet::isNode($root)) {
8083
return $root->getKey();
8184
}
8285

86+
if ($root !== null) {
87+
return $root;
88+
}
89+
8390
// If root node is not specified we take parent id of node with
8491
// 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();
9499
}
95100
}
96101

src/NestedSet.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,16 @@ public static function getDefaultColumns()
6868
return [ self::LFT, self::RGT, self::PARENT_ID ];
6969
}
7070

71+
/**
72+
* Replaces instanceof calls for this trait.
73+
*
74+
* @param mixed $node
75+
*
76+
* @return bool
77+
*/
78+
public static function isNode($node)
79+
{
80+
return is_object($node) && in_array(NodeTrait::class, (array)$node);
81+
}
82+
7183
}

0 commit comments

Comments
 (0)