1 | <?php |
||||||||||
2 | |||||||||||
3 | declare(strict_types=1); |
||||||||||
4 | |||||||||||
5 | namespace Umbrellio\LTree\Helpers; |
||||||||||
6 | |||||||||||
7 | use Illuminate\Database\Eloquent\Model; |
||||||||||
8 | use Illuminate\Support\Facades\DB; |
||||||||||
9 | use Umbrellio\LTree\Interfaces\LTreeInterface; |
||||||||||
10 | use Umbrellio\LTree\Interfaces\LTreeModelInterface; |
||||||||||
11 | use Umbrellio\LTree\Types\LTreeType; |
||||||||||
12 | |||||||||||
13 | class LTreeHelper |
||||||||||
14 | { |
||||||||||
15 | /** |
||||||||||
16 | * @param LTreeInterface|Model $model |
||||||||||
17 | */ |
||||||||||
18 | public function buildPath($model): void |
||||||||||
19 | { |
||||||||||
20 | $pathValue = []; |
||||||||||
21 | if ($model->getLtreeParentId()) { |
||||||||||
0 ignored issues
–
show
Bug
introduced
by
![]() The expression
$model->getLtreeParentId() of type integer|null is loosely compared to true ; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||||||||||
22 | $parent = $model->ltreeParent; |
||||||||||
23 | $pathValue = array_merge($pathValue, $parent->getLtreePath()); |
||||||||||
24 | } |
||||||||||
25 | $pathValue[] = $model->getKey(); |
||||||||||
0 ignored issues
–
show
The method
getKey() does not exist on Illuminate\Database\Eloquent\Model . It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The method
getKey() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface . Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||||
26 | DB::statement(sprintf( |
||||||||||
27 | "UPDATE %s SET %s = text2ltree('%s') WHERE %s = %s", |
||||||||||
28 | $model->getTable(), |
||||||||||
0 ignored issues
–
show
The method
getTable() does not exist on Illuminate\Database\Eloquent\Model . It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Illuminate\Database\Eloquent\Relations\Pivot .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The method
getTable() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface .
(
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. ![]() |
|||||||||||
29 | $model->getLtreePathColumn(), |
||||||||||
0 ignored issues
–
show
The method
getLtreePathColumn() does not exist on Illuminate\Database\Eloquent\Model . It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||||
30 | implode(LTreeType::TYPE_SEPARATE, $pathValue), |
||||||||||
31 | $model->getKeyName(), |
||||||||||
0 ignored issues
–
show
The method
getKeyName() does not exist on Illuminate\Database\Eloquent\Model . It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The method
getKeyName() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface . Since it exists in all sub-types, consider adding an abstract or default implementation to Umbrellio\LTree\Interfaces\LTreeInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||||
32 | $model->getKey() |
||||||||||
33 | )); |
||||||||||
34 | $model->refresh(); |
||||||||||
0 ignored issues
–
show
The method
refresh() does not exist on Illuminate\Database\Eloquent\Model .
(
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. ![]() The method
refresh() does not exist on Umbrellio\LTree\Interfaces\LTreeInterface .
(
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. ![]() |
|||||||||||
35 | } |
||||||||||
36 | |||||||||||
37 | /** |
||||||||||
38 | * @param LTreeInterface|Model $model |
||||||||||
39 | * @param LTreeInterface|Model|null $to |
||||||||||
40 | */ |
||||||||||
41 | public function moveNode($model, $to = null, array $columns = []): void |
||||||||||
42 | { |
||||||||||
43 | $pathName = $model->getLtreePathColumn(); |
||||||||||
44 | $oldPath = $model->getLtreePath(LTreeModelInterface::AS_STRING); |
||||||||||
0 ignored issues
–
show
The method
getLtreePath() does not exist on Illuminate\Database\Eloquent\Model . It seems like you code against a sub-type of Illuminate\Database\Eloquent\Model such as Umbrellio\LTree\tests\_data\Models\CategoryStub or Umbrellio\LTree\tests\_data\Models\ProductStub .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||||
45 | $newPath = $to ? $to->getLtreePath(LTreeModelInterface::AS_STRING) : ''; |
||||||||||
46 | $expressions = static::wrapExpressions($columns); |
||||||||||
47 | $expressions[] = " |
||||||||||
48 | \"${pathName}\" = (text2ltree('${newPath}') || subpath(\"${pathName}\", (nlevel(text2ltree('${oldPath}')) - 1))) |
||||||||||
49 | "; |
||||||||||
50 | |||||||||||
51 | DB::statement(sprintf( |
||||||||||
52 | "UPDATE %s SET %s WHERE (%s <@ text2ltree('%s')) = true", |
||||||||||
53 | $model->getTable(), |
||||||||||
54 | implode(', ', $expressions), |
||||||||||
55 | $pathName, |
||||||||||
56 | $oldPath |
||||||||||
57 | )); |
||||||||||
58 | $model->refresh(); |
||||||||||
59 | } |
||||||||||
60 | |||||||||||
61 | /** |
||||||||||
62 | * @param LTreeInterface|Model $model |
||||||||||
63 | */ |
||||||||||
64 | public function dropDescendants($model, array $columns = []): void |
||||||||||
65 | { |
||||||||||
66 | $sql = sprintf( |
||||||||||
67 | "UPDATE %s SET %s WHERE (%s <@ text2ltree('%s')) = true", |
||||||||||
68 | $model->getTable(), |
||||||||||
69 | implode(', ', static::wrapExpressions($columns)), |
||||||||||
70 | $model->getLtreePathColumn(), |
||||||||||
71 | $model->getLtreePath(LTreeModelInterface::AS_STRING) |
||||||||||
72 | ); |
||||||||||
73 | DB::statement($sql); |
||||||||||
74 | $model->refresh(); |
||||||||||
75 | } |
||||||||||
76 | |||||||||||
77 | private function wrapExpressions(array $columns): array |
||||||||||
78 | { |
||||||||||
79 | $expressions = []; |
||||||||||
80 | foreach ($columns as $column => $value) { |
||||||||||
81 | switch (true) { |
||||||||||
82 | case $value === null: |
||||||||||
83 | $expressions[] = sprintf('%s = null', (string) $column); |
||||||||||
84 | break; |
||||||||||
85 | case is_string($value): |
||||||||||
86 | $expressions[] = sprintf("%s = '%s'", (string) $column, (string) $value); |
||||||||||
87 | break; |
||||||||||
88 | default: |
||||||||||
89 | $expressions[] = sprintf('%s = %s', (string) $column, (string) $value); |
||||||||||
90 | } |
||||||||||
91 | } |
||||||||||
92 | return $expressions; |
||||||||||
93 | } |
||||||||||
94 | } |
||||||||||
95 |