|
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 TraitWriteableTree |
|
16
|
|
|
{ |
|
17
|
|
|
use TraitWriteableTreeUtilities; |
|
18
|
|
|
|
|
19
|
84 |
|
public function ModifyDaftNestedObjectTreeInsert( |
|
20
|
|
|
DaftNestedWriteableObject $newLeaf, |
|
21
|
|
|
DaftNestedWriteableObject $referenceLeaf, |
|
22
|
|
|
bool $before = false, |
|
23
|
|
|
bool $above = null |
|
24
|
|
|
) : DaftNestedWriteableObject { |
|
25
|
84 |
|
if ($newLeaf->GetId() === $referenceLeaf->GetId()) { |
|
26
|
24 |
|
throw new InvalidArgumentException('Cannot modify leaf relative to itself!'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
60 |
|
if (true === $above) { |
|
30
|
48 |
|
$this->ModifyDaftNestedObjectTreeInsertAbove($newLeaf, $referenceLeaf); |
|
31
|
32 |
|
} elseif (false === $above) { |
|
32
|
6 |
|
$this->ModifyDaftNestedObjectTreeInsertBelow($newLeaf, $referenceLeaf); |
|
33
|
|
|
} else { |
|
34
|
28 |
|
$this->ModifyDaftNestedObjectTreeInsertAdjacent($newLeaf, $referenceLeaf, $before); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
60 |
|
return $this->RebuildAfterInsert($newLeaf); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param mixed $leaf |
|
42
|
|
|
* @param mixed $referenceId |
|
43
|
|
|
*/ |
|
44
|
72 |
|
public function ModifyDaftNestedObjectTreeInsertLoose( |
|
45
|
|
|
$leaf, |
|
46
|
|
|
$referenceId, |
|
47
|
|
|
bool $before = false, |
|
48
|
|
|
bool $above = null |
|
49
|
|
|
) : DaftNestedWriteableObject { |
|
50
|
72 |
|
$leaf = $this->MaybeGetLeaf($leaf); |
|
51
|
|
|
|
|
52
|
48 |
|
$reference = $this->RecallDaftObject($referenceId); |
|
53
|
48 |
|
$tree = $this->ThrowIfNotTree(); |
|
54
|
|
|
|
|
55
|
48 |
|
$resp = $this->ModifyDaftNestedObjectTreeInsertMaybeLooseIntoTree( |
|
56
|
48 |
|
$tree, |
|
57
|
48 |
|
$leaf, |
|
58
|
48 |
|
$reference, |
|
59
|
48 |
|
$referenceId === $tree->GetNestedObjectTreeRootId(), |
|
60
|
48 |
|
$before, |
|
61
|
48 |
|
$above |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
48 |
|
if ($resp instanceof DaftNestedWriteableObject) { |
|
65
|
24 |
|
return $resp; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
24 |
|
throw new InvalidArgumentException(sprintf( |
|
69
|
24 |
|
'Argument %u passed to %s() did not resolve to a leaf node!', |
|
70
|
24 |
|
is_null($leaf) ? 1 : 2, |
|
71
|
24 |
|
__METHOD__ |
|
72
|
|
|
)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
8 |
|
public function ModifyDaftNestedObjectTreeRemoveWithObject( |
|
76
|
|
|
DaftNestedWriteableObject $root, |
|
77
|
|
|
? DaftNestedWriteableObject $replacementRoot |
|
78
|
|
|
) : int { |
|
79
|
|
|
if ( |
|
80
|
8 |
|
$this->CountDaftNestedObjectTreeWithObject($root, false, null) > 0 && |
|
81
|
8 |
|
is_null($replacementRoot) |
|
82
|
|
|
) { |
|
83
|
2 |
|
throw new BadMethodCallException('Cannot leave orphan objects in a tree'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
$root = $this->StoreThenRetrieveFreshLeaf($root); |
|
87
|
|
|
|
|
88
|
6 |
|
if ( ! is_null($replacementRoot)) { |
|
89
|
4 |
|
$this->ModifyDaftNestedObjectTreeRemoveWithObjectPrepareRemovalAndRebuild( |
|
90
|
4 |
|
$root, |
|
91
|
4 |
|
$replacementRoot |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
$this->RemoveDaftObject($root); |
|
96
|
|
|
|
|
97
|
6 |
|
$this->RebuildTreeInefficiently(); |
|
98
|
|
|
|
|
99
|
6 |
|
return $this->CountDaftNestedObjectFullTree(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param mixed $root |
|
104
|
|
|
* @param scalar|scalar[]|null $replacementRoot |
|
105
|
|
|
*/ |
|
106
|
12 |
|
public function ModifyDaftNestedObjectTreeRemoveWithId($root, $replacementRoot) : int |
|
107
|
|
|
{ |
|
108
|
12 |
|
$rootObject = $this->RecallDaftObject($root); |
|
109
|
|
|
|
|
110
|
12 |
|
if ($rootObject instanceof DaftNestedWriteableObject) { |
|
111
|
12 |
|
$tree = $this->ThrowIfNotTree(); |
|
112
|
|
|
|
|
113
|
|
|
if ( |
|
114
|
12 |
|
$tree->CountDaftNestedObjectTreeWithObject($rootObject, false, null) > 0 && |
|
115
|
12 |
|
is_null($replacementRoot) |
|
116
|
|
|
) { |
|
117
|
2 |
|
throw new BadMethodCallException('Cannot leave orphan objects in a tree'); |
|
118
|
|
|
} elseif ( |
|
119
|
10 |
|
! is_null($replacementRoot) && |
|
120
|
10 |
|
$replacementRoot !== $tree->GetNestedObjectTreeRootId() |
|
121
|
|
|
) { |
|
122
|
4 |
|
$replacementRoot = $tree->RecallDaftObject($replacementRoot); |
|
123
|
|
|
|
|
124
|
4 |
|
return $this->MaybeRemoveWithPossibleObject($rootObject, $replacementRoot); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @var scalar|scalar[] $replacementRoot |
|
129
|
|
|
*/ |
|
130
|
6 |
|
$replacementRoot = $replacementRoot; |
|
131
|
|
|
|
|
132
|
6 |
|
$this->UpdateRemoveThenRebuild($rootObject, $replacementRoot); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6 |
|
return $this->CountDaftNestedObjectFullTree(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
62 |
|
public function StoreThenRetrieveFreshLeaf( |
|
139
|
|
|
DaftNestedWriteableObject $leaf |
|
140
|
|
|
) : DaftNestedWriteableObject { |
|
141
|
62 |
|
$this->RememberDaftObject($leaf); |
|
142
|
62 |
|
$this->ForgetDaftObject($leaf); |
|
143
|
62 |
|
$this->ForgetDaftObjectById($leaf->GetId()); |
|
144
|
|
|
|
|
145
|
62 |
|
$fresh = $this->RecallDaftObject($leaf->GetId()); |
|
146
|
|
|
|
|
147
|
62 |
|
if ( ! ($fresh instanceof DaftNestedWriteableObject)) { |
|
148
|
2 |
|
throw new RuntimeException('Was not able to obtain a fresh copy of the object!'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
60 |
|
return $fresh; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|