Passed
Push — master ( eb0e31...13a668 )
by SignpostMarv
03:09
created

SelectingQueryDaftNestedObjectTreeFromArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
use ParagonIE\EasyDB\EasyDB;
12
use PDO;
13
use PDOStatement;
14
15
abstract class AbstractDaftObjectEasyDBTree extends AbstractDaftObjectEasyDBRepository implements DaftNestedObjectTree
16
{
17 28
    public function RecallDaftNestedObjectFullTree(int $limit = null) : array
18
    {
19 28
        return $this->RecallDaftNestedObjectTreeFromArgs(null, null, $limit, false);
20
    }
21
22 14
    public function CountDaftNestedObjectFullTree(int $withLimit = null) : int
23
    {
24 14
        return $this->CountDaftNestedObjectTreeFromArgs(null, null, $withLimit, false);
25
    }
26
27 14
    public function RecallDaftNestedObjectTreeWithObject(
28
        DaftNestedObject $root,
29
        bool $withRoot,
30
        ? int $limit
31
    ) : array {
32 14
        $left = $root->GetIntNestedLeft();
33 14
        $right = $root->GetIntNestedRight();
34 14
        $limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null;
35
36 14
        return $this->RecallDaftNestedObjectTreeFromArgs($left, $right, $limit, $withRoot);
37
    }
38
39 20
    public function CountDaftNestedObjectTreeWithObject(
40
        DaftNestedObject $root,
41
        bool $withRoot,
42
        ? int $limit
43
    ) : int {
44 20
        $left = $root->GetIntNestedLeft();
45 20
        $right = $root->GetIntNestedRight();
46 20
        $limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null;
47
48 20
        return $this->CountDaftNestedObjectTreeFromArgs($left, $right, $limit, $withRoot);
49
    }
50
51 28
    public function RecallDaftNestedObjectTreeWithId(
52
        $id,
53
        bool $withRoot,
54
        ? int $limit
55
    ) : array {
56 28
        $object = $this->RecallDaftObject($id);
57
58
        return
59 28
            ($object instanceof DaftNestedObject)
60 4
                ? $this->RecallDaftNestedObjectTreeWithObject(
61 4
                    $object,
62 4
                    $withRoot,
63 4
                    $limit
64
                )
65
                : (
66 24
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
67 24
                        ? $this->RecallDaftNestedObjectFullTree(0)
68 28
                        : []
69
                );
70
    }
71
72 4
    public function CountDaftNestedObjectTreeWithId(
73
        $id,
74
        bool $withRoot,
75
        ? int $limit
76
    ) : int {
77 4
        $object = $this->RecallDaftObject($id);
78
79
        return
80 4
            ($object instanceof DaftNestedObject)
81 4
                ? $this->CountDaftNestedObjectTreeWithObject(
82 4
                    $object,
83 4
                    $withRoot,
84 4
                    $limit
85
                )
86
                : (
87 4
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
88 4
                        ? $this->CountDaftNestedObjectFullTree($limit)
89 4
                        : 0
90
                );
91
    }
92
93 4
    public function RecallDaftNestedObjectPathToObject(
94
        DaftNestedObject $leaf,
95
        bool $includeLeaf
96
    ) : array {
97 4
        return $this->RecallDaftNestedObjectTreeFromArgs(
98 4
            $leaf->GetIntNestedLeft(),
99 4
            $leaf->GetIntNestedRight(),
100 4
            null,
101 4
            $includeLeaf,
102 4
            false
103
        );
104
    }
105
106 4
    public function CountDaftNestedObjectPathToObject(
107
        DaftNestedObject $leaf,
108
        bool $includeLeaf
109
    ) : int {
110 4
        return $this->CountDaftNestedObjectTreeFromArgs(
111 4
            $leaf->GetIntNestedLeft(),
112 4
            $leaf->GetIntNestedRight(),
113 4
            null,
114 4
            $includeLeaf,
115 4
            false
116
        );
117
    }
118
119 4
    public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array
120
    {
121 4
        $object = $this->RecallDaftObject($id);
122
123
        return
124 4
            ($object instanceof DaftNestedObject)
125 4
                ? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf)
126 4
                : [];
127
    }
128
129 4
    public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int
130
    {
131 4
        $object = $this->RecallDaftObject($id);
132
133
        return
134 4
            ($object instanceof DaftNestedObject)
135 4
                ? $this->CountDaftNestedObjectPathToObject($object, $includeLeaf)
136 4
                : 0;
137
    }
138
139 24
    public function CompareObjects(DaftNestedObject $a, DaftNestedObject $b) : int
140
    {
141 24
        return $a->GetIntNestedSortOrder() <=> $b->GetIntNestedSortOrder();
142
    }
