Passed
Push — master ( 36d137...3e275c )
by SignpostMarv
02:51
created

RecallDaftNestedObjectTreeWithObject()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 2
nop 3
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
ccs 14
cts 14
cp 1
crap 4
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 (
97
                DaftNestedObject $e
98
            ) use (
99 6
                $includeRoot,
100 6
                $left,
101 6
                $right,
102 6
                $relativeDepthLimit
103
            ) : bool {
104 6
                if (is_int($relativeDepthLimit) && $e->GetIntNestedLevel() > $relativeDepthLimit) {
105 4
                    return false;
106
                }
107
108 6
                return $this->FilterLeaf($includeRoot, $left, $right, $e);
109 6
            }
110
        ));
111
    }
112
113 6
    public function CountDaftNestedObjectTreeWithObject(
114
        DaftNestedObject $root,
115
        bool $includeRoot,
116
        ? int $relativeDepthLimit
117
    ) : int {
118 6
        return count(
119 6
            $this->RecallDaftNestedObjectTreeWithObject($root, $includeRoot, $relativeDepthLimit)
120
        );
121
    }
122
123 14
    public function RecallDaftNestedObjectTreeWithId(
124
        $id,
125
        bool $includeRoot,
126
        ? int $relativeDepthLimit
127
    ) : array {
128 14
        $object = $this->RecallDaftObject($id);
129
130
        return
131 14
            ($object instanceof DaftNestedObject)
132 4
                ? $this->RecallDaftNestedObjectTreeWithObject(
133 4
                    $object,
134 4
                    $includeRoot,
135 4
                    $relativeDepthLimit
136
                )
137
                : (
138 10
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
139 10
                        ? $this->RecallDaftNestedObjectFullTree(0)
140 14
                        : []
141
                );
142
    }
143
144 4
    public function CountDaftNestedObjectTreeWithId(
145
        $id,
146
        bool $includeRoot,
147
        ? int $relativeDepthLimit
148
    ) : int {
149 4
        return count($this->RecallDaftNestedObjectTreeWithId(
150 4
            $id,
151 4
            $includeRoot,
152 4
            $relativeDepthLimit
153
        ));
154
    }
155
156 4
    public function RecallDaftNestedObjectPathToObject(
157
        DaftNestedObject $leaf,
158
        bool $includeLeaf
159
    ) : array {
160 4
        $left = $leaf->GetIntNestedLeft();
161 4
        $right = $leaf->GetIntNestedRight();
162
163 4
        if ( ! $includeLeaf) {
164 4
            --$left;
165 4
            ++$right;
166
        }
167
168 4
        return array_values(array_filter(
169 4
            $this->RecallDaftNestedObjectFullTree(),
170
            function (DaftNestedObject $e) use ($left, $right) : bool {
171 4
                return $e->GetIntNestedLeft() <= $left && $e->GetIntNestedRight() >= $right;
172 4
            }
173
        ));
174
    }
175
176 4
    public function CountDaftNestedObjectPathToObject(
177
        DaftNestedObject $leaf,
178
        bool $includeLeaf
179
    ) : int {
180 4
        return count($this->RecallDaftNestedObjectPathToObject($leaf, $includeLeaf));
181
    }
182
183 4
    public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array
184
    {
185 4
        $object = $this->RecallDaftObject($id);
186
187
        return
188 4
            ($object instanceof DaftNestedObject)
189 4
                ? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf)
190 4
                : [];
191
    }
192
193
    /*
194
    * @param mixed $id
195
    */
196 4
    public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int
197
    {
198 4
        return count($this->RecallDaftNestedObjectPathToId($id, $includeLeaf));
199
    }
200
201 10
    public function CompareObjects(DaftNestedObject $a, DaftNestedObject $b) : int
202
    {
203 10
        return $a->GetIntNestedSortOrder() <=> $b->GetIntNestedSortOrder();
204
    }
205
206 6
    protected function FilterLeaf(
207
        bool $includeRoot,
208
        int $left,
209
        int $right,
210
        DaftNestedObject $e
211
    ) : bool {
212 6
        if ($includeRoot) {
213 4
            return $e->GetIntNestedLeft() >= $left && $e->GetIntNestedRight() <= $right;
214
        }
215
216 6
        return $e->GetIntNestedLeft() > $left && $e->GetIntNestedRight() < $right;
217
    }
218
219 18
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
220
    {
221 18
        static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__);
222
223 18
        parent::RememberDaftObjectData($object);
224 18
    }
225
226
    /**
227
    * @param DaftObject|string $object
228
    */
229 56
    protected static function ThrowIfNotType(
230
        $object,
231
        string $type,
232
        int $argument,
233
        string $function
234
    ) : void {
235 56
        parent::ThrowIfNotType($object, $type, $argument, $function);
236
237 56
        if ( ! is_a($object, DaftNestedObject::class, is_string($object))) {
238 2
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
239 2
                $argument,
240 2
                static::class,
241 2
                $function,
242 2
                DaftNestedObject::class,
243 2
                is_string($object) ? $object : get_class($object)
244
            );
245
        }
246 54
    }
247
}
248