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 | public function getLtreeParentColumn(): string |
||
18 | { |
||
19 | return 'parent_id'; |
||
20 | } |
||
21 | |||
22 | public function getLtreePathColumn(): string |
||
23 | { |
||
24 | return 'path'; |
||
25 | } |
||
26 | |||
27 | public function getLtreeParentId(): ?int |
||
28 | { |
||
29 | $value = $this->getAttribute($this->getLtreeParentColumn()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
30 | return $value ? (int) $value : null; |
||
31 | } |
||
32 | |||
33 | public function getLtreePath($mode = LTreeInterface::AS_ARRAY) |
||
34 | { |
||
35 | $path = $this->getAttribute($this->getLtreePathColumn()); |
||
36 | if ($mode === LTreeModelInterface::AS_ARRAY) { |
||
37 | return $path !== null ? explode(LTreeType::TYPE_SEPARATE, $path) : []; |
||
38 | } |
||
39 | return (string) $path; |
||
40 | } |
||
41 | |||
42 | public function getLtreeLevel(): int |
||
43 | { |
||
44 | return is_array($path = $this->getLtreePath()) ? count($path) : 1; |
||
45 | } |
||
46 | } |
||
47 |