1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Umbrellio\LTree\Collections; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Umbrellio\LTree\Helpers\LTreeBuilder; |
10
|
|
|
use Umbrellio\LTree\Helpers\LTreeNode; |
11
|
|
|
use Umbrellio\LTree\Interfaces\HasLTreeRelations; |
12
|
|
|
use Umbrellio\LTree\Interfaces\LTreeInterface; |
13
|
|
|
use Umbrellio\LTree\Interfaces\ModelInterface; |
14
|
|
|
use Umbrellio\LTree\Traits\LTreeModelTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @method LTreeInterface|ModelInterface first() |
18
|
|
|
* @property LTreeInterface[]|ModelInterface[]|HasLTreeRelations[] $items |
19
|
|
|
*/ |
20
|
|
|
class LTreeCollection extends Collection |
21
|
|
|
{ |
22
|
|
|
private $withLeaves = true; |
23
|
|
|
|
24
|
|
|
public function toTree(bool $usingSort = true, bool $loadMissing = true): LTreeNode |
25
|
|
|
{ |
26
|
|
|
if (!$model = $this->first()) { |
27
|
|
|
return new LTreeNode(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($loadMissing) { |
31
|
|
|
$this->loadMissingNodes($model); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (!$this->withLeaves) { |
35
|
|
|
$this->excludeLeaves(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$builder = new LTreeBuilder( |
39
|
|
|
$model->getLtreePathColumn(), |
40
|
|
|
$model->getKeyName(), |
41
|
|
|
$model->getLtreeParentColumn() |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
return $builder->build($collection ?? $this, $usingSort); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function withLeaves(bool $state = true): self |
48
|
|
|
{ |
49
|
|
|
$this->withLeaves = $state; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function loadMissingNodes($model): self |
55
|
|
|
{ |
56
|
|
|
if ($this->hasMissingNodes($model)) { |
57
|
|
|
$this->appendAncestors($model); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function excludeLeaves(): void |
64
|
|
|
{ |
65
|
|
|
foreach ($this->items as $key => $item) { |
66
|
|
|
/** @var LTreeModelTrait $item */ |
67
|
|
|
if ($item->ltreeChildren->isEmpty()) { |
68
|
|
|
$this->forget($key); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param LTreeInterface|ModelInterface $model |
75
|
|
|
*/ |
76
|
|
|
private function hasMissingNodes($model): bool |
77
|
|
|
{ |
78
|
|
|
$paths = collect(); |
79
|
|
|
|
80
|
|
|
foreach ($this->items as $item) { |
81
|
|
|
$paths = $paths->merge($item->getLtreePath()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $paths |
85
|
|
|
->unique() |
86
|
|
|
->diff($this->pluck($model->getKeyName())) |
|
|
|
|
87
|
|
|
->isNotEmpty(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param LTreeInterface|ModelInterface $model |
92
|
|
|
*/ |
93
|
|
|
private function appendAncestors($model): void |
94
|
|
|
{ |
95
|
|
|
$paths = $this |
96
|
|
|
->pluck($model->getLtreePathColumn()) |
|
|
|
|
97
|
|
|
->toArray(); |
98
|
|
|
$ids = $this |
99
|
|
|
->pluck($model->getKeyName()) |
100
|
|
|
->toArray(); |
101
|
|
|
|
102
|
|
|
/** @var Model $model */ |
103
|
|
|
$parents = $model::parentsOf($paths) |
|
|
|
|
104
|
|
|
->whereKeyNot($ids) |
|
|
|
|
105
|
|
|
->get(); |
106
|
|
|
|
107
|
|
|
foreach ($parents as $item) { |
108
|
|
|
$this->add($item); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|