1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @template T as DaftNestedObject |
13
|
|
|
* |
14
|
|
|
* @template-extends DaftObjectMemoryRepository<T> |
15
|
|
|
* |
16
|
|
|
* @template-implements DaftNestedObjectTree<T> |
17
|
|
|
*/ |
18
|
|
|
abstract class DaftObjectMemoryTree extends DaftObjectMemoryRepository implements DaftNestedObjectTree |
19
|
|
|
{ |
20
|
|
|
const DECREMENT = -1; |
21
|
|
|
|
22
|
|
|
const INCREMENT = 1; |
23
|
|
|
|
24
|
|
|
const BOOL_DEFAULT_ASSUME_DOES_NOT_EXIST = false; |
25
|
|
|
|
26
|
|
|
const INT_ARG_INDEX_FIRST = 1; |
27
|
|
|
|
28
|
|
|
const BOOL_DEFAULT_NO_MODIFY = 0; |
29
|
|
|
|
30
|
90 |
|
public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var array<int, DaftNestedObject> |
34
|
|
|
*/ |
35
|
90 |
|
$out = $this->memory; |
36
|
|
|
|
37
|
90 |
|
$outIds = []; |
38
|
|
|
|
39
|
90 |
|
foreach ($out as $obj) { |
40
|
|
|
/** |
41
|
|
|
* @var array<int, scalar|(scalar|array|object|null)[]> |
42
|
|
|
*/ |
43
|
44 |
|
$id = $obj->GetId(); |
44
|
|
|
|
45
|
44 |
|
$outIds[] = $id; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array<int, DaftNestedObject> |
50
|
|
|
* |
51
|
|
|
* @psalm-var array<int, T> |
52
|
|
|
*/ |
53
|
90 |
|
$fromMemory = array_filter( |
54
|
90 |
|
array_map([$this, 'MapDataToObject'], $this->data), |
55
|
|
|
function (DaftNestedObject $leaf) use ($outIds) : bool { |
56
|
64 |
|
return ! in_array($leaf->GetId(), $outIds, true); |
57
|
90 |
|
} |
58
|
|
|
); |
59
|
|
|
|
60
|
90 |
|
$out = array_merge($out, $fromMemory); |
61
|
|
|
|
62
|
90 |
|
usort( |
63
|
90 |
|
$out, |
64
|
|
|
/** |
65
|
|
|
* @psalm-param T $a |
66
|
|
|
* @psalm-param T $b |
67
|
|
|
*/ |
68
|
|
|
function (DaftNestedObject $a, DaftNestedObject $b) : int { |
69
|
64 |
|
return $a->GetIntNestedLeft() <=> $b->GetIntNestedLeft(); |
70
|
90 |
|
} |
71
|
|
|
); |
72
|
|
|
|
73
|
90 |
|
if (is_int($relativeDepthLimit)) { |
74
|
|
|
$filter = |
75
|
|
|
/** |
76
|
|
|
* @psalm-param T $e |
77
|
|
|
*/ |
78
|
|
|
function (DaftNestedObject $e) use ($relativeDepthLimit) : bool { |
79
|
32 |
|
return $e->GetIntNestedLevel() <= $relativeDepthLimit; |
80
|
32 |
|
}; |
81
|
32 |
|
$out = array_filter($out, $filter); |
82
|
|
|
} |
83
|
|
|
|
84
|
90 |
|
return $out; |
85
|
|
|
} |
86
|
|
|
|
87
|
90 |
|
public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int |
88
|
|
|
{ |
89
|
90 |
|
return count($this->RecallDaftNestedObjectFullTree($relativeDepthLimit)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
* |
95
|
|
|
* @psalm-param T $root |
96
|
|
|
* |
97
|
|
|
* @psalm-return array<int, T> |
98
|
|
|
*/ |
99
|
44 |
|
public function RecallDaftNestedObjectTreeWithObject( |
100
|
|
|
DaftNestedObject $root, |
101
|
|
|
bool $includeRoot, |
102
|
|
|
? int $limit |
103
|
|
|
) : array { |
104
|
44 |
|
$left = $root->GetIntNestedLeft(); |
105
|
44 |
|
$right = $root->GetIntNestedRight(); |
106
|
44 |
|
$limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null; |
107
|
|
|
|
108
|
44 |
|
$leaves = $this->RecallDaftNestedObjectFullTree(); |
109
|
|
|
|
110
|
44 |
|
if (is_int($limit)) { |
111
|
|
|
$leaves = array_filter($leaves, function (DaftNestedObject $e) use ($limit) : bool { |
112
|
38 |
|
return $e->GetIntNestedLevel() <= $limit; |
113
|
38 |
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
44 |
|
return array_values(array_filter( |
117
|
44 |
|
$leaves, |
118
|
|
|
/** |
119
|
|
|
* @psalm-param T $e |
120
|
|
|
*/ |
121
|
|
|
function (DaftNestedObject $e) use ($includeRoot, $left, $right) : bool { |
122
|
44 |
|
return $this->FilterLeaf($includeRoot, $left, $right, $e); |
123
|
44 |
|
} |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @psalm-param T $root |
129
|
|
|
*/ |
130
|
20 |
|
public function CountDaftNestedObjectTreeWithObject( |
131
|
|
|
DaftNestedObject $root, |
132
|
|
|
bool $includeRoot, |
133
|
|
|
? int $relativeDepthLimit |
134
|
|
|
) : int { |
135
|
20 |
|
return count( |
136
|
20 |
|
$this->RecallDaftNestedObjectTreeWithObject($root, $includeRoot, $relativeDepthLimit) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param scalar|(scalar|array|object|null)[] $id |
142
|
|
|
* |
143
|
|
|
* @return array<int, DaftNestedObject> |
144
|
|
|
*/ |
145
|
56 |
|
public function RecallDaftNestedObjectTreeWithId( |
146
|
|
|
$id, |
147
|
|
|
bool $includeRoot, |
148
|
|
|
? int $relativeDepthLimit |
149
|
|
|
) : array { |
150
|
|
|
/** |
151
|
|
|
* @psalm-var T|null |
152
|
|
|
*/ |
153
|
56 |
|
$object = $this->RecallDaftObject($id); |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @var array<int, DaftNestedObject> |
157
|
|
|
* |
158
|
|
|
* @psalm-var array<int, T> |
159
|
|
|
*/ |
160
|
|
|
$out = |
161
|
56 |
|
($object instanceof DaftNestedObject) |
162
|
28 |
|
? $this->RecallDaftNestedObjectTreeWithObject( |
163
|
28 |
|
$object, |
164
|
28 |
|
$includeRoot, |
165
|
28 |
|
$relativeDepthLimit |
166
|
|
|
) |
167
|
|
|
: ( |
168
|
32 |
|
((array) $id === (array) $this->GetNestedObjectTreeRootId()) |
169
|
32 |
|
? $this->RecallDaftNestedObjectFullTree($relativeDepthLimit) |
170
|
56 |
|
: [] |
171
|
|
|
); |
172
|
|
|
|
173
|
56 |
|
return $out; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param scalar|(scalar|array|object|null)[] $id |
178
|
|
|
*/ |
179
|
4 |
|
public function CountDaftNestedObjectTreeWithId( |
180
|
|
|
$id, |
181
|
|
|
bool $includeRoot, |
182
|
|
|
? int $relativeDepthLimit |
183
|
|
|
) : int { |
184
|
4 |
|
return count($this->RecallDaftNestedObjectTreeWithId( |
185
|
4 |
|
$id, |
186
|
4 |
|
$includeRoot, |
187
|
4 |
|
$relativeDepthLimit |
188
|
|
|
)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @psalm-param T $leaf |
193
|
|
|
* |
194
|
|
|
* @return array<int, DaftNestedObject> |
195
|
|
|
* |
196
|
|
|
* @psalm-return array<int, T> |
197
|
|
|
*/ |
198
|
4 |
|
public function RecallDaftNestedObjectPathToObject( |
199
|
|
|
DaftNestedObject $leaf, |
200
|
|
|
bool $includeLeaf |
201
|
|
|
) : array { |
202
|
|
|
$left = |
203
|
4 |
|
$leaf->GetIntNestedLeft() + |
204
|
4 |
|
($includeLeaf ? self::BOOL_DEFAULT_NO_MODIFY : self::DECREMENT); |
205
|
|
|
$right = |
206
|
4 |
|
$leaf->GetIntNestedRight() + |
207
|
4 |
|
($includeLeaf ? self::BOOL_DEFAULT_NO_MODIFY : self::INCREMENT); |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @var array<int, DaftNestedObject> |
211
|
|
|
* |
212
|
|
|
* @psalm-var array<int, T> |
213
|
|
|
*/ |
214
|
4 |
|
$out = array_values(array_filter( |
215
|
4 |
|
$this->RecallDaftNestedObjectFullTree(), |
216
|
|
|
function (DaftNestedObject $e) use ($left, $right) : bool { |
217
|
4 |
|
return $e->GetIntNestedLeft() <= $left && $e->GetIntNestedRight() >= $right; |
218
|
4 |
|
} |
219
|
|
|
)); |
220
|
|
|
|
221
|
4 |
|
return $out; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @psalm-param T $leaf |
226
|
|
|
*/ |
227
|
4 |
|
public function CountDaftNestedObjectPathToObject( |
228
|
|
|
DaftNestedObject $leaf, |
229
|
|
|
bool $includeLeaf |
230
|
|
|
) : int { |
231
|
4 |
|
return count($this->RecallDaftNestedObjectPathToObject($leaf, $includeLeaf)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param scalar|(scalar|array|object|null)[] $id |
236
|
|
|
* |
237
|
|
|
* @return array<int, DaftNestedObject> |
238
|
|
|
* |
239
|
|
|
* @psalm-return array<int, T> |
240
|
|
|
*/ |
241
|
4 |
|
public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array |
242
|
|
|
{ |
243
|
|
|
/** |
244
|
|
|
* @var DaftNestedObject|null |
245
|
|
|
* |
246
|
|
|
* @psalm-var T|null |
247
|
|
|
*/ |
248
|
4 |
|
$object = $this->RecallDaftObject($id); |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @var array<int, DaftNestedObject> |
252
|
|
|
* |
253
|
|
|
* @psalm-var array<int, T> |
254
|
|
|
*/ |
255
|
|
|
$out = |
256
|
4 |
|
($object instanceof DaftNestedObject) |
257
|
4 |
|
? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf) |
258
|
4 |
|
: []; |
259
|
|
|
|
260
|
4 |
|
return $out; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param scalar|(scalar|array|object|null)[] $id |
265
|
|
|
*/ |
266
|
4 |
|
public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int |
267
|
|
|
{ |
268
|
4 |
|
return count($this->RecallDaftNestedObjectPathToId($id, $includeLeaf)); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @psalm-param T $object |
273
|
|
|
*/ |
274
|
94 |
|
public function RememberDaftObject(SuitableForRepositoryType $object) : void |
275
|
|
|
{ |
276
|
94 |
|
parent::RememberDaftObject($object); |
277
|
94 |
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* {@inheritdoc} |
281
|
|
|
* |
282
|
|
|
* @psalm-return T|null |
283
|
|
|
*/ |
284
|
68 |
|
public function RecallDaftObject($id) : ? SuitableForRepositoryType |
285
|
|
|
{ |
286
|
68 |
|
return parent::RecallDaftObject($id); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @psalm-return T |
291
|
|
|
*/ |
292
|
64 |
|
private function MapDataToObject(array $row) : DaftNestedObject |
293
|
|
|
{ |
294
|
|
|
/** |
295
|
|
|
* @psalm-var class-string<T> |
296
|
|
|
*/ |
297
|
64 |
|
$type = $this->type; |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @var DaftNestedObject |
301
|
|
|
* |
302
|
|
|
* @psalm-var T |
303
|
|
|
*/ |
304
|
64 |
|
$out = new $type($row); |
305
|
|
|
|
306
|
64 |
|
return $out; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @psalm-param T $e |
311
|
|
|
*/ |
312
|
44 |
|
private function FilterLeaf( |
313
|
|
|
bool $includeRoot, |
314
|
|
|
int $left, |
315
|
|
|
int $right, |
316
|
|
|
DaftNestedObject $e |
317
|
|
|
) : bool { |
318
|
44 |
|
if ($includeRoot) { |
319
|
4 |
|
return $e->GetIntNestedLeft() >= $left && $e->GetIntNestedRight() <= $right; |
320
|
|
|
} |
321
|
|
|
|
322
|
44 |
|
return $e->GetIntNestedLeft() > $left && $e->GetIntNestedRight() < $right; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|