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
|
6 |
|
public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string[] $props |
19
|
|
|
*/ |
20
|
6 |
|
$props = $this->type::DaftObjectIdProperties(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var DaftNestedObject[] $out |
24
|
|
|
*/ |
25
|
6 |
|
$out = array_map( |
26
|
|
|
function (array $id) : DaftNestedObject { |
27
|
6 |
|
$out = $this->RecallDaftObject($id); |
28
|
|
|
|
29
|
6 |
|
if ( ! ($out instanceof DaftNestedObject)) { |
30
|
|
|
throw new RuntimeException('Could not retrieve leaf from tree!'); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
return $out; |
34
|
6 |
|
}, |
35
|
6 |
|
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
|
6 |
|
$out = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string $prop |
47
|
|
|
*/ |
48
|
6 |
|
foreach ($props as $prop) { |
49
|
6 |
|
$out[$prop] = $row[$prop] ?? null; |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
return $out; |
53
|
6 |
|
}, |
54
|
6 |
|
(array) $this->data |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
usort($out, function (DaftNestedObject $a, DaftNestedObject $b) : int { |
59
|
6 |
|
return $a->GetIntNestedLeft() <=> $b->GetIntNestedLeft(); |
60
|
6 |
|
}); |
61
|
|
|
|
62
|
6 |
|
if (is_int($relativeDepthLimit)) { |
63
|
6 |
|
$out = array_filter( |
64
|
6 |
|
$out, |
65
|
|
|
function (DaftNestedObject $e) use ($relativeDepthLimit) : bool { |
66
|
6 |
|
return $e->GetIntNestedLevel() <= $relativeDepthLimit; |
67
|
6 |
|
} |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
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
|
6 |
|
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
|
4 |
|
public function RecallDaftNestedObjectTreeWithId( |
122
|
|
|
$id, |
123
|
|
|
bool $includeRoot, |
124
|
|
|
? int $relativeDepthLimit |
125
|
|
|
) : array { |
126
|
4 |
|
$object = $this->RecallDaftObject($id); |
127
|
|
|
|
128
|
|
|
return |
129
|
4 |
|
($object instanceof DaftNestedObject) |
130
|
4 |
|
? $this->RecallDaftNestedObjectTreeWithObject( |
131
|
4 |
|
$object, |
132
|
4 |
|
$includeRoot, |
133
|
4 |
|
$relativeDepthLimit |
134
|
|
|
) |
135
|
4 |
|
: []; |
136
|
|
|
} |
137
|
|
|
|
138
|
4 |
|
public function CountDaftNestedObjectTreeWithId( |
139
|
|
|
$id, |
140
|
|
|
bool $includeRoot, |
141
|
|
|
? int $relativeDepthLimit |
142
|
|
|
) : int { |
143
|
4 |
|
return count($this->RecallDaftNestedObjectTreeWithId( |
144
|
4 |
|
$id, |
145
|
4 |
|
$includeRoot, |
146
|
4 |
|
$relativeDepthLimit |
147
|
|
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
4 |
|
public function RecallDaftNestedObjectPathToObject( |
151
|
|
|
DaftNestedObject $leaf, |
152
|
|
|
bool $includeLeaf |
153
|
|
|
) : array { |
154
|
4 |
|
$left = $leaf->GetIntNestedLeft(); |
155
|
4 |
|
$right = $leaf->GetIntNestedRight(); |
156
|
|
|
|
157
|
4 |
|
if ( ! $includeLeaf) { |
158
|
4 |
|
--$left; |
159
|
4 |
|
++$right; |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
return array_values(array_filter( |
163
|
4 |
|
$this->RecallDaftNestedObjectFullTree(), |
164
|
|
|
function (DaftNestedObject $e) use ($left, $right) : bool { |
165
|
4 |
|
return $e->GetIntNestedLeft() <= $left && $e->GetIntNestedRight() >= $right; |
166
|
4 |
|
} |
167
|
|
|
)); |
168
|
|
|
} |
169
|
|
|
|
170
|
4 |
|
public function CountDaftNestedObjectPathToObject( |
171
|
|
|
DaftNestedObject $leaf, |
172
|
|
|
bool $includeLeaf |
173
|
|
|
) : int { |
174
|
4 |
|
return count($this->RecallDaftNestedObjectPathToObject($leaf, $includeLeaf)); |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array |
178
|
|
|
{ |
179
|
4 |
|
$object = $this->RecallDaftObject($id); |
180
|
|
|
|
181
|
|
|
return |
182
|
4 |
|
($object instanceof DaftNestedObject) |
183
|
4 |
|
? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf) |
184
|
4 |
|
: []; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/* |
188
|
|
|
* @param mixed $id |
189
|
|
|
*/ |
190
|
4 |
|
public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int |
191
|
|
|
{ |
192
|
4 |
|
return count($this->RecallDaftNestedObjectPathToId($id, $includeLeaf)); |
193
|
|
|
} |
194
|
|
|
|
195
|
10 |
|
protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void |
196
|
|
|
{ |
197
|
10 |
|
static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__); |
198
|
|
|
|
199
|
10 |
|
parent::RememberDaftObjectData($object); |
200
|
10 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param DaftObject|string $object |
204
|
|
|
*/ |
205
|
48 |
|
protected static function ThrowIfNotType( |
206
|
|
|
$object, |
207
|
|
|
string $type, |
208
|
|
|
int $argument, |
209
|
|
|
string $function |
210
|
|
|
) : void { |
211
|
48 |
|
parent::ThrowIfNotType($object, $type, $argument, $function); |
212
|
|
|
|
213
|
48 |
|
if ( ! is_a($object, DaftNestedObject::class, is_string($object))) { |
214
|
2 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
215
|
2 |
|
$argument, |
216
|
2 |
|
static::class, |
217
|
2 |
|
$function, |
218
|
2 |
|
DaftNestedObject::class, |
219
|
2 |
|
is_string($object) ? $object : get_class($object) |
220
|
|
|
); |
221
|
|
|
} |
222
|
46 |
|
} |
223
|
|
|
} |
224
|
|
|
|