umbrellio /
laravel-ltree
| 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 | 23 | public function toTree(bool $usingSort = true, bool $loadMissing = true): LTreeNode |
|
| 25 | { |
||
| 26 | 23 | if (!$model = $this->first()) { |
|
| 27 | 1 | return new LTreeNode(); |
|
| 28 | } |
||
| 29 | |||
| 30 | 22 | if ($loadMissing) { |
|
| 31 | 18 | $this->loadMissingNodes($model); |
|
| 32 | } |
||
| 33 | |||
| 34 | 22 | if (!$this->withLeaves) { |
|
| 35 | 1 | $this->excludeLeaves(); |
|
| 36 | } |
||
| 37 | |||
| 38 | 22 | $builder = new LTreeBuilder( |
|
| 39 | 22 | $model->getLtreePathColumn(), |
|
| 40 | 22 | $model->getKeyName(), |
|
| 41 | 22 | $model->getLtreeParentColumn() |
|
| 42 | 22 | ); |
|
| 43 | |||
| 44 | 22 | return $builder->build($collection ?? $this, $usingSort); |
|
| 45 | } |
||
| 46 | |||
| 47 | 1 | public function withLeaves(bool $state = true): self |
|
| 48 | { |
||
| 49 | 1 | $this->withLeaves = $state; |
|
| 50 | |||
| 51 | 1 | return $this; |
|
| 52 | } |
||
| 53 | |||
| 54 | 18 | public function loadMissingNodes($model): self |
|
| 55 | { |
||
| 56 | 18 | if ($this->hasMissingNodes($model)) { |
|
| 57 | 4 | $this->appendAncestors($model); |
|
| 58 | } |
||
| 59 | |||
| 60 | 18 | return $this; |
|
| 61 | } |
||
| 62 | |||
| 63 | 1 | private function excludeLeaves(): void |
|
| 64 | { |
||
| 65 | 1 | foreach ($this->items as $key => $item) { |
|
| 66 | /** @var LTreeModelTrait $item */ |
||
| 67 | 1 | if ($item->ltreeChildren->isEmpty() && $item->getLtreeParentId()) { |
|
|
0 ignored issues
–
show
|
|||
| 68 | 1 | $this->forget($key); |
|
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param LTreeInterface|ModelInterface $model |
||
| 75 | */ |
||
| 76 | 18 | private function hasMissingNodes($model): bool |
|
| 77 | { |
||
| 78 | 18 | $paths = collect(); |
|
| 79 | |||
| 80 | 18 | foreach ($this->items as $item) { |
|
| 81 | 18 | $paths = $paths->merge($item->getLtreePath()); |
|
| 82 | } |
||
| 83 | |||
| 84 | 18 | return $paths |
|
| 85 | 18 | ->unique() |
|
| 86 | 18 | ->diff($this->pluck($model->getKeyName())) |
|
| 87 | 18 | ->isNotEmpty(); |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param LTreeInterface|ModelInterface $model |
||
| 92 | */ |
||
| 93 | 4 | private function appendAncestors($model): void |
|
| 94 | { |
||
| 95 | 4 | $paths = $this |
|
| 96 | 4 | ->pluck($model->getLtreePathColumn()) |
|
| 97 | 4 | ->toArray(); |
|
| 98 | 4 | $ids = $this |
|
| 99 | 4 | ->pluck($model->getKeyName()) |
|
| 100 | 4 | ->toArray(); |
|
| 101 | |||
| 102 | /** @var Model $model */ |
||
| 103 | 4 | $parents = $model::parentsOf($paths) |
|
| 104 | 4 | ->whereKeyNot($ids) |
|
| 105 | 4 | ->get(); |
|
| 106 | |||
| 107 | 4 | foreach ($parents as $item) { |
|
| 108 | 3 | $this->add($item); |
|
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: