umbrellio /
laravel-ltree
| 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); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 28 | } |
||||
| 29 | |||||
| 30 | 2 | public function ltreeParent(): BelongsTo |
|||
| 31 | { |
||||
| 32 | 2 | return $this->belongsTo(static::class, $this->getLtreeParentColumn()); |
|||
|
0 ignored issues
–
show
It seems like
belongsTo() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 33 | } |
||||
| 34 | |||||
| 35 | 2 | public function ltreeChildren(): HasMany |
|||
| 36 | { |
||||
| 37 | 2 | return $this->hasMany(static::class, $this->getLtreeParentColumn()); |
|||
|
0 ignored issues
–
show
It seems like
hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 38 | } |
||||
| 39 | |||||
| 40 | 1 | public function isParentOf(int $id): bool |
|||
| 41 | { |
||||
| 42 | 1 | return self::descendantsOf($this)->withoutSelf($this->getKey())->find($id) !== null; |
|||
|
0 ignored issues
–
show
It seems like
getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 43 | } |
||||
| 44 | |||||
| 45 | 6 | public function scopeParentsOf(Builder $query, array $paths): Builder |
|||
| 46 | { |
||||
| 47 | 6 | return $query->whereRaw(sprintf( |
|||
|
0 ignored issues
–
show
The method
whereRaw() does not exist on Illuminate\Database\Eloquent\Builder.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 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()); |
|||
|
0 ignored issues
–
show
The method
whereNull() does not exist on Illuminate\Database\Eloquent\Builder.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 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)); |
|||
|
0 ignored issues
–
show
It seems like
getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 80 | } |
||||
| 81 | |||||
| 82 | 2 | public function getLtreeProxyUpdateColumns(): array |
|||
| 83 | { |
||||
| 84 | 2 | return [$this->getUpdatedAtColumn()]; |
|||
|
0 ignored issues
–
show
It seems like
getUpdatedAtColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 85 | } |
||||
| 86 | |||||
| 87 | 1 | public function getLtreeProxyDeleteColumns(): array |
|||
| 88 | { |
||||
| 89 | 1 | return [$this->getDeletedAtColumn()]; |
|||
|
0 ignored issues
–
show
It seems like
getDeletedAtColumn() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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), |
|||
|
0 ignored issues
–
show
It seems like
$path ?: $this->getLtree...elInterface::AS_STRING) can also be of type array and string[]; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 102 | 1 | $level |
|||
| 103 | 1 | )); |
|||
| 104 | } |
||||
| 105 | } |
||||
| 106 |