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 ParagonIE\EasyDB\EasyDB; |
12
|
|
|
use PDO; |
13
|
|
|
use PDOStatement; |
14
|
|
|
|
15
|
|
|
abstract class AbstractDaftObjectEasyDBTree extends AbstractDaftObjectEasyDBRepository implements DaftNestedObjectTree |
16
|
|
|
{ |
17
|
28 |
|
public function RecallDaftNestedObjectFullTree(int $limit = null) : array |
18
|
|
|
{ |
19
|
28 |
|
return $this->RecallDaftNestedObjectTreeFromArgs(null, null, $limit, false); |
20
|
|
|
} |
21
|
|
|
|
22
|
40 |
|
public function CountDaftNestedObjectFullTree(int $withLimit = null) : int |
23
|
|
|
{ |
24
|
40 |
|
return $this->CountDaftNestedObjectTreeFromArgs(null, null, $withLimit, false); |
25
|
|
|
} |
26
|
|
|
|
27
|
14 |
|
public function RecallDaftNestedObjectTreeWithObject( |
28
|
|
|
DaftNestedObject $root, |
29
|
|
|
bool $withRoot, |
30
|
|
|
? int $limit |
31
|
|
|
) : array { |
32
|
14 |
|
$left = $root->GetIntNestedLeft(); |
33
|
14 |
|
$right = $root->GetIntNestedRight(); |
34
|
14 |
|
$limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null; |
35
|
|
|
|
36
|
14 |
|
return $this->RecallDaftNestedObjectTreeFromArgs($left, $right, $limit, $withRoot); |
37
|
|
|
} |
38
|
|
|
|
39
|
20 |
|
public function CountDaftNestedObjectTreeWithObject( |
40
|
|
|
DaftNestedObject $root, |
41
|
|
|
bool $withRoot, |
42
|
|
|
? int $limit |
43
|
|
|
) : int { |
44
|
20 |
|
$left = $root->GetIntNestedLeft(); |
45
|
20 |
|
$right = $root->GetIntNestedRight(); |
46
|
20 |
|
$limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null; |
47
|
|
|
|
48
|
20 |
|
return $this->CountDaftNestedObjectTreeFromArgs($left, $right, $limit, $withRoot); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $id |
53
|
|
|
*/ |
54
|
28 |
|
public function RecallDaftNestedObjectTreeWithId( |
55
|
|
|
$id, |
56
|
|
|
bool $withRoot, |
57
|
|
|
? int $limit |
58
|
|
|
) : array { |
59
|
28 |
|
$object = $this->RecallDaftObject($id); |
60
|
|
|
|
61
|
|
|
return |
62
|
28 |
|
($object instanceof DaftNestedObject) |
63
|
4 |
|
? $this->RecallDaftNestedObjectTreeWithObject( |
64
|
4 |
|
$object, |
65
|
4 |
|
$withRoot, |
66
|
4 |
|
$limit |
67
|
|
|
) |
68
|
|
|
: ( |
69
|
24 |
|
((array) $id === (array) $this->GetNestedObjectTreeRootId()) |
70
|
24 |
|
? $this->RecallDaftNestedObjectFullTree(0) |
71
|
28 |
|
: [] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $id |
77
|
|
|
*/ |
78
|
4 |
|
public function CountDaftNestedObjectTreeWithId( |
79
|
|
|
$id, |
80
|
|
|
bool $withRoot, |
81
|
|
|
? int $limit |
82
|
|
|
) : int { |
83
|
4 |
|
$object = $this->RecallDaftObject($id); |
84
|
|
|
|
85
|
|
|
return |
86
|
4 |
|
($object instanceof DaftNestedObject) |
87
|
4 |
|
? $this->CountDaftNestedObjectTreeWithObject( |
88
|
4 |
|
$object, |
89
|
4 |
|
$withRoot, |
90
|
4 |
|
$limit |
91
|
|
|
) |
92
|
|
|
: ( |
93
|
4 |
|
((array) $id === (array) $this->GetNestedObjectTreeRootId()) |
94
|
4 |
|
? $this->CountDaftNestedObjectFullTree($limit) |
95
|
4 |
|
: 0 |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
public function RecallDaftNestedObjectPathToObject( |
100
|
|
|
DaftNestedObject $leaf, |
101
|
|
|
bool $includeLeaf |
102
|
|
|
) : array { |
103
|
4 |
|
return $this->RecallDaftNestedObjectTreeFromArgs( |
104
|
4 |
|
$leaf->GetIntNestedLeft(), |
105
|
4 |
|
$leaf->GetIntNestedRight(), |
106
|
4 |
|
null, |
107
|
4 |
|
$includeLeaf, |
108
|
4 |
|
false |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
public function CountDaftNestedObjectPathToObject( |
113
|
|
|
DaftNestedObject $leaf, |
114
|
|
|
bool $includeLeaf |
115
|
|
|
) : int { |
116
|
4 |
|
return $this->CountDaftNestedObjectTreeFromArgs( |
117
|
4 |
|
$leaf->GetIntNestedLeft(), |
118
|
4 |
|
$leaf->GetIntNestedRight(), |
119
|
4 |
|
null, |
120
|
4 |
|
$includeLeaf, |
121
|
4 |
|
false |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param mixed $id |
127
|
|
|
*/ |
128
|
4 |
|
public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array |
129
|
|
|
{ |
130
|
4 |
|
$object = $this->RecallDaftObject($id); |
131
|
|
|
|
132
|
|
|
return |
133
|
4 |
|
($object instanceof DaftNestedObject) |
134
|
4 |
|
? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf) |
135
|
4 |
|
: []; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed $id |
140
|
|
|
*/ |
141
|
4 |
|
public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int |
142
|
|
|
{ |
143
|
4 |
|
$object = $this->RecallDaftObject($id); |
144
|
|
|
|
145
|
|
|
return |
146
|
4 |
|
($object instanceof DaftNestedObject) |
147
|
4 |
|
? $this->CountDaftNestedObjectPathToObject($object, $includeLeaf) |
148
|
4 |
|
: 0; |
149
|
|
|
} |
150
|
|
|
|
151
|
50 |
|
public static function DaftObjectRepositoryByType( |
152
|
|
|
string $type, |
153
|
|
|
? EasyDB $db = null |
154
|
|
|
) : DaftObjectRepository { |
155
|
50 |
|
if (is_a(static::class, DaftNestedWriteableObjectTree::class, true)) { |
156
|
38 |
|
if ( ! is_a($type, DaftNestedWriteableObject::class, true)) { |
157
|
10 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
158
|
10 |
|
1, |
159
|
10 |
|
static::class, |
160
|
10 |
|
__FUNCTION__, |
161
|
10 |
|
DaftNestedWriteableObject::class, |
162
|
38 |
|
$type |
163
|
|
|
); |
164
|
|
|
} |
165
|
12 |
|
} elseif ( ! is_a($type, DaftNestedObject::class, true)) { |
166
|
10 |
|
throw new DaftObjectRepositoryTypeByClassMethodAndTypeException( |
167
|
10 |
|
1, |
168
|
10 |
|
static::class, |
169
|
10 |
|
__FUNCTION__, |
170
|
10 |
|
DaftNestedObject::class, |
171
|
10 |
|
$type |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
30 |
|
return parent::DaftObjectRepositoryByType($type, $db); |
176
|
|
|
} |
177
|
|
|
|
178
|
40 |
|
public function RememberDaftObjectData( |
179
|
|
|
DefinesOwnIdPropertiesInterface $object, |
180
|
|
|
bool $assumeDoesNotExist = false |
181
|
|
|
) : void { |
182
|
40 |
|
static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__); |
183
|
|
|
|
184
|
40 |
|
parent::RememberDaftObjectData($object, $assumeDoesNotExist); |
185
|
40 |
|
} |
186
|
|
|
|
187
|
40 |
|
final protected function SelectingQueryDaftNestedObjectTreeFromArgs(bool $recall) : string |
188
|
|
|
{ |
189
|
40 |
|
if ($recall) { |
190
|
|
|
/** |
191
|
|
|
* @var string[] $props |
192
|
|
|
*/ |
193
|
28 |
|
$props = $this->type::DaftObjectIdProperties(); |
194
|
|
|
|
195
|
28 |
|
return implode(', ', array_map( |
196
|
|
|
function (string $prop) : string { |
197
|
28 |
|
return $this->db->escapeIdentifier($prop); |
198
|
28 |
|
}, |
199
|
28 |
|
$props |
200
|
|
|
)); |
201
|
|
|
} |
202
|
|
|
|
203
|
40 |
|
return 'COUNT(*)'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param array<int, string> $filter |
208
|
|
|
*/ |
209
|
40 |
|
final protected function FilterQueryDaftNestedObjectTreeFromArgs(array $filter) : string |
210
|
|
|
{ |
211
|
40 |
|
return (count($filter) > 0) ? (' WHERE ' . implode(' AND ', $filter)) : ''; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return array<int, string> |
216
|
|
|
*/ |
217
|
40 |
|
final protected function LeftOpRightOpDaftNestedObjectTreeFromArgs( |
218
|
|
|
bool $withRoot, |
219
|
|
|
bool $treeNotPath |
220
|
|
|
) : array { |
221
|
40 |
|
$leftOp = ($withRoot ? ' >= ' : ' > '); |
222
|
40 |
|
$rightOp = ($withRoot ? ' <= ' : ' < '); |
223
|
|
|
|
224
|
40 |
|
if ( ! $treeNotPath) { |
225
|
4 |
|
list($leftOp, $rightOp) = [$rightOp, $leftOp]; |
226
|
|
|
} |
227
|
|
|
|
228
|
40 |
|
return [$leftOp, $rightOp]; |
229
|
|
|
} |
230
|
|
|
|
231
|
40 |
|
protected function QueryDaftNestedObjectTreeFromArgs( |
232
|
|
|
bool $recall, |
233
|
|
|
? int $left, |
234
|
|
|
? int $right, |
235
|
|
|
? int $limit, |
236
|
|
|
bool $withRoot, |
237
|
|
|
bool $treeNotPath = true |
238
|
|
|
) : PDOStatement { |
239
|
40 |
|
$queryArgs = []; |
240
|
40 |
|
$filter = []; |
241
|
|
|
|
242
|
40 |
|
list($leftOp, $rightOp) = $this->LeftOpRightOpDaftNestedObjectTreeFromArgs( |
243
|
40 |
|
$withRoot, |
244
|
40 |
|
$treeNotPath |
245
|
|
|
); |
246
|
|
|
|
247
|
40 |
|
$escapedLeft = $this->db->escapeIdentifier('intNestedLeft'); |
248
|
|
|
|
249
|
|
|
$maybeArgs = [ |
250
|
40 |
|
($escapedLeft . $leftOp . ' ?') => $left, |
251
|
40 |
|
($this->db->escapeIdentifier('intNestedRight') . $rightOp . ' ?') => $right, |
252
|
40 |
|
($this->db->escapeIdentifier('intNestedLevel') . ' <= ?') => $limit, |
253
|
|
|
]; |
254
|
|
|
|
255
|
40 |
|
foreach (array_filter($maybeArgs, 'is_int') as $filterStr => $queryArgVar) { |
256
|
28 |
|
$queryArgs[] = $queryArgVar; |
257
|
28 |
|
$filter[] = $filterStr; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$query = |
261
|
|
|
'SELECT ' . |
262
|
40 |
|
$this->SelectingQueryDaftNestedObjectTreeFromArgs($recall) . |
263
|
40 |
|
' FROM ' . |
264
|
40 |
|
$this->db->escapeIdentifier($this->DaftObjectDatabaseTable()) . |
265
|
40 |
|
$this->FilterQueryDaftNestedObjectTreeFromArgs($filter) . |
266
|
40 |
|
' ORDER BY ' . |
267
|
40 |
|
$escapedLeft; |
268
|
|
|
|
269
|
40 |
|
$sth = $this->db->prepare($query); |
270
|
|
|
|
271
|
40 |
|
$sth->execute($queryArgs); |
272
|
|
|
|
273
|
40 |
|
return $sth; |
274
|
|
|
} |
275
|
|
|
|
276
|
28 |
|
protected function RecallDaftNestedObjectTreeFromArgs( |
277
|
|
|
? int $left, |
278
|
|
|
? int $right, |
279
|
|
|
? int $limit, |
280
|
|
|
bool $withRoot, |
281
|
|
|
bool $treeNotPath = true |
282
|
|
|
) : array { |
283
|
28 |
|
$sth = $this->QueryDaftNestedObjectTreeFromArgs( |
284
|
28 |
|
true, |
285
|
28 |
|
$left, |
286
|
28 |
|
$right, |
287
|
28 |
|
$limit, |
288
|
28 |
|
$withRoot, |
289
|
28 |
|
$treeNotPath |
290
|
|
|
); |
291
|
|
|
|
292
|
28 |
|
return array_filter( |
293
|
28 |
|
array_map([$this, 'RecallDaftObject'], (array) $sth->fetchAll(PDO::FETCH_NUM)), |
294
|
|
|
function (? DaftObject $maybe) : bool { |
295
|
28 |
|
return $maybe instanceof DaftNestedObject; |
296
|
28 |
|
} |
297
|
|
|
); |
298
|
|
|
} |
299
|
|
|
|
300
|
40 |
|
protected function CountDaftNestedObjectTreeFromArgs( |
301
|
|
|
? int $left, |
302
|
|
|
? int $right, |
303
|
|
|
? int $limit, |
304
|
|
|
bool $withRoot, |
305
|
|
|
bool $treeNotPath = true |
306
|
|
|
) : int { |
307
|
40 |
|
$sth = $this->QueryDaftNestedObjectTreeFromArgs( |
308
|
40 |
|
false, |
309
|
40 |
|
$left, |
310
|
40 |
|
$right, |
311
|
40 |
|
$limit, |
312
|
40 |
|
$withRoot, |
313
|
40 |
|
$treeNotPath |
314
|
|
|
); |
315
|
|
|
|
316
|
40 |
|
return (int) $sth->fetchColumn(); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|