|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft nested 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
|
|
|
trait TraitWriteableTreeUtilities |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param mixed $id |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract public function RecallDaftObject($id) : ? DaftObject; |
|
21
|
|
|
|
|
22
|
|
|
abstract public function CountDaftNestedObjectTreeWithObject( |
|
23
|
|
|
DaftNestedObject $root, |
|
24
|
|
|
bool $includeRoot, |
|
25
|
|
|
? int $relativeDepthLimit |
|
26
|
|
|
) : int; |
|
27
|
|
|
|
|
28
|
|
|
abstract public function RemoveDaftObject(DefinesOwnIdPropertiesInterface $object) : void; |
|
29
|
|
|
|
|
30
|
|
|
abstract public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int; |
|
31
|
|
|
|
|
32
|
|
|
abstract public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void; |
|
33
|
|
|
|
|
34
|
|
|
abstract public function ForgetDaftObject(DefinesOwnIdPropertiesInterface $object) : void; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param mixed $id |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract public function ForgetDaftObjectById($id) : void; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array<int, DaftNestedObject> |
|
43
|
|
|
*/ |
|
44
|
|
|
abstract public function RecallDaftNestedObjectTreeWithObject( |
|
45
|
|
|
DaftNestedObject $root, |
|
46
|
|
|
bool $includeRoot, |
|
47
|
|
|
? int $relativeDepthLimit |
|
48
|
|
|
) : array; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array<int, DaftNestedWriteableObject> |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param mixed $id |
|
57
|
|
|
* |
|
58
|
|
|
* @return array<int, DaftNestedWriteableObject> |
|
59
|
|
|
*/ |
|
60
|
|
|
abstract public function RecallDaftNestedObjectTreeWithId( |
|
61
|
|
|
$id, |
|
62
|
|
|
bool $includeRoot, |
|
63
|
|
|
? int $relativeDepthLimit |
|
64
|
|
|
) : array; |
|
65
|
|
|
|
|
66
|
48 |
|
protected function ModifyDaftNestedObjectTreeInsertMaybeLooseIntoTree( |
|
67
|
|
|
DaftNestedWriteableObjectTree $tree, |
|
68
|
|
|
? DaftNestedWriteableObject $leaf, |
|
69
|
|
|
? DaftObject $reference, |
|
70
|
|
|
bool $isRoot, |
|
71
|
|
|
bool $before, |
|
72
|
|
|
? bool $above |
|
73
|
|
|
) : ? DaftNestedWriteableObject { |
|
74
|
48 |
|
if ( ! is_null($leaf) && (($reference instanceof DaftNestedWriteableObject) || $isRoot)) { |
|
75
|
24 |
|
if ($reference instanceof DaftNestedWriteableObject) { |
|
76
|
18 |
|
return $tree->ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
24 |
|
return $this->ModifyDaftNestedObjectTreeInsertLooseIntoTree($leaf, $before, $above); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
24 |
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
60 |
|
protected function RebuildAfterInsert( |
|
86
|
|
|
DaftNestedWriteableObject $newLeaf |
|
87
|
|
|
) : DaftNestedWriteableObject { |
|
88
|
60 |
|
$this->RebuildTreeInefficiently(); |
|
89
|
|
|
|
|
90
|
60 |
|
$newLeaf = $this->RecallDaftObject($newLeaf->GetId()); |
|
91
|
|
|
|
|
92
|
60 |
|
if ( ! ($newLeaf instanceof DaftNestedWriteableObject)) { |
|
93
|
12 |
|
throw new RuntimeException('Could not retrieve leaf from tree after rebuilding!'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
48 |
|
return $newLeaf; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
protected function ModifyDaftNestedObjectTreeRemoveWithObjectPrepareRemovalAndRebuild( |
|
100
|
|
|
DaftNestedWriteableObject $root, |
|
101
|
|
|
DaftNestedWriteableObject $replacementRoot |
|
102
|
|
|
) : void { |
|
103
|
|
|
/** |
|
104
|
|
|
* @var scalar|scalar[] $replacementRootId |
|
105
|
|
|
*/ |
|
106
|
4 |
|
$replacementRootId = $this->StoreThenRetrieveFreshLeaf($replacementRoot)->GetId(); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
4 |
|
$this->UpdateRoots($root, $replacementRootId); |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param scalar|scalar[] $replacementRootId |
|
113
|
|
|
*/ |
|
114
|
10 |
|
protected function UpdateRoots(DaftNestedWriteableObject $root, $replacementRootId) : void |
|
115
|
|
|
{ |
|
116
|
|
|
/** |
|
117
|
|
|
* @var DaftNestedWriteableObject $alter |
|
118
|
|
|
*/ |
|
119
|
10 |
|
foreach ($this->RecallDaftNestedObjectTreeWithObject($root, false, 1) as $alter) { |
|
120
|
4 |
|
if ($alter instanceof DaftNestedWriteableObject) { |
|
121
|
4 |
|
$alter->AlterDaftNestedObjectParentId($replacementRootId); |
|
122
|
4 |
|
$this->RememberDaftObject($alter); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
10 |
|
} |
|
126
|
|
|
|
|
127
|
110 |
|
final protected function ThrowIfNotTree() : DaftNestedWriteableObjectTree |
|
128
|
|
|
{ |
|
129
|
110 |
|
if ( ! ($this instanceof DaftNestedWriteableObjectTree)) { |
|
130
|
2 |
|
throw new BadMethodCallException( |
|
131
|
|
|
'Cannot call ' . |
|
132
|
|
|
__FUNCTION__ . |
|
133
|
|
|
' on ' . |
|
134
|
2 |
|
static::class . |
|
135
|
2 |
|
', class does not implement ' . |
|
136
|
2 |
|
DaftNestedWriteableObjectTree::class |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
108 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param DaftNestedWriteableObject|mixed $leaf |
|
145
|
|
|
*/ |
|
146
|
72 |
|
protected function MaybeGetLeaf($leaf) : ? DaftNestedWriteableObject |
|
147
|
|
|
{ |
|
148
|
72 |
|
$tree = $this->ThrowIfNotTree(); |
|
149
|
|
|
|
|
150
|
72 |
|
if ($leaf === $tree->GetNestedObjectTreeRootId()) { |
|
151
|
24 |
|
throw new InvalidArgumentException('Cannot pass root id as new leaf'); |
|
152
|
48 |
|
} elseif ($leaf instanceof DaftNestedWriteableObject) { |
|
153
|
24 |
|
return $tree->StoreThenRetrieveFreshLeaf($leaf); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @var DaftNestedWriteableObject|null $out |
|
158
|
|
|
*/ |
|
159
|
40 |
|
$out = $tree->RecallDaftObject($leaf); |
|
160
|
|
|
|
|
161
|
40 |
|
return ($out instanceof DaftNestedWriteableObject) ? $out : null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
24 |
|
protected function ModifyDaftNestedObjectTreeInsertLooseIntoTree( |
|
165
|
|
|
DaftNestedWriteableObject $leaf, |
|
166
|
|
|
bool $before, |
|
167
|
|
|
? bool $above |
|
168
|
|
|
) : DaftNestedWriteableObject { |
|
169
|
24 |
|
$leaves = $this->RecallDaftNestedObjectFullTree(0); |
|
170
|
|
|
$leaves = array_filter($leaves, function (DaftNestedWriteableObject $e) use ($leaf) : bool { |
|
171
|
24 |
|
return $e->GetId() !== $leaf->GetId(); |
|
172
|
24 |
|
}); |
|
173
|
24 |
|
$tree = $this->ThrowIfNotTree(); |
|
174
|
|
|
|
|
175
|
24 |
|
if (0 === count($leaves)) { |
|
176
|
24 |
|
$leaf->SetIntNestedLeft(0); |
|
177
|
24 |
|
$leaf->SetIntNestedRight(1); |
|
178
|
24 |
|
$leaf->SetIntNestedLevel(0); |
|
179
|
24 |
|
$leaf->AlterDaftNestedObjectParentId($tree->GetNestedObjectTreeRootId()); |
|
180
|
|
|
|
|
181
|
24 |
|
return $tree->StoreThenRetrieveFreshLeaf($leaf); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
24 |
|
$reference = $before ? current($leaves) : end($leaves); |
|
185
|
|
|
|
|
186
|
24 |
|
return $this->ModifyDaftNestedObjectTreeInsert($leaf, $reference, $before, $above); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
4 |
|
protected function MaybeRemoveWithPossibleObject( |
|
190
|
|
|
DaftNestedWriteableObject $rootObject, |
|
191
|
|
|
? DaftObject $replacementRootObject |
|
192
|
|
|
) : int { |
|
193
|
4 |
|
if ( ! ($replacementRootObject instanceof DaftNestedWriteableObject)) { |
|
194
|
2 |
|
throw new InvalidArgumentException( |
|
195
|
2 |
|
'Could not locate replacement root, cannot leave orphan objects!' |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
2 |
|
return $this->ModifyDaftNestedObjectTreeRemoveWithObject( |
|
|
|
|
|
|
200
|
2 |
|
$rootObject, |
|
201
|
2 |
|
$replacementRootObject |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param scalar|scalar[] $replacementRoot |
|
207
|
|
|
*/ |
|
208
|
6 |
|
protected function UpdateRemoveThenRebuild( |
|
209
|
|
|
DaftNestedWriteableObject $rootObject, |
|
210
|
|
|
$replacementRoot |
|
211
|
|
|
) : void { |
|
212
|
6 |
|
$this->UpdateRoots($rootObject, $replacementRoot); |
|
213
|
|
|
|
|
214
|
6 |
|
$this->RemoveDaftObject($rootObject); |
|
215
|
|
|
|
|
216
|
6 |
|
$this->RebuildTreeInefficiently(); |
|
217
|
6 |
|
} |
|
218
|
|
|
|
|
219
|
48 |
|
protected function ModifyDaftNestedObjectTreeInsertAbove( |
|
220
|
|
|
DaftNestedWriteableObject $newLeaf, |
|
221
|
|
|
DaftNestedWriteableObject $referenceLeaf |
|
222
|
|
|
) : void { |
|
223
|
48 |
|
$newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->ObtainDaftNestedObjectParentId()); |
|
224
|
48 |
|
$referenceLeaf->AlterDaftNestedObjectParentId($newLeaf->GetId()); |
|
225
|
|
|
|
|
226
|
48 |
|
$this->StoreThenRetrieveFreshLeaf($newLeaf); |
|
227
|
48 |
|
$this->StoreThenRetrieveFreshLeaf($referenceLeaf); |
|
228
|
48 |
|
} |
|
229
|
|
|
|
|
230
|
6 |
|
protected function ModifyDaftNestedObjectTreeInsertBelow( |
|
231
|
|
|
DaftNestedWriteableObject $newLeaf, |
|
232
|
|
|
DaftNestedWriteableObject $referenceLeaf |
|
233
|
|
|
) : void { |
|
234
|
6 |
|
$newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->GetId()); |
|
235
|
6 |
|
$this->StoreThenRetrieveFreshLeaf($newLeaf); |
|
236
|
6 |
|
} |
|
237
|
|
|
|
|
238
|
52 |
|
protected function ModifyDaftNestedObjectTreeInsertAdjacent( |
|
239
|
|
|
DaftNestedWriteableObject $newLeaf, |
|
240
|
|
|
DaftNestedWriteableObject $referenceLeaf, |
|
241
|
|
|
bool $before |
|
242
|
|
|
) : void { |
|
243
|
|
|
/** |
|
244
|
|
|
* @var array<int, DaftNestedWriteableObject> $siblings |
|
245
|
|
|
*/ |
|
246
|
52 |
|
$siblings = array_values(array_filter( |
|
247
|
52 |
|
$this->RecallDaftNestedObjectTreeWithId( |
|
248
|
52 |
|
$referenceLeaf->ObtainDaftNestedObjectParentId(), |
|
249
|
52 |
|
false, |
|
250
|
|
|
0 |
|
251
|
|
|
), |
|
252
|
|
|
function (DaftNestedWriteableObject $leaf) use ($newLeaf) : bool { |
|
253
|
28 |
|
return $leaf->GetId() !== $newLeaf->GetId(); |
|
254
|
52 |
|
} |
|
255
|
|
|
)); |
|
256
|
|
|
|
|
257
|
52 |
|
$siblingIds = []; |
|
258
|
52 |
|
$siblingSort = []; |
|
259
|
52 |
|
$j = count($siblings); |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @var DaftNestedWriteableObject $leaf |
|
263
|
|
|
*/ |
|
264
|
52 |
|
foreach ($siblings as $leaf) { |
|
265
|
|
|
/** |
|
266
|
|
|
* @var scalar|scalar[] $siblingId |
|
267
|
|
|
*/ |
|
268
|
28 |
|
$siblingId = $leaf->GetId(); |
|
269
|
28 |
|
$siblingIds[] = $siblingId; |
|
270
|
28 |
|
$siblingSort[] = $leaf->GetIntNestedSortOrder(); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
52 |
|
$pos = array_search($referenceLeaf->GetId(), $siblingIds, true); |
|
274
|
|
|
|
|
275
|
52 |
|
if (false === $pos) { |
|
276
|
24 |
|
throw new RuntimeException('Reference leaf not found in siblings tree!'); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
28 |
|
for ($i = 0; $i < $j; ++$i) { |
|
280
|
28 |
|
$siblings[$i]->SetIntNestedSortOrder( |
|
281
|
28 |
|
$siblingSort[$i] + |
|
282
|
28 |
|
(($before ? ($i < $pos) : ($i <= $pos)) ? -1 : 1) |
|
283
|
|
|
); |
|
284
|
28 |
|
$this->StoreThenRetrieveFreshLeaf($siblings[$i]); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
28 |
|
$newLeaf->SetIntNestedSortOrder($siblingSort[$pos]); |
|
288
|
28 |
|
$newLeaf->AlterDaftNestedObjectParentId($referenceLeaf->ObtainDaftNestedObjectParentId()); |
|
289
|
|
|
|
|
290
|
28 |
|
$this->StoreThenRetrieveFreshLeaf($newLeaf); |
|
291
|
28 |
|
} |
|
292
|
|
|
|
|
293
|
60 |
|
protected function RebuildTreeInefficiently() : void |
|
294
|
|
|
{ |
|
295
|
|
|
/** |
|
296
|
|
|
* @var DaftNestedWriteableObjectTree $tree |
|
297
|
|
|
*/ |
|
298
|
60 |
|
$tree = $this->ThrowIfNotTree(); |
|
299
|
60 |
|
$rebuilder = new InefficientDaftNestedRebuild($tree); |
|
300
|
60 |
|
$rebuilder->RebuildTree(); |
|
301
|
60 |
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|