1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Umbrellio\LTree\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
11
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
12
|
|
|
use Umbrellio\LTree\Collections\LTreeCollection; |
13
|
|
|
use Umbrellio\LTree\Interfaces\LTreeModelInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @mixin Model |
17
|
|
|
* @mixin LTreeModelInterface |
18
|
|
|
* @mixin SoftDeletes |
19
|
|
|
* @const string DELETED_AT |
20
|
|
|
*/ |
21
|
|
|
trait LTreeModelTrait |
22
|
|
|
{ |
23
|
|
|
use LTreeTrait; |
24
|
|
|
|
25
|
40 |
|
public function newCollection(array $models = []): LTreeCollection |
26
|
|
|
{ |
27
|
40 |
|
return new LTreeCollection($models); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
public function ltreeParent(): BelongsTo |
31
|
|
|
{ |
32
|
2 |
|
return $this->belongsTo(static::class, $this->getLtreeParentColumn()); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public function ltreeChildren(): HasMany |
36
|
|
|
{ |
37
|
2 |
|
return $this->hasMany(static::class, $this->getLtreeParentColumn()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function isParentOf(int $id): bool |
41
|
|
|
{ |
42
|
1 |
|
return self::descendantsOf($this)->withoutSelf($this->getKey())->find($id) !== null; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
public function scopeParentsOf(Builder $query, array $paths): Builder |
46
|
|
|
{ |
47
|
6 |
|
return $query->whereRaw(sprintf( |
|
|
|
|
48
|
6 |
|
"%s @> array['%s']::ltree[]", |
49
|
6 |
|
$this->getLtreePathColumn(), |
50
|
6 |
|
implode("', '", $paths) |
51
|
6 |
|
)); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function scopeRoot(Builder $query): Builder |
55
|
|
|
{ |
56
|
1 |
|
return $query->whereNull($this->getLtreeParentColumn()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
public function scopeDescendantsOf(Builder $query, LTreeModelInterface $model, bool $reverse = true): Builder |
60
|
|
|
{ |
61
|
7 |
|
return $query->whereRaw(sprintf( |
62
|
7 |
|
"({$this->getLtreePathColumn()} <@ text2ltree('%s')) = %s", |
63
|
7 |
|
$model->getLtreePath(LTreeModelInterface::AS_STRING), |
64
|
7 |
|
$reverse ? 'true' : 'false' |
65
|
7 |
|
)); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function scopeAncestorsOf(Builder $query, LTreeModelInterface $model, bool $reverse = true): Builder |
69
|
|
|
{ |
70
|
3 |
|
return $query->whereRaw(sprintf( |
71
|
3 |
|
"({$this->getLtreePathColumn()} @> text2ltree('%s')) = %s", |
72
|
3 |
|
$model->getLtreePath(LTreeModelInterface::AS_STRING), |
73
|
3 |
|
$reverse ? 'true' : 'false' |
74
|
3 |
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
public function scopeWithoutSelf(Builder $query, int $id): Builder |
78
|
|
|
{ |
79
|
5 |
|
return $query->whereRaw(sprintf('%s <> %s', $this->getKeyName(), $id)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
public function getLtreeProxyUpdateColumns(): array |
83
|
|
|
{ |
84
|
2 |
|
return [$this->getUpdatedAtColumn()]; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function getLtreeProxyDeleteColumns(): array |
88
|
|
|
{ |
89
|
1 |
|
return [$this->getDeletedAtColumn()]; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function getAncestorByLevel(int $level = 1) |
93
|
|
|
{ |
94
|
1 |
|
return static::ancestorByLevel($level)->first(); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function scopeAncestorByLevel(Builder $query, int $level = 1, ?string $path = null): Builder |
98
|
|
|
{ |
99
|
1 |
|
return $query->whereRaw(sprintf( |
100
|
1 |
|
"({$this->getLtreePathColumn()} @> text2ltree('%s')) and nlevel({$this->getLtreePathColumn()}) = %d", |
101
|
1 |
|
$path ?: $this->getLtreePath(LTreeModelInterface::AS_STRING), |
|
|
|
|
102
|
1 |
|
$level |
103
|
1 |
|
)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|