1 | <?php |
||
2 | /** |
||
3 | * Base daft objects. |
||
4 | * |
||
5 | * @author SignpostMarv |
||
6 | */ |
||
7 | declare(strict_types=1); |
||
8 | |||
9 | namespace SignpostMarv\DaftObject\EasyDB; |
||
10 | |||
11 | use PDO; |
||
12 | use SignpostMarv\DaftObject\DaftNestedWriteableObject; |
||
13 | use SignpostMarv\DaftObject\DefinesOwnArrayIdInterface; |
||
14 | use SignpostMarv\DaftObject\SuitableForRepositoryType; |
||
15 | |||
16 | /** |
||
17 | * @template T as DaftNestedWriteableObject |
||
18 | */ |
||
19 | trait WriteableTreeTrait |
||
20 | { |
||
21 | /** |
||
22 | * @param scalar|(scalar|array|object|null)[] $id |
||
23 | * |
||
24 | * @psalm-param class-string<T> $type |
||
25 | * |
||
26 | * @psalm-return T |
||
27 | */ |
||
28 | abstract public function RecallDaftObjectOrThrow( |
||
29 | $id, |
||
30 | string $type = SuitableForRepositoryType::class |
||
31 | ) : SuitableForRepositoryType; |
||
32 | |||
33 | abstract protected function DaftObjectDatabaseTable() : string; |
||
34 | |||
35 | /** |
||
36 | * @psalm-return T |
||
37 | */ |
||
38 | 24 | protected function ObtainLastLeafInTree() : DaftNestedWriteableObject |
|
39 | { |
||
40 | /** |
||
41 | * @var \ParagonIE\EasyDB\EasyDB |
||
42 | */ |
||
43 | 24 | $db = $this->db; |
|
44 | |||
45 | /** |
||
46 | * @psalm-var class-string<T> |
||
47 | */ |
||
48 | 24 | $type = $this->type; |
|
49 | |||
50 | 24 | $sth = $db->prepare( |
|
51 | 'SELECT ' . |
||
52 | 24 | implode(',', array_map( |
|
53 | 24 | [$db, 'escapeIdentifier'], |
|
54 | 24 | $type::DaftObjectIdProperties() |
|
55 | )) . |
||
56 | 24 | ' FROM ' . |
|
57 | 24 | $this->DaftObjectDatabaseTable() . |
|
58 | 24 | ' ORDER BY ' . |
|
59 | 24 | $db->escapeIdentifier('intNestedLeft') . |
|
60 | 24 | ' DESC LIMIT 1' |
|
61 | ); |
||
62 | |||
63 | 24 | $sth->execute(); |
|
64 | |||
65 | /** |
||
66 | * @var array<string, scalar> |
||
67 | */ |
||
68 | 24 | $res = $sth->fetch(PDO::FETCH_ASSOC); |
|
69 | |||
70 | 24 | if (1 === count($res) && ! is_a($type, DefinesOwnArrayIdInterface::class, true)) { |
|
71 | 24 | $res = current($res); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @var DaftNestedWriteableObject |
||
76 | * |
||
77 | * @psalm-var T |
||
78 | */ |
||
79 | 24 | $out = $this->RecallDaftObjectOrThrow($res); |
|
80 | |||
81 | 24 | return $out; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
82 | } |
||
83 | } |
||
84 |