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 BadMethodCallException; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use RuntimeException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @template T as DaftNestedWriteableObject |
17
|
|
|
* |
18
|
|
|
* @template-extends DaftObjectMemoryTree<T> |
19
|
|
|
* |
20
|
|
|
* @template-implements DaftNestedWriteableObjectTree<T> |
21
|
|
|
*/ |
22
|
|
|
abstract class DaftWriteableObjectMemoryTree extends DaftObjectMemoryTree implements DaftNestedWriteableObjectTree |
23
|
|
|
{ |
24
|
|
|
const DEFINITELY_BELOW = false; |
25
|
|
|
|
26
|
|
|
const EXCLUDE_ROOT = false; |
27
|
|
|
|
28
|
|
|
const INSERT_AFTER = false; |
29
|
|
|
|
30
|
|
|
const LIMIT_ONE = 1; |
31
|
|
|
|
32
|
|
|
const RELATIVE_DEPTH_SAME = 0; |
33
|
|
|
|
34
|
|
|
const INT_ARG_INDEX_SECOND = 2; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* |
39
|
|
|
* @psalm-return T |
40
|
|
|
*/ |
41
|
62 |
|
public function RecallDaftNestedWriteableObjectOrThrow($id) : DaftNestedWriteableObject |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var DaftNestedWriteableObject|null |
45
|
|
|
* |
46
|
|
|
* @psalm-var T|null |
47
|
|
|
*/ |
48
|
62 |
|
$out = $this->RecallDaftNestedObjectOrThrow($id); |
49
|
|
|
|
50
|
60 |
|
if (is_null($out)) { |
51
|
|
|
throw new DaftObjectNotRecalledException( |
52
|
|
|
'Argument 1 passed to ' . |
53
|
|
|
DaftNestedWriteableObjectTree::class . |
54
|
|
|
'::RecallDaftNestedWriteableObjectOrThrow() did not resolve to an instance of ' . |
55
|
|
|
DaftNestedWriteableObject::class . |
56
|
|
|
' from ' . |
57
|
|
|
static::class . |
58
|
|
|
'::RecallDaftObject()' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
60 |
|
return $out; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @psalm-param T $newLeaf |
67
|
|
|
* @psalm-param T $referenceLeaf |
68
|
|
|
* |
69
|
|
|
* @psalm-return T |
70
|
|
|
*/ |
71
|
84 |
|
public function ModifyDaftNestedObjectTreeInsert( |
72
|
|
|
DaftNestedWriteableObject $newLeaf, |
73
|
|
|
DaftNestedWriteableObject $referenceLeaf, |
74
|
|
|
bool $before = self::INSERT_AFTER, |
75
|
|
|
bool $above = null |
76
|
|
|
) : DaftNestedWriteableObject { |
77
|
84 |
|
if ($newLeaf->GetId() === $referenceLeaf->GetId()) { |
78
|
40 |
|
throw new InvalidArgumentException('Cannot modify leaf relative to itself!'); |
79
|
|
|
} |
80
|
|
|
|
81
|
60 |
|
if ((bool) $above) { |
82
|
48 |
|
$this->ModifyDaftNestedObjectTreeInsertAbove($newLeaf, $referenceLeaf); |
83
|
32 |
|
} elseif (self::DEFINITELY_BELOW === $above) { |
84
|
6 |
|
$this->ModifyDaftNestedObjectTreeInsertBelow($newLeaf, $referenceLeaf); |
85
|
|
|
} else { |
86
|
28 |
|
$this->ModifyDaftNestedObjectTreeInsertAdjacent($newLeaf, $referenceLeaf, $before); |
87
|
|
|
} |
88
|
|
|
|
89
|
60 |
|
return $this->RebuildAfterInsert($newLeaf); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param DaftNestedWriteableObject|scalar|(scalar|array|object|null)[] $leaf |
94
|
|
|
* @param DaftNestedWriteableObject|scalar|(scalar|array|object|null)[] $referenceId |
95
|
|
|
* |
96
|
|
|
* @psalm-param T|scalar|(scalar|array|object|null)[] $leaf |
97
|
|
|
* @psalm-param T|scalar|(scalar|array|object|null)[] $referenceId |
98
|
|
|
* |
99
|
|
|
* @psalm-return T |
100
|
|
|
*/ |
101
|
48 |
|
public function ModifyDaftNestedObjectTreeInsertLoose( |
102
|
|
|
$leaf, |
103
|
|
|
$referenceId, |
104
|
|
|
bool $before = self::INSERT_AFTER, |
105
|
|
|
bool $above = null |
106
|
|
|
) : DaftNestedWriteableObject { |
107
|
48 |
|
$leaf = $this->MaybeGetLeaf($leaf); |
108
|
|
|
|
109
|
24 |
|
$reference = $referenceId; |
110
|
|
|
|
111
|
24 |
|
if ( ! ($referenceId instanceof DaftNestedWriteableObject)) { |
112
|
|
|
/** |
113
|
|
|
* @var scalar|(scalar|array|object|null)[] |
114
|
|
|
*/ |
115
|
24 |
|
$referenceId = $referenceId; |
116
|
|
|
|
117
|
24 |
|
$reference = $this->RecallDaftObject($referenceId); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var DaftNestedWriteableObject|null |
122
|
|
|
* |
123
|
|
|
* @psalm-var T|null |
124
|
|
|
*/ |
125
|
24 |
|
$reference = $reference; |
126
|
|
|
|
127
|
24 |
|
$resp = $this->ModifyDaftNestedObjectTreeInsertMaybeLooseIntoTree( |
128
|
24 |
|
$this, |
129
|
24 |
|
$leaf, |
130
|
24 |
|
$reference, |
131
|
24 |
|
$referenceId === $this->GetNestedObjectTreeRootId(), |
132
|
24 |
|
$before, |
133
|
24 |
|
$above |
134
|
|
|
); |
135
|
|
|
|
136
|
24 |
|
if ($resp instanceof DaftNestedWriteableObject) { |
137
|
24 |
|
return $resp; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
throw new InvalidArgumentException(sprintf( |
141
|
|
|
'Argument %u passed to %s() did not resolve to a leaf node!', |
142
|
|
|
is_null($leaf) ? self::INT_ARG_INDEX_FIRST : self::INT_ARG_INDEX_SECOND, |
143
|
|
|
__METHOD__ |
144
|
|
|
)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @psalm-param T $root |
149
|
|
|
* @psalm-param T|null $replacementRoot |
150
|
|
|
*/ |
151
|
8 |
|
public function ModifyDaftNestedObjectTreeRemoveWithObject( |
152
|
|
|
DaftNestedWriteableObject $root, |
153
|
|
|
? DaftNestedWriteableObject $replacementRoot |
154
|
|
|
) : int { |
155
|
|
|
if ( |
156
|
8 |
|
$this->CountDaftNestedObjectTreeWithObject( |
157
|
8 |
|
$root, |
158
|
8 |
|
false, |
159
|
8 |
|
null |
160
|
8 |
|
) > AbstractArrayBackedDaftNestedObject::COUNT_EXPECT_NON_EMPTY && |
161
|
8 |
|
is_null($replacementRoot) |
162
|
|
|
) { |
163
|
2 |
|
throw new BadMethodCallException('Cannot leave orphan objects in a tree'); |
164
|
|
|
} |
165
|
|
|
|
166
|
6 |
|
$root = $this->StoreThenRetrieveFreshLeaf($root); |
167
|
|
|
|
168
|
6 |
|
if ( ! is_null($replacementRoot)) { |
169
|
4 |
|
$this->ModifyDaftNestedObjectTreeRemoveWithObjectPrepareRemovalAndRebuild( |
170
|
4 |
|
$root, |
171
|
4 |
|
$replacementRoot |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
6 |
|
$this->RemoveDaftObject($root); |
176
|
|
|
|
177
|
6 |
|
$this->RebuildTreeInefficiently(); |
178
|
|
|
|
179
|
6 |
|
return $this->CountDaftNestedObjectFullTree(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param scalar|(scalar|array|object|null)[] $root |
184
|
|
|
* @param scalar|(scalar|array|object|null)[]|null $replacementRoot |
185
|
|
|
*/ |
186
|
12 |
|
public function ModifyDaftNestedObjectTreeRemoveWithId($root, $replacementRoot) : int |
187
|
|
|
{ |
188
|
12 |
|
$rootObject = $this->RecallDaftObject($root); |
189
|
|
|
|
190
|
12 |
|
$resp = null; |
191
|
|
|
|
192
|
12 |
|
if ($rootObject instanceof DaftNestedWriteableObject) { |
193
|
12 |
|
$resp = $this->ModifyDaftNestedObjectTreeRemoveWithIdUsingRootObject( |
194
|
12 |
|
$replacementRoot, |
195
|
12 |
|
$rootObject |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
8 |
|
return is_int($resp) ? $resp : $this->CountDaftNestedObjectFullTree(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @psalm-param T $leaf |
204
|
|
|
* |
205
|
|
|
* @psalm-return T |
206
|
|
|
*/ |
207
|
62 |
|
public function StoreThenRetrieveFreshLeaf( |
208
|
|
|
DaftNestedWriteableObject $leaf |
209
|
|
|
) : DaftNestedWriteableObject { |
210
|
62 |
|
$this->RememberDaftObject($leaf); |
211
|
62 |
|
$this->ForgetDaftObject($leaf); |
212
|
62 |
|
$this->ForgetDaftObjectById($leaf->GetId()); |
213
|
|
|
|
214
|
62 |
|
return $this->RecallDaftNestedWriteableObjectOrThrow($leaf->GetId()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @psalm-param T $object |
219
|
|
|
*/ |
220
|
88 |
|
public function RememberDaftObject(SuitableForRepositoryType $object) : void |
221
|
|
|
{ |
222
|
|
|
/** |
223
|
|
|
* @var DaftNestedWriteableObject |
224
|
|
|
* |
225
|
|
|
* @psalm-var T |
226
|
|
|
*/ |
227
|
88 |
|
$object = $object; |
228
|
|
|
|
229
|
88 |
|
$left = $object->GetIntNestedLeft(); |
|
|
|
|
230
|
88 |
|
$right = $object->GetIntNestedRight(); |
|
|
|
|
231
|
88 |
|
$level = $object->GetIntNestedLevel(); |
|
|
|
|
232
|
|
|
|
233
|
88 |
|
if (0 === $left && 0 === $right && 0 === $level) { |
234
|
86 |
|
$fullTreeCount = $this->CountDaftNestedObjectFullTree(); |
235
|
|
|
|
236
|
86 |
|
if ($fullTreeCount > AbstractArrayBackedDaftNestedObject::COUNT_EXPECT_NON_EMPTY) { |
237
|
60 |
|
$tree = $this->RecallDaftNestedObjectFullTree(); |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @var DaftNestedWriteableObject |
241
|
|
|
* |
242
|
|
|
* @psalm-var T |
243
|
|
|
*/ |
244
|
60 |
|
$end = end($tree); |
245
|
|
|
|
246
|
60 |
|
$left = $end->GetIntNestedRight() + 1; |
247
|
|
|
} else { |
248
|
86 |
|
$left = $fullTreeCount + $fullTreeCount; |
249
|
|
|
} |
250
|
|
|
|
251
|
86 |
|
$object->SetIntNestedLeft($left); |
|
|
|
|
252
|
86 |
|
$object->SetIntNestedRight($left + 1); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
88 |
|
parent::RememberDaftObject($object); |
256
|
88 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @psalm-param T $newLeaf |
260
|
|
|
* @psalm-param T $referenceLeaf |
261
|
|
|
*/ |
262
|
52 |
|
protected function ModifyDaftNestedObjectTreeInsertAdjacent( |
263
|
|
|
DaftNestedWriteableObject $newLeaf, |
264
|
|
|
DaftNestedWriteableObject $referenceLeaf, |
265
|
|
|
bool $before |
266
|
|
|
) : void { |
267
|
|
|
/** |
268
|
|
|
* @var array<int, DaftNestedWriteableObject> |
269
|
|
|
* |
270
|
|
|
* @psalm-var array<int, T> |
271
|
|
|
*/ |
272
|
52 |
|
$siblings = $this->SiblingsExceptLeaf($newLeaf, $referenceLeaf); |
273
|
|
|
|
274
|
52 |
|
$siblingIds = []; |
275
|
52 |
|
$siblingSort = []; |
276
|
52 |
|
$j = count($siblings); |
277
|
|
|
|
278
|
52 |
|
foreach ($siblings as $leaf) { |
279
|
|
|
/** |
280
|
|
|
* @var scalar|(scalar|array|object|null)[] |
281
|
|
|
*/ |
282
|
28 |
|
$siblingId = $leaf->GetId(); |
283
|
28 |
|
$siblingIds[] = $siblingId; |
284
|
28 |
|
$siblingSort[] = $leaf->GetIntNestedSortOrder(); |
285
|
|
|
} |
286
|
|
|
|
287
|
52 |
|
$pos = array_search($referenceLeaf->GetId(), $siblingIds, true); |
288
|
|
|
|
289
|
52 |
|
if (false === $pos) { |
290
|
24 |
|
throw new RuntimeException('Reference leaf not found in siblings tree!'); |
291
|
|
|
} |
292
|
|
|
|
293
|
28 |
|
for ($i = 0; $i < $j; ++$i) { |
294
|
28 |
|
$siblings[$i]->SetIntNestedSortOrder( |
295
|
28 |
|
$siblingSort[$i] + |
296
|
28 |
|
(($before ? ($i < $pos) : ($i <= $pos)) ? self::DECREMENT : self::INCREMENT) |
297
|
|
|
); |
298
|
28 |
|
$this->StoreThenRetrieveFreshLeaf($siblings[$i]); |
299
|
|
|
} |
300
|
|
|
|
301
|
28 |
|
$newLeaf->SetIntNestedSortOrder($siblingSort[$pos]); |
302
|
28 |
|
$newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->ObtainDaftNestedObjectParentId()); |
303
|
|
|
|
304
|
28 |
|
$this->StoreThenRetrieveFreshLeaf($newLeaf); |
305
|
28 |
|
} |
306
|
|
|
|
307
|
60 |
|
protected function RebuildTreeInefficiently() : void |
308
|
|
|
{ |
309
|
60 |
|
$rebuilder = new InefficientDaftNestedRebuild($this); |
310
|
60 |
|
$rebuilder->RebuildTree(); |
311
|
60 |
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @psalm-param T|null $leaf |
315
|
|
|
* @psalm-param T|null $reference |
316
|
|
|
* |
317
|
|
|
* @psalm-return T|null |
318
|
|
|
*/ |
319
|
24 |
|
private function ModifyDaftNestedObjectTreeInsertMaybeLooseIntoTree( |
320
|
|
|
DaftNestedWriteableObjectTree $tree, |
321
|
|
|
? DaftNestedWriteableObject $leaf, |
322
|
|
|
? DaftObject $reference, |
323
|
|
|
bool $isRoot, |
324
|
|
|
bool $before, |
325
|
|
|
? bool $above |
326
|
|
|
) : ? DaftNestedWriteableObject { |
327
|
24 |
|
if ( ! is_null($leaf) && (($reference instanceof DaftNestedWriteableObject) || $isRoot)) { |
328
|
24 |
|
if ($reference instanceof DaftNestedWriteableObject) { |
329
|
18 |
|
return $tree->ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above); |
330
|
|
|
} |
331
|
|
|
|
332
|
24 |
|
return $this->ModifyDaftNestedObjectTreeInsertLooseIntoTree($leaf, $before, $above); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return null; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @psalm-param T $newLeaf |
340
|
|
|
* |
341
|
|
|
* @psalm-return T |
342
|
|
|
*/ |
343
|
60 |
|
private function RebuildAfterInsert( |
344
|
|
|
DaftNestedWriteableObject $newLeaf |
345
|
|
|
) : DaftNestedWriteableObject { |
346
|
60 |
|
$this->RebuildTreeInefficiently(); |
347
|
|
|
|
348
|
60 |
|
return $this->RecallDaftNestedWriteableObjectOrThrow($newLeaf->GetId()); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @psalm-param T $root |
353
|
|
|
* @psalm-param T $replacementRoot |
354
|
|
|
*/ |
355
|
4 |
|
private function ModifyDaftNestedObjectTreeRemoveWithObjectPrepareRemovalAndRebuild( |
356
|
|
|
DaftNestedWriteableObject $root, |
357
|
|
|
DaftNestedWriteableObject $replacementRoot |
358
|
|
|
) : void { |
359
|
|
|
/** |
360
|
|
|
* @var scalar|(scalar|array|object|null)[] |
361
|
|
|
*/ |
362
|
4 |
|
$replacementRootId = $this->StoreThenRetrieveFreshLeaf($replacementRoot)->GetId(); |
363
|
|
|
|
364
|
4 |
|
$this->UpdateRoots($root, $replacementRootId); |
365
|
4 |
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param scalar|(scalar|array|object|null)[] $replacementRootId |
369
|
|
|
* |
370
|
|
|
* @psalm-param T $root |
371
|
|
|
*/ |
372
|
10 |
|
private function UpdateRoots(DaftNestedWriteableObject $root, $replacementRootId) : void |
373
|
|
|
{ |
374
|
|
|
/** |
375
|
|
|
* @var array<int, DaftNestedObject> |
376
|
|
|
*/ |
377
|
10 |
|
$alterThese = $this->RecallDaftNestedObjectTreeWithObject($root, false, self::LIMIT_ONE); |
378
|
|
|
|
379
|
10 |
|
foreach ($alterThese as $alter) { |
380
|
4 |
|
if ($alter instanceof DaftNestedWriteableObject) { |
381
|
4 |
|
$alter->AlterDaftNestedObjectParentId($replacementRootId); |
382
|
4 |
|
$this->RememberDaftObject($alter); |
383
|
|
|
} |
384
|
|
|
} |
385
|
10 |
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param DaftNestedWriteableObject|scalar|(scalar|array|object|null)[] $leaf |
389
|
|
|
* |
390
|
|
|
* @psalm-param T|scalar|(scalar|array|object|null)[] $leaf |
391
|
|
|
*/ |
392
|
48 |
|
private function MaybeGetLeaf($leaf) : ? DaftNestedWriteableObject |
393
|
|
|
{ |
394
|
48 |
|
if ($leaf === $this->GetNestedObjectTreeRootId()) { |
395
|
24 |
|
throw new InvalidArgumentException('Cannot pass root id as new leaf'); |
396
|
24 |
|
} elseif ($leaf instanceof DaftNestedWriteableObject) { |
397
|
24 |
|
return $this->StoreThenRetrieveFreshLeaf($leaf); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @psalm-var scalar|(scalar|array|object|null)[] |
402
|
|
|
*/ |
403
|
16 |
|
$leaf = $leaf; |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @var DaftNestedWriteableObject|null |
407
|
|
|
* |
408
|
|
|
* @psalm-var T|null |
409
|
|
|
*/ |
410
|
16 |
|
$out = $this->RecallDaftObject($leaf); |
411
|
|
|
|
412
|
16 |
|
return $out; |
|
|
|
|
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @psalm-param T $leaf |
417
|
|
|
* |
418
|
|
|
* @psalm-return T |
419
|
|
|
*/ |
420
|
24 |
|
private function ModifyDaftNestedObjectTreeInsertLooseIntoTree( |
421
|
|
|
DaftNestedWriteableObject $leaf, |
422
|
|
|
bool $before, |
423
|
|
|
? bool $above |
424
|
|
|
) : DaftNestedWriteableObject { |
425
|
|
|
/** |
426
|
|
|
* @var array<int, DaftNestedWriteableObject> |
427
|
|
|
* |
428
|
|
|
* @psalm-var array<int, T> |
429
|
|
|
*/ |
430
|
24 |
|
$leaves = $this->RecallDaftNestedObjectFullTree(self::RELATIVE_DEPTH_SAME); |
431
|
24 |
|
$leaves = array_filter( |
432
|
24 |
|
$leaves, |
433
|
|
|
/** |
434
|
|
|
* @psalm-param T $e |
435
|
|
|
*/ |
436
|
|
|
function (DaftNestedWriteableObject $e) use ($leaf) : bool { |
437
|
24 |
|
return $e->GetId() !== $leaf->GetId(); |
438
|
24 |
|
} |
439
|
|
|
); |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @var false|DaftNestedWriteableObject |
443
|
|
|
* |
444
|
|
|
* @psalm-var false|T |
445
|
|
|
*/ |
446
|
24 |
|
$reference = $before ? current($leaves) : end($leaves); |
447
|
|
|
|
448
|
24 |
|
if ( ! ($reference instanceof DaftNestedWriteableObject)) { |
449
|
24 |
|
$leaf->SetIntNestedLeft(0); |
450
|
24 |
|
$leaf->SetIntNestedRight(1); |
451
|
24 |
|
$leaf->SetIntNestedLevel(0); |
452
|
24 |
|
$leaf->AlterDaftNestedObjectParentId($this->GetNestedObjectTreeRootId()); |
453
|
|
|
|
454
|
24 |
|
return $this->StoreThenRetrieveFreshLeaf($leaf); |
455
|
|
|
} |
456
|
|
|
|
457
|
24 |
|
return $this->ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @psalm-param T $rootObject |
462
|
|
|
* @psalm-param T|null $replacementRootObject |
463
|
|
|
*/ |
464
|
4 |
|
private function MaybeRemoveWithPossibleObject( |
465
|
|
|
DaftNestedWriteableObject $rootObject, |
466
|
|
|
? DaftObject $replacementRootObject |
467
|
|
|
) : int { |
468
|
4 |
|
if ( ! ($replacementRootObject instanceof DaftNestedWriteableObject)) { |
469
|
2 |
|
throw new InvalidArgumentException( |
470
|
2 |
|
'Could not locate replacement root, cannot leave orphan objects!' |
471
|
|
|
); |
472
|
|
|
} |
473
|
|
|
|
474
|
2 |
|
return $this->ModifyDaftNestedObjectTreeRemoveWithObject( |
475
|
2 |
|
$rootObject, |
476
|
2 |
|
$replacementRootObject |
477
|
|
|
); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @param scalar|(scalar|array|object|null)[] $replacementRoot |
482
|
|
|
* |
483
|
|
|
* @psalm-param T $rootObject |
484
|
|
|
*/ |
485
|
6 |
|
private function UpdateRemoveThenRebuild( |
486
|
|
|
DaftNestedWriteableObject $rootObject, |
487
|
|
|
$replacementRoot |
488
|
|
|
) : void { |
489
|
6 |
|
$this->UpdateRoots($rootObject, $replacementRoot); |
490
|
|
|
|
491
|
6 |
|
$this->RemoveDaftObject($rootObject); |
492
|
|
|
|
493
|
6 |
|
$this->RebuildTreeInefficiently(); |
494
|
6 |
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* @psalm-param T $newLeaf |
498
|
|
|
* @psalm-param T $referenceLeaf |
499
|
|
|
*/ |
500
|
48 |
|
private function ModifyDaftNestedObjectTreeInsertAbove( |
501
|
|
|
DaftNestedWriteableObject $newLeaf, |
502
|
|
|
DaftNestedWriteableObject $referenceLeaf |
503
|
|
|
) : void { |
504
|
48 |
|
$newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->ObtainDaftNestedObjectParentId()); |
505
|
48 |
|
$referenceLeaf->AlterDaftNestedObjectParentId($newLeaf->GetId()); |
506
|
|
|
|
507
|
48 |
|
$this->StoreThenRetrieveFreshLeaf($newLeaf); |
508
|
48 |
|
$this->StoreThenRetrieveFreshLeaf($referenceLeaf); |
509
|
48 |
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* @psalm-param T $newLeaf |
513
|
|
|
* @psalm-param T $referenceLeaf |
514
|
|
|
*/ |
515
|
6 |
|
private function ModifyDaftNestedObjectTreeInsertBelow( |
516
|
|
|
DaftNestedWriteableObject $newLeaf, |
517
|
|
|
DaftNestedWriteableObject $referenceLeaf |
518
|
|
|
) : void { |
519
|
6 |
|
$newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->GetId()); |
520
|
6 |
|
$this->StoreThenRetrieveFreshLeaf($newLeaf); |
521
|
6 |
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* @psalm-param T $newLeaf |
525
|
|
|
* @psalm-param T $referenceLeaf |
526
|
|
|
* |
527
|
|
|
* @psalm-return array<int, T> |
528
|
|
|
*/ |
529
|
52 |
|
private function SiblingsExceptLeaf( |
530
|
|
|
DaftNestedWriteableObject $newLeaf, |
531
|
|
|
DaftNestedWriteableObject $referenceLeaf |
532
|
|
|
) : array { |
533
|
|
|
/** |
534
|
|
|
* @var array<int, DaftNestedWriteableObject> |
535
|
|
|
* |
536
|
|
|
* @psalm-var array<int, T> |
537
|
|
|
*/ |
538
|
52 |
|
$siblings = $this->RecallDaftNestedObjectTreeWithId( |
539
|
52 |
|
$referenceLeaf->ObtainDaftNestedObjectParentId(), |
540
|
52 |
|
self::EXCLUDE_ROOT, |
541
|
52 |
|
self::RELATIVE_DEPTH_SAME |
542
|
|
|
); |
543
|
|
|
|
544
|
52 |
|
$siblings = array_values(array_filter( |
545
|
52 |
|
$siblings, |
546
|
|
|
/** |
547
|
|
|
* @psalm-param T $leaf |
548
|
|
|
*/ |
549
|
|
|
function (DaftNestedWriteableObject $leaf) use ($newLeaf) : bool { |
550
|
28 |
|
return $leaf->GetId() !== $newLeaf->GetId(); |
551
|
52 |
|
} |
552
|
|
|
)); |
553
|
|
|
|
554
|
52 |
|
return $siblings; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* @param scalar|(scalar|array|object|null)[]|null $replacementRoot |
559
|
|
|
* |
560
|
|
|
* @psalm-param T $rootObject |
561
|
|
|
*/ |
562
|
12 |
|
private function ModifyDaftNestedObjectTreeRemoveWithIdUsingRootObject( |
563
|
|
|
$replacementRoot, |
564
|
|
|
DaftNestedWriteableObject $rootObject |
565
|
|
|
) : ? int { |
566
|
|
|
if ( |
567
|
12 |
|
$this->CountDaftNestedObjectTreeWithObject( |
568
|
12 |
|
$rootObject, |
569
|
12 |
|
false, |
570
|
12 |
|
null |
571
|
12 |
|
) > AbstractArrayBackedDaftNestedObject::COUNT_EXPECT_NON_EMPTY && |
572
|
12 |
|
is_null($replacementRoot) |
573
|
|
|
) { |
574
|
2 |
|
throw new BadMethodCallException('Cannot leave orphan objects in a tree'); |
575
|
|
|
} elseif ( |
576
|
10 |
|
! is_null($replacementRoot) && |
577
|
10 |
|
$replacementRoot !== $this->GetNestedObjectTreeRootId() |
578
|
|
|
) { |
579
|
4 |
|
$replacementRoot = $this->RecallDaftObject($replacementRoot); |
580
|
|
|
|
581
|
4 |
|
return $this->MaybeRemoveWithPossibleObject($rootObject, $replacementRoot); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* @var scalar|(scalar|array|object|null)[] |
586
|
|
|
*/ |
587
|
6 |
|
$replacementRoot = $replacementRoot; |
588
|
|
|
|
589
|
6 |
|
$this->UpdateRemoveThenRebuild($rootObject, $replacementRoot); |
590
|
|
|
|
591
|
6 |
|
return null; |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
|