1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
abstract class DaftObjectMemoryTree extends DaftObjectMemoryRepository implements DaftNestedObjectTree |
12
|
|
|
{ |
13
|
114 |
|
public function RecallDaftNestedObjectFullTree(int $relativeDepthLimit = null) : array |
14
|
|
|
{ |
15
|
114 |
|
$type = $this->type; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DaftNestedObject[] $out |
19
|
|
|
*/ |
20
|
114 |
|
$out = $this->memory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array<int, scalar|scalar[]> $outIds |
24
|
|
|
*/ |
25
|
114 |
|
$outIds = []; |
26
|
|
|
|
27
|
114 |
|
foreach ($out as $obj) { |
28
|
|
|
/** |
29
|
|
|
* @var array<int, scalar|scalar[]> $id |
30
|
|
|
*/ |
31
|
68 |
|
$id = $obj->GetId(); |
32
|
|
|
|
33
|
68 |
|
$outIds[] = $id; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DaftNestedObject[] $fromMemory |
38
|
|
|
*/ |
39
|
114 |
|
$fromMemory = array_filter( |
40
|
114 |
|
array_map( |
41
|
|
|
/** |
42
|
|
|
* @param array<string, scalar|null> $row |
43
|
|
|
*/ |
44
|
|
|
function (array $row) use ($type) : DaftNestedObject { |
45
|
|
|
/** |
46
|
|
|
* @var DaftNestedObject $out |
47
|
|
|
*/ |
48
|
88 |
|
$out = new $type($row); |
49
|
|
|
|
50
|
88 |
|
return $out; |
51
|
114 |
|
}, |
52
|
114 |
|
(array) $this->data |
53
|
|
|
), |
54
|
|
|
function (DaftNestedObject $leaf) use ($outIds) : bool { |
55
|
88 |
|
return ! in_array($leaf->GetId(), $outIds, true); |
56
|
114 |
|
} |
57
|
|
|
); |
58
|
|
|
|
59
|
114 |
|
$out = array_merge($out, $fromMemory); |
60
|
|
|
|
61
|
|
|
usort($out, function (DaftNestedObject $a, DaftNestedObject $b) : int { |
62
|
64 |
|
return $a->GetIntNestedLeft() <=> $b->GetIntNestedLeft(); |
63
|
114 |
|
}); |
64
|
|
|
|
65
|
114 |
|
if (is_int($relativeDepthLimit)) { |
66
|
32 |
|
$out = array_filter( |
67
|
32 |
|
$out, |
68
|
|
|
function (DaftNestedObject $e) use ($relativeDepthLimit) : bool { |
69
|
32 |
|
return $e->GetIntNestedLevel() <= $relativeDepthLimit; |
70
|
32 |
|
} |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
114 |
|
return $out; |
75
|
|
|
} |
76
|
|
|
|
77
|
114 |
|
public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int |
78
|
|
|
{ |
79
|
114 |
|
return count($this->RecallDaftNestedObjectFullTree($relativeDepthLimit)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
44 |
|
public function RecallDaftNestedObjectTreeWithObject( |
86
|
|
|
DaftNestedObject $root, |
87
|
|
|
bool $includeRoot, |
88
|
|
|
? int $limit |
89
|
|
|
) : array { |
90
|
44 |
|
$left = $root->GetIntNestedLeft(); |
91
|
44 |
|
$right = $root->GetIntNestedRight(); |
92
|
44 |
|
$limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null; |
93
|
|
|
|
94
|
44 |
|
$leaves = $this->RecallDaftNestedObjectFullTree(); |
95
|
|
|
|
96
|
44 |
|
if (is_int($limit)) { |
97
|
|
|
$leaves = array_filter($leaves, function (DaftNestedObject $e) use ($limit) : bool { |
98
|
38 |
|
return $e->GetIntNestedLevel() <= $limit; |
99
|
38 |
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
44 |
|
return array_values(array_filter( |
103
|
44 |
|
$leaves, |
104
|
|
|
function (DaftNestedObject $e) use ($includeRoot, $left, $right) : bool { |
105
|
44 |
|
return $this->FilterLeaf($includeRoot, $left, $right, $e); |
106
|
44 |
|
} |
107
|
|
|
)); |
108
|
|
|
} |
109
|
|
|
|
110
|
20 |
|
public function CountDaftNestedObjectTreeWithObject( |
111
|
|
|
DaftNestedObject $root, |
112
|
|
|
bool $includeRoot, |
113
|
|
|
? int $relativeDepthLimit |
114
|
|
|
) : int { |
115
|
20 |
|
return count( |
116
|
20 |
|
$this->RecallDaftNestedObjectTreeWithObject($root, $includeRoot, $relativeDepthLimit) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
56 |
|
public function RecallDaftNestedObjectTreeWithId( |
121
|
|
|
$id, |
122
|
|
|
bool $includeRoot, |
123
|
|
|
? int $relativeDepthLimit |
124
|
|
|
) : array { |
125
|
56 |
|
$object = $this->RecallDaftObject($id); |
126
|
|
|
|
127
|
|
|
return |
128
|
56 |
|
($object instanceof DaftNestedObject) |
129
|
28 |
|
? $this->RecallDaftNestedObjectTreeWithObject( |
130
|
28 |
|
$object, |
131
|
28 |
|
$includeRoot, |
132
|
28 |
|
$relativeDepthLimit |
133
|
|
|
) |
134
|
|
|
: ( |
135
|
32 |
|
((array) $id === (array) $this->GetNestedObjectTreeRootId()) |
136
|
32 |
|
? $this->RecallDaftNestedObjectFullTree($relativeDepthLimit) |
137
|
56 |
|
: [] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
public function CountDaftNestedObjectTreeWithId( |
142
|
|
|
$id, |
143
|
|
|
bool $includeRoot, |
144
|
|
|
? int $relativeDepthLimit |
145
|
|
|
) : int { |
146
|
4 |
|
return count($this->RecallDaftNestedObjectTreeWithId( |
147
|
4 |
|
$id, |
148
|
4 |
|
$includeRoot, |
149
|
4 |
|
$relativeDepthLimit |
150
|
|
|
)); |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
public function RecallDaftNestedObjectPathToObject( |
154
|
|
|
DaftNestedObject $leaf, |
155
|
|
|
bool $includeLeaf |
156
|
|
|
) : array { |
157
|
4 |
|
$left = $leaf->GetIntNestedLeft(); |
158
|
4 |
|
$right = $leaf->GetIntNestedRight(); |
159
|
|
|
|
160
|
4 |
|
if ( ! $includeLeaf) { |
161
|
4 |
|
--$left; |
162
|
4 |
|
++$right; |
163
|
|
|
} |
164
|
|
|
|
165
|
4 |
|
return array_values(array_filter( |
166
|
4 |
|
$this->RecallDaftNestedObjectFullTree(), |
167
|
|
|
function (DaftNestedObject $e) use ($left, $right) : bool { |
168
|
4 |
|
return $e->GetIntNestedLeft() <= $left && $e->GetIntNestedRight() >= $right; |
169
|
4 |
|
} |
170
|
|
|
)); |
171
|
|
|
} |
172
|
|
|
|
173
|
4 |
|
public function CountDaftNestedObjectPathToObject( |
174
|
|
|
DaftNestedObject $leaf, |
175
|
|
|
bool $includeLeaf |
176
|
|
|
) : int { |
177
|
4 |
|
return count($this->RecallDaftNestedObjectPathToObject($leaf, $includeLeaf)); |
178
|
|
|
} |
179
|
|
|
|
180
|
4 |
|
public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array |
181
|
|
|
{ |
182
|
4 |
|
$object = $this->RecallDaftObject($id); |
183
|
|
|
|
184
|
|
|
return |
185
|
4 |
|
($object instanceof DaftNestedObject) |
186
|
4 |
|
? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf) |
187
|
4 |
|
: []; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/* |
191
|
|
|
* @param mixed $id |
192
|
|
|
*/ |
193
|
4 |
|
public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int |
194
|
|
|
{ |
195
|
4 |
|
return count($this->RecallDaftNestedObjectPathToId($id, $includeLeaf)); |
196
|
|
|
} |
197
|
|
|
|
198
|
60 |
|
public function CompareObjects(DaftNestedObject $a, DaftNestedObject $b) : int |
199
|
|
|
{ |
200
|
60 |
|
return $a->GetIntNestedSortOrder() <=> $b->GetIntNestedSortOrder(); |
201
|
|
|
} |
202
|
|
|
|
203
|
44 |
|
protected function FilterLeaf( |
204
|
|
|
bool $includeRoot, |
205
|
|
|
int $left, |
206
|
|
|
int $right, |
207
|
|
|
DaftNestedObject $e |
208
|
|
|
) : bool { |
209
|
44 |
|
if ($includeRoot) { |
210
|
4 |
|
return $e->GetIntNestedLeft() >= $left && $e->GetIntNestedRight() <= $right; |
211
|
|
|
} |
212
|
|
|
|
213
|
44 |
|
return $e->GetIntNestedLeft() > $left && $e->GetIntNestedRight() < $right; |
214
|
|
|
} |
215
|
|
|
|
216
|
118 |
|
protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void |
217
|
|
|
{ |
218
|
118 |
|
static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__); |
219
|
|
|
|
220
|
118 |
|
parent::RememberDaftObjectData($object); |
221
|
118 |
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param DaftObject|string $object |
225
|
|
|
*/ |
226
|
156 |
|
protected static function ThrowIfNotType( |
227
|
|
|
$object, |
228
|
|
|
string $type, |
229
|
|
|
int $argument, |
230
|
|
|
string $function |
231
|
|
|
) : void { |
232
|
156 |
|
parent::ThrowIfNotType($object, $type, $argument, $function); |
233
|
|
|
|
234
|
156 |
|
if ( ! is_a($object, DaftNestedObject::class, is_string($object))) { |
235
|
2 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
236
|
2 |
|
$argument, |
237
|
2 |
|
static::class, |
238
|
2 |
|
$function, |
239
|
2 |
|
DaftNestedObject::class, |
240
|
2 |
|
is_string($object) ? $object : get_class($object) |
241
|
|
|
); |
242
|
|
|
} |
243
|
154 |
|
} |
244
|
|
|
} |
245
|
|
|
|