-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathCollection.php
143 lines (115 loc) · 3.43 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Kalnoy\Nestedset;
use Illuminate\Database\Eloquent\Collection as BaseCollection;
use Illuminate\Database\Eloquent\Model;
class Collection extends BaseCollection
{
/**
* Fill `parent` and `children` relationships for every node in the collection.
*
* This will overwrite any previously set relations.
*
* @return $this
*/
public function linkNodes()
{
if ($this->isEmpty()) return $this;
$groupedNodes = $this->groupBy($this->first()->getParentIdName());
/** @var NodeTrait|Model $node */
foreach ($this->items as $node) {
if ( ! $node->getParentId()) {
$node->setRelation('parent', null);
}
$children = $groupedNodes->get($node->getKey(), [ ]);
/** @var Model|NodeTrait $child */
foreach ($children as $child) {
$child->setRelation('parent', $node);
}
$node->setRelation('children', BaseCollection::make($children));
}
return $this;
}
/**
* Build a tree from a list of nodes. Each item will have set children relation.
*
* To successfully build tree "id", "_lft" and "parent_id" keys must present.
*
* If `$root` is provided, the tree will contain only descendants of that node.
*
* @param mixed $root
*
* @return Collection
*/
public function toTree($root = false)
{
if ($this->isEmpty()) {
return new static;
}
$this->linkNodes();
$items = [ ];
$root = $this->getRootNodeId($root);
/** @var Model|NodeTrait $node */
foreach ($this->items as $node) {
if ($node->getParentId() == $root) {
$items[] = $node;
}
}
return new static($items);
}
/**
* @param mixed $root
*
* @return int
*/
protected function getRootNodeId($root = false)
{
if (NestedSet::isNode($root)) {
return $root->getKey();
}
if ($root !== false) {
return $root;
}
// If root node is not specified we take parent id of node with
// least lft value as root node id.
$leastValue = null;
/** @var Model|NodeTrait $node */
foreach ($this->items as $node) {
if ($leastValue === null || $node->getLft() < $leastValue) {
$leastValue = $node->getLft();
$root = $node->getParentId();
}
}
return $root;
}
/**
* Build a list of nodes that retain the order that they were pulled from
* the database.
*
* @param bool $root
*
* @return static
*/
public function toFlatTree($root = false)
{
$result = new static;
if ($this->isEmpty()) return $result;
$groupedNodes = $this->groupBy($this->first()->getParentIdName());
return $result->flattenTree($groupedNodes, $this->getRootNodeId($root));
}
/**
* Flatten a tree into a non recursive array.
*
* @param Collection $groupedNodes
* @param mixed $parentId
*
* @return $this
*/
protected function flattenTree(self $groupedNodes, $parentId)
{
foreach ($groupedNodes->get($parentId, []) as $node) {
$this->push($node);
$this->flattenTree($groupedNodes, $node->getKey());
}
return $this;
}
}