Passed
Push — master ( 6821c2...050b8d )
by SignpostMarv
03:14
created

SelectingQueryDaftNestedObjectTreeFromArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 2
rs 9.7
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 40
    public function CountDaftNestedObjectFullTree(int $withLimit = null) : int
23
    {
24 40
        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 12
        } elseif ( ! is_a($type, DaftNestedObject::class, true)) {
159 10
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
160 10
                1,
161 10
                static::class,
162 10
                __FUNCTION__,
163 10
                DaftNestedObject::class,
164 10
                $type
165
            );
166
        }
167
168 30
        return parent::DaftObjectRepositoryByType($type, $db);
169
    }
170
171 40
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
172
    {
173 40
        static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__);
174
175 40
        parent::RememberDaftObjectData($object);
176 40
    }
177
178 40
    final protected function SelectingQueryDaftNestedObjectTreeFromArgs(bool $recall) : string
179
    {
180 40
        if ($recall) {
181
            /**
182
            * @var string[] $props
183
            */
184 28
            $props = $this->type::DaftObjectIdProperties();
185
186 28
            return implode(', ', array_map(
187
                function (string $prop) : string {
188 28
                    return $this->db->escapeIdentifier($prop);
189 28
                },
190 28
                $props
191
            ));
192
        }
193
194 40
        return 'COUNT(*)';
195
    }
196
197
    /**
198
    * @param array<int, string> $filter
199
    */
200 40
    final protected function FilterQueryDaftNestedObjectTreeFromArgs(array $filter) : string
201
    {
202 40
        return (count($filter) > 0) ? (' WHERE ' . implode(' AND ', $filter)) : '';
203
    }
204
205
    /**
206
    * @return array<int, string>
207
    */
208 40
    final protected function LeftOpRightOpDaftNestedObjectTreeFromArgs(
209
        bool $withRoot,
210
        bool $treeNotPath
211
    ) : array {
212 40
        $leftOp = ($withRoot ? ' >= ' : ' > ');
213 40
        $rightOp = ($withRoot ? ' <= ' : ' < ');
214
215 40
        if ( ! $treeNotPath) {
216 4
            list($leftOp, $rightOp) = [$rightOp, $leftOp];
217
        }
218
219 40
        return [$leftOp, $rightOp];
220
    }
221
222 40
    protected function QueryDaftNestedObjectTreeFromArgs(
223
        bool $recall,
224
        ? int $left,
225
        ? int $right,
226
        ? int $limit,
227
        bool $withRoot,
228
        bool $treeNotPath = true
229
    ) : PDOStatement {
230 40
        $queryArgs = [];
231 40
        $filter = [];
232
233 40
        list($leftOp, $rightOp) = $this->LeftOpRightOpDaftNestedObjectTreeFromArgs(
234 40
            $withRoot,
235 40
            $treeNotPath
236
        );
237
238 40
        $escapedLeft = $this->db->escapeIdentifier('intNestedLeft');
239
240
        $maybeArgs = [
241 40
            ($escapedLeft . $leftOp . ' ?') => $left,
242 40
            ($this->db->escapeIdentifier('intNestedRight') . $rightOp . ' ?') => $right,
243 40
            ($this->db->escapeIdentifier('intNestedLevel') . ' <= ?') => $limit,
244
        ];
245
246 40
        foreach (array_filter($maybeArgs, 'is_int') as $filterStr => $queryArgVar) {
247 28
            $queryArgs[] = $queryArgVar;
248 28
            $filter[] = $filterStr;
249
        }
250
251
        $query =
252
            'SELECT ' .
253 40
            $this->SelectingQueryDaftNestedObjectTreeFromArgs($recall) .
254 40
            ' FROM ' .
255 40
            $this->db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
256 40
            $this->FilterQueryDaftNestedObjectTreeFromArgs($filter) .
257 40
            ' ORDER BY ' .
258 40
            $escapedLeft;
259
260 40
        $sth = $this->db->prepare($query);
261
262 40
        $sth->execute($queryArgs);
263
264 40
        return $sth;
265
    }
266
267 28
    protected function RecallDaftNestedObjectTreeFromArgs(
268
        ? int $left,
269
        ? int $right,
270
        ? int $limit,
271
        bool $withRoot,
272
        bool $treeNotPath = true
273
    ) : array {
274 28
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
275 28
            true,
276 28
            $left,
277 28
            $right,
278 28
            $limit,
279 28
            $withRoot,
280 28
            $treeNotPath
281
        );
282
283 28
        return array_filter(
284 28
            array_map([$this, 'RecallDaftObject'], $sth->fetchAll(PDO::FETCH_NUM)),
285
            function (? DaftObject $maybe) : bool {
286 28
                return $maybe instanceof DaftNestedObject;
287 28
            }
288
        );
289
    }
290
291 40
    protected function CountDaftNestedObjectTreeFromArgs(
292
        ? int $left,
293
        ? int $right,
294
        ? int $limit,
295
        bool $withRoot,
296
        bool $treeNotPath = true
297
    ) : int {
298 40
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
299 40
            false,
300 40
            $left,
301 40
            $right,
302 40
            $limit,
303 40
            $withRoot,
304 40
            $treeNotPath
305
        );
306
307 40
        return (int) $sth->fetchColumn();
308
    }
309
}
310