143
144 50
    public static function DaftObjectRepositoryByType(
145
        string $type,
146
        ? EasyDB $db = null
147
    ) : DaftObjectRepository {
148 50
        if (is_a(static::class, DaftNestedWriteableObjectTree::class, true)) {
149 38
            if ( ! is_a($type, DaftNestedWriteableObject::class, true)) {
150 10
                throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
151 10
                    1,
152 10
                    static::class,
153 10
                    __FUNCTION__,
154 10
                    DaftNestedWriteableObject::class,
155 38
                    $type
156
                );
157
            }
158
        } else {
159 12
            if ( ! is_a($type, DaftNestedObject::class, true)) {
160 10
                throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
161 10
                1,
162 10
                static::class,
163 10
                __FUNCTION__,
164 10
                DaftNestedObject::class,
165 10
                $type
166
            );
167
            }
168
        }
169
170 30
        return parent::DaftObjectRepositoryByType($type, $db);
171
    }
172
173 40
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
174
    {
175 40
        static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__);
176
177 40
        parent::RememberDaftObjectData($object);
178 40
    }
179
180 28
    final protected function SelectingQueryDaftNestedObjectTreeFromArgs(bool $recall) : string
181
    {
182 28
        if ($recall) {
183
            /**
184
            * @var string[] $props
185
            */
186 28
            $props = $this->type::DaftObjectIdProperties();
187
188 28
            return implode(', ', array_map(
189
                function (string $prop) : string {
190 28
                    return $this->db->escapeIdentifier($prop);
191 28
                },
192 28
                $props
193
            ));
194
        }
195
196 20
        return 'COUNT(*)';
197
    }
198
199
    /**
200
    * @param array<int, string> $filter
201
    */
202 28
    final protected function FilterQueryDaftNestedObjectTreeFromArgs(array $filter) : string
203
    {
204 28
        return (count($filter) > 0) ? (' WHERE ' . implode(' AND ', $filter)) : '';
205
    }
206
207 28
    protected function QueryDaftNestedObjectTreeFromArgs(
208
        bool $recall,
209
        ? int $left,
210
        ? int $right,
211
        ? int $limit,
212
        bool $withRoot,
213
        bool $treeNotPath = true
214
    ) : PDOStatement {
215 28
        $selecting = $this->SelectingQueryDaftNestedObjectTreeFromArgs($recall);
216
217 28
        $queryArgs = [];
218 28
        $filter = [];
219
220 28
        $leftOp = ($withRoot ? ' >= ' : ' > ');
221 28
        $rightOp = ($withRoot ? ' <= ' : ' < ');
222
223
        foreach (
224
            [
225
                (
226 28
                    $this->db->escapeIdentifier('intNestedLeft') .
227 28
                    ($treeNotPath ? $leftOp : $rightOp) .
228 28
                    ' ?'
229 28
                ) => $left,
230
                (
231 28
                    $this->db->escapeIdentifier('intNestedRight') .
232 28
                    ($treeNotPath ? $rightOp : $leftOp) .
233 28
                    ' ?'
234 28
                ) => $right,
235
            ] as $filterStr => $queryArgVar
236
        ) {
237 28
            if (is_int($queryArgVar)) {
238 20
                $queryArgs[] = $queryArgVar;
239 28
                $filter[] = $filterStr;
240
            }
241
        }
242
243 28
        if (is_int($limit)) {
244 28
            $queryArgs[] = $limit;
245 28
            $filter[] =
246 28
                $this->db->escapeIdentifier('intNestedLevel') .
247 28
                ' <= ?';
248
        }
249
250
        $query =
251
            'SELECT ' .
252 28
            $selecting .
253 28
            ' FROM ' .
254 28
            $this->db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
255 28
            $this->FilterQueryDaftNestedObjectTreeFromArgs($filter) .
256 28
            ' ORDER BY ' .
257 28
            $this->db->escapeIdentifier('intNestedLeft');
258
259 28
        $sth = $this->db->prepare($query);
260
261 28
        $sth->execute($queryArgs);
262
263 28
        return $sth;
264
    }
265
266 28
    protected function RecallDaftNestedObjectTreeFromArgs(
267
        ? int $left,
268
        ? int $right,
269
        ? int $limit,
270
        bool $withRoot,
271
        bool $treeNotPath = true
272
    ) : array {
273 28
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
274 28
            true,
275 28
            $left,
276 28
            $right,
277 28
            $limit,
278 28
            $withRoot,
279 28
            $treeNotPath
280
        );
281
282 28
        $out = [];
283
284
        /**
285
        * @var array<string, scalar> $id
286
        */
287 28
        foreach ($sth->fetchAll(PDO::FETCH_NUM) as $id) {
288 28
            $obj = $this->RecallDaftObject($id);
289
290 28
            if ($obj instanceof DaftNestedObject) {
291 28
                $out[] = $obj;
292
            }
293
        }
294
295 28
        return $out;
296
    }
297
298 20
    protected function CountDaftNestedObjectTreeFromArgs(
299
        ? int $left,
300
        ? int $right,
301
        ? int $limit,
302
        bool $withRoot,
303
        bool $treeNotPath = true
304
    ) : int {
305 20
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
306 20
            false,
307 20
            $left,
308 20
            $right,
309 20
            $limit,
310 20
            $withRoot,
311 20
            $treeNotPath
312
        );
313
314 20
        return (int) $sth->fetchColumn();
315
    }
316
}
317