Passed
Push — master ( 4bc3e6...eb0e31 )
by SignpostMarv
02:55
created

FilterQueryDaftNestedObjectTreeFromArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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
        $selecting = 'COUNT(*)';
183
184 28
        if ($recall) {
185
            /**
186
            * @var string[] $props
187
            */
188 28
            $props = $this->type::DaftObjectIdProperties();
189
190 28
            $escapedProps = [];
191
192 28
            foreach ($props as $prop) {
193 28
                $escapedProps[] = $this->db->escapeIdentifier($prop);
194
            }
195
196 28
            $selecting = implode(', ', array_map(
197
                function (string $prop) : string {
198 28
                    return $this->db->escapeIdentifier($prop);
199 28
                },
200 28
                $props
201
            ));
202
        }
203
204 28
        return $selecting;
205
    }
206
207
    /**
208
    * @param array<int, string> $filter
209
    */
210 28
    final protected function FilterQueryDaftNestedObjectTreeFromArgs(array $filter) : string
211
    {
212 28
        return (count($filter) > 0) ? (' WHERE ' . implode(' AND ', $filter)) : '';
213
    }
214
215 28
    protected function QueryDaftNestedObjectTreeFromArgs(
216
        bool $recall,
217
        ? int $left,
218
        ? int $right,
219
        ? int $limit,
220
        bool $withRoot,
221
        bool $treeNotPath = true
222
    ) : PDOStatement {
223 28
        $selecting = $this->SelectingQueryDaftNestedObjectTreeFromArgs($recall);
224
225 28
        $queryArgs = [];
226 28
        $filter = [];
227
228 28
        $leftOp = ($withRoot ? ' >= ' : ' > ');
229 28
        $rightOp = ($withRoot ? ' <= ' : ' < ');
230
231
        foreach (
232
            [
233
                (
234 28
                    $this->db->escapeIdentifier('intNestedLeft') .
235 28
                    ($treeNotPath ? $leftOp : $rightOp) .
236 28
                    ' ?'
237 28
                ) => $left,
238
                (
239 28
                    $this->db->escapeIdentifier('intNestedRight') .
240 28
                    ($treeNotPath ? $rightOp : $leftOp) .
241 28
                    ' ?'
242 28
                ) => $right,
243
            ] as $filterStr => $queryArgVar
244
        ) {
245 28
            if (is_int($queryArgVar)) {
246 20
                $queryArgs[] = $queryArgVar;
247 28
                $filter[] = $filterStr;
248
            }
249
        }
250
251 28
        if (is_int($limit)) {
252 28
            $queryArgs[] = $limit;
253 28
            $filter[] =
254 28
                $this->db->escapeIdentifier('intNestedLevel') .
255 28
                ' <= ?';
256
        }
257
258
        $query =
259
            'SELECT ' .
260 28
            $selecting .
261 28
            ' FROM ' .
262 28
            $this->db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
263 28
            $this->FilterQueryDaftNestedObjectTreeFromArgs($filter) .
264 28
            ' ORDER BY ' .
265 28
            $this->db->escapeIdentifier('intNestedLeft');
266
267 28
        $sth = $this->db->prepare($query);
268
269 28
        $sth->execute($queryArgs);
270
271 28
        return $sth;
272
    }
273
274 28
    protected function RecallDaftNestedObjectTreeFromArgs(
275
        ? int $left,
276
        ? int $right,
277
        ? int $limit,
278
        bool $withRoot,
279
        bool $treeNotPath = true
280
    ) : array {
281 28
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
282 28
            true,
283 28
            $left,
284 28
            $right,
285 28
            $limit,
286 28
            $withRoot,
287 28
            $treeNotPath
288
        );
289
290 28
        $out = [];
291
292
        /**
293
        * @var array<string, scalar> $id
294
        */
295 28
        foreach ($sth->fetchAll(PDO::FETCH_NUM) as $id) {
296 28
            $obj = $this->RecallDaftObject($id);
297
298 28
            if ($obj instanceof DaftNestedObject) {
299 28
                $out[] = $obj;
300
            }
301
        }
302
303 28
        return $out;
304
    }
305
306 20
    protected function CountDaftNestedObjectTreeFromArgs(
307
        ? int $left,
308
        ? int $right,
309
        ? int $limit,
310
        bool $withRoot,
311
        bool $treeNotPath = true
312
    ) : int {
313 20
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
314 20
            false,
315 20
            $left,
316 20
            $right,
317 20
            $limit,
318 20
            $withRoot,
319 20
            $treeNotPath
320
        );
321
322 20
        return (int) $sth->fetchColumn();
323
    }
324
}
325