1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Umbrellio\LTree\Traits; |
||
6 | |||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Umbrellio\LTree\Interfaces\LTreeInterface; |
||
9 | use Umbrellio\LTree\Interfaces\LTreeModelInterface; |
||
10 | use Umbrellio\LTree\Types\LTreeType; |
||
11 | |||
12 | /** |
||
13 | * @mixin Model |
||
14 | */ |
||
15 | trait LTreeTrait |
||
16 | { |
||
17 | 27 | public function getLtreeParentColumn(): string |
|
18 | { |
||
19 | 27 | return 'parent_id'; |
|
20 | } |
||
21 | |||
22 | 38 | public function getLtreePathColumn(): string |
|
23 | { |
||
24 | 38 | return 'path'; |
|
25 | } |
||
26 | |||
27 | 4 | public function getLtreeParentId(): ?int |
|
28 | { |
||
29 | 4 | $value = $this->getAttribute($this->getLtreeParentColumn()); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
30 | 4 | return $value ? (int) $value : null; |
|
31 | } |
||
32 | |||
33 | 32 | public function getLtreePath($mode = LTreeInterface::AS_ARRAY) |
|
34 | { |
||
35 | 32 | $path = $this->getAttribute($this->getLtreePathColumn()); |
|
36 | 32 | if ($mode === LTreeModelInterface::AS_ARRAY) { |
|
37 | 23 | return $path !== null ? explode(LTreeType::TYPE_SEPARATE, $path) : []; |
|
38 | } |
||
39 | 13 | return (string) $path; |
|
40 | } |
||
41 | |||
42 | 3 | public function getLtreeLevel(): int |
|
43 | { |
||
44 | 3 | return is_array($path = $this->getLtreePath()) ? count($path) : 1; |
|
45 | } |
||
46 | } |
||
47 |