Passed
Push — master ( 75eea4...64d761 )
by SignpostMarv
03:10
created

RecallDaftNestedObjectTreeWithId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 3
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 RuntimeException;
12
13
abstract class DaftObjectMemoryTree extends DaftObjectMemoryRepository implements DaftNestedObjectTree
14
{
15 14
    public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array
16
    {
17
        /**
18
        * @var string[] $props
19
        */
20 14
        $props = $this->type::DaftObjectIdProperties();
21
22
        /**
23
        * @var DaftNestedObject[] $out
24
        */
25 14
        $out = array_map(
26
            function (array $id) : DaftNestedObject {
27 14
                $out = $this->RecallDaftObject($id);
28
29 14
                if ( ! ($out instanceof DaftNestedObject)) {
30
                    throw new RuntimeException('Could not retrieve leaf from tree!');
31
                }
32
33 14
                return $out;
34 14
            },
35 14
            array_map(
36
                /**
37
                * @param array<string, scalar|null> $row
38
                */
39
                function (array $row) use ($props) : array {
40
                    /**
41
                    * @var array<string, scalar|null> $out
42
                    */
43 14
                    $out = [];
44
45
                    /**
46
                    * @var string $prop
47
                    */
48 14
                    foreach ($props as $prop) {
49 14
                        $out[$prop] = $row[$prop] ?? null;
50
                    }
51
52 14
                    return $out;
53 14
                },
54 14
                (array) $this->data
55
            )
56
        );
57
58
        usort($out, function (DaftNestedObject $a, DaftNestedObject $b) : int {
59 14
            return $a->GetIntNestedLeft() <=> $b->GetIntNestedLeft();
60 14
        });
61
62 14
        if (is_int($relativeDepthLimit)) {
63 14
            $out = array_filter(
64 14
                $out,
65
                function (DaftNestedObject $e) use ($relativeDepthLimit) : bool {
66 14
                    return $e->GetIntNestedLevel() <= $relativeDepthLimit;
67 14
                }
68
            );
69
        }
70
71 14
        return $out;
72
    }
73
74 6
    public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int
75
    {
76 6
        return count($this->RecallDaftNestedObjectFullTree($relativeDepthLimit));
77
    }
78
79
    /**
80
    * {@inheritdoc}
81
    */
82 6
    public function RecallDaftNestedObjectTreeWithObject(
83
        DaftNestedObject $root,
84
        bool $includeRoot,
85
        ? int $relativeDepthLimit
86
    ) : array {
87 6
        $left = $root->GetIntNestedLeft();
88 6
        $right = $root->GetIntNestedRight();
89
90 6
        if (is_int($relativeDepthLimit)) {
91 4
            $relativeDepthLimit = $root->GetIntNestedLevel() + $relativeDepthLimit;
92
        }
93
94 6
        return array_values(array_filter(
95 6
            $this->RecallDaftNestedObjectFullTree(),
96
            function (DaftNestedObject $e) use ($includeRoot, $left, $right, $relativeDepthLimit) : bool {
97
                if (
98 6
                    is_int($relativeDepthLimit) &&
99 6
                    $e->GetIntNestedLevel() > $relativeDepthLimit
100
                ) {
101 4
                    return false;
102 6
                } elseif ($includeRoot) {
103 4
                    return $e->GetIntNestedLeft() >= $left && $e->GetIntNestedRight() <= $right;
104
                }
105
106 6
                return $e->GetIntNestedLeft() > $left && $e->GetIntNestedRight() < $right;
107 6
            }
108
        ));
109
    }
110
111 6
    public function CountDaftNestedObjectTreeWithObject(
112
        DaftNestedObject $root,
113
        bool $includeRoot,
114
        ? int $relativeDepthLimit
115
    ) : int {
116 6
        return count(
117 6
            $this->RecallDaftNestedObjectTreeWithObject($root, $includeRoot, $relativeDepthLimit)
118
        );
119
    }
120
121 14
    public function RecallDaftNestedObjectTreeWithId(
122
        $id,
123
        bool $includeRoot,
124
        ? int $relativeDepthLimit
125
    ) : array {
126 14
        $object = $this->RecallDaftObject($id);
127
128
        return
129 14
            ($object instanceof DaftNestedObject)
130 4
                ? $this->RecallDaftNestedObjectTreeWithObject(
131 4
                    $object,
132 4
                    $includeRoot,
133 4
                    $relativeDepthLimit
134
                )
135
                : (
136 10
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
137 10
                        ? $this->RecallDaftNestedObjectFullTree(0)
138 14
                        : []
139
                );
140
    }
141
142 4
    public function CountDaftNestedObjectTreeWithId(
143
        $id,
144
        bool $includeRoot,
145
        ? int $relativeDepthLimit
146
    ) : int {
147 4
        return count($this->RecallDaftNestedObjectTreeWithId(
148 4
            $id,
149 4
            $includeRoot,
150 4
            $relativeDepthLimit
151
        ));
152
    }
153
154 4
    public function RecallDaftNestedObjectPathToObject(
155
        DaftNestedObject $leaf,
156
        bool $includeLeaf
157
    ) : array {
158 4
        $left = $leaf->GetIntNestedLeft();
159 4
        $right = $leaf->GetIntNestedRight();
160
161 4
        if ( ! $includeLeaf) {
162 4
            --$left;
163 4
            ++$right;
164
        }
165
166 4
        return array_values(array_filter(
167 4
            $this->RecallDaftNestedObjectFullTree(),
168
            function (DaftNestedObject $e) use ($left, $right) : bool {
169 4
                return $e->GetIntNestedLeft() <= $left && $e->GetIntNestedRight() >= $right;
170 4
            }
171
        ));
172
    }
173
174 4
    public function CountDaftNestedObjectPathToObject(
175
        DaftNestedObject $leaf,
176
        bool $includeLeaf
177
    ) : int {
178 4
        return count($this->RecallDaftNestedObjectPathToObject($leaf, $includeLeaf));
179
    }
180
181 4
    public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array
182
    {
183 4
        $object = $this->RecallDaftObject($id);
184
185
        return
186 4
            ($object instanceof DaftNestedObject)
187 4
                ? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf)
188 4
                : [];
189
    }
190
191
    /*
192
    * @param mixed $id
193
    */
194 4
    public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int
195
    {
196 4
        return count($this->RecallDaftNestedObjectPathToId($id, $includeLeaf));
197
    }
198
199 10
    public function CompareObjects(DaftNestedObject $a, DaftNestedObject $b) : int
200
    {
201 10
        return $a->GetIntNestedSortOrder() <=> $b->GetIntNestedSortOrder();
202
    }
203
204 18
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
205
    {
206 18
        static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__);
207
208 18
        parent::RememberDaftObjectData($object);
209 18
    }
210
211
    /**
212
    * @param DaftObject|string $object
213
    */
214 56
    protected static function ThrowIfNotType(
215
        $object,
216
        string $type,
217
        int $argument,
218
        string $function
219
    ) : void {
220 56
        parent::ThrowIfNotType($object, $type, $argument, $function);
221
222 56
        if ( ! is_a($object, DaftNestedObject::class, is_string($object))) {
223 2
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
224 2
                $argument,
225 2
                static::class,
226 2
                $function,
227 2
                DaftNestedObject::class,
228 2
                is_string($object) ? $object : get_class($object)
229
            );
230
        }
231 54
    }
232
}
233