-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoHydratorRecursive.php
More file actions
379 lines (362 loc) · 13.7 KB
/
AutoHydratorRecursive.php
File metadata and controls
379 lines (362 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
declare(strict_types=1);
namespace Bancer\NativeQueryMapper\ORM;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\ORM\Association\HasMany;
use Cake\ORM\Association\HasOne;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\BelongsToMany;
class AutoHydratorRecursive
{
/**
* A list of uknown aliases.
*
* @var string[]
*/
private array $unknownAliases = [];
protected Table $rootTable;
/** @var array<string,string[]> SQL alias => fields */
protected array $aliasMap = [];
/** @var array<string,Table> SQL alias => Table instance */
protected array $tableByAlias = [];
/**
* Precomputed mapping strategy.
*
* @var array<string, mixed[]>
*/
protected array $mappingStrategy = [];
/**
* @param Table $rootTable
* @param mixed[] $rows
*/
public function __construct(Table $rootTable, array $rows)
{
$this->rootTable = $rootTable;
$first = $rows[0] ?? [];
if (!is_array($first)) {
throw new \InvalidArgumentException('First element of the result set is not an array');
}
$keys = array_keys($first);
$this->aliasMap = $this->buildAliasMapFromRowKeys($keys);
$allAliases = array_keys($this->aliasMap);
$this->unknownAliases = array_combine($allAliases, $allAliases);
$this->buildMappingStrategy();
}
/**
* @param (int|string)[] $keys
* @return string[][]
*/
protected function buildAliasMapFromRowKeys(array $keys): array
{
$map = [];
foreach ($keys as $k) {
if (!is_string($k)) {
throw new UnknownAliasException('SQL alias is not a string');
}
if (!str_contains($k, '__')) {
continue;
}
[$alias, $field] = explode('__', $k, 2);
$map[$alias][] = $field;
}
return $map;
}
/**
* Precompute mapping strategy and resolve table aliases.
*/
protected function buildMappingStrategy(): void
{
$rootAlias = $this->rootTable->getAlias();
$this->tableByAlias[$rootAlias] = $this->rootTable;
$this->mappingStrategy = [];
foreach ($this->aliasMap as $alias => $_fields) {
if ($alias === $rootAlias) {
continue;
}
$table = $this->resolveTableByAliasRecursive($alias);
if ($table === null) {
throw new UnknownAliasException(
"SQL alias '$alias' does not match any reachable Table from '$rootAlias'."
);
}
$this->tableByAlias[$alias] = $table;
}
$allAliases = array_keys($this->aliasMap);
$aliasesToMap = array_combine($allAliases, $allAliases);
foreach ($this->tableByAlias as $alias => $table) {
if (isset($aliasesToMap[$alias])) {
$this->mappingStrategy[$alias] = [];
foreach ($table->associations() as $assoc) {
$type = null;
if ($assoc instanceof HasOne) {
$type = 'hasOne';
} elseif ($assoc instanceof BelongsTo) {
$type = 'belongsTo';
} elseif ($assoc instanceof BelongsToMany) {
$type = 'belongsToMany';
} elseif ($assoc instanceof HasMany) {
$type = 'hasMany';
}
if ($type === null) {
continue;
}
$childAlias = $assoc->getTarget()->getAlias();
if (!isset($aliasesToMap[$childAlias])) {
continue;
}
$entry = [];
if ($assoc instanceof BelongsToMany) {
$through = $assoc->getThrough();
if ($through === null) {
$through = $assoc->junction();
}
if (is_object($through)) {
$through = $through->getAlias();
}
$entry['through'] = $through;
if (isset($aliasesToMap[$through])) {
unset($aliasesToMap[$through]);
}
}
$entry['property'] = $assoc->getProperty();
$this->mappingStrategy[$alias][$type][$childAlias] = $entry;
unset($aliasesToMap[$childAlias]);
}
}
}
}
protected function resolveTableByAlias(string $alias): ?Table
{
return $this->tableByAlias[$alias] ?? null;
}
/**
* Resolves a table instance based on a given SQL alias.
*
* This method performs a breadth-first search (BFS) starting from the root table
* to find a table that matches the provided alias. It traverses all associations
* (HasOne, HasMany, BelongsTo, BelongsToMany) recursively and also considers
* junction tables for BelongsToMany associations.
*
* For BelongsToMany associations, the junction table is only enqueued if it exists
* and is not already visited. If the alias matches a junction table, the method
* retrieves it from the TableLocator.
*
* @param string $alias The SQL alias to resolve.
* @return \Cake\ORM\Table|null The Table instance corresponding to the alias, or null if not found.
*/
protected function resolveTableByAliasRecursive(string $alias): ?Table
{
$visited = [];
$queue = [$this->rootTable];
while ($queue && !empty($this->unknownAliases)) {
/** @var Table $table */
$table = array_shift($queue);
$visited[$table->getAlias()] = true;
foreach ($table->associations() as $assoc) {
$target = $assoc->getTarget();
$ta = $target->getAlias();
if (isset($this->unknownAliases[$ta])) {
unset($this->unknownAliases[$ta]);
if ($ta === $alias) {
return $target;
}
}
if (!isset($visited[$ta])) {
$queue[] = $target;
}
if ($assoc instanceof BelongsToMany) {
$through = $assoc->getThrough();
if ($through !== null) {
if (is_object($through)) {
$through = $through->getAlias();
}
if (isset($this->unknownAliases[$through])) {
unset($this->unknownAliases[$through]);
if ($through === $alias) {
return TableRegistry::getTableLocator()->get($through);
}
}
if (!isset($visited[$through])) {
$queue[] = TableRegistry::getTableLocator()->get($through);
}
}
}
}
}
return null;
}
/**
* @param mixed[][] $rows
* @return \Cake\Datasource\EntityInterface[]
*/
public function hydrateMany(array $rows): array
{
$results = [];
$rootAlias = $this->rootTable->getAlias();
foreach ($rows as $row) {
$tree = $this->buildEntityRecursive($this->rootTable, $row);
$root = $tree[$rootAlias];
$key = $this->entityKey($root, $this->rootTable);
if (!isset($results[$key])) {
$results[$key] = $root;
} else {
$this->mergeEntityCollections($results[$key], $root);
}
}
return array_values($results);
}
/**
* @param Table $table
* @param mixed[] $row
* @param mixed[] $visited
* @return \Cake\Datasource\EntityInterface[]
*/
protected function buildEntityRecursive(
Table $table,
array $row,
array &$visited = []
): array {
$alias = $table->getAlias();
// prevent infinite recursion
if (isset($visited[$alias])) {
return [];
}
$visited[$alias] = true;
$out = [];
if (!isset($this->aliasMap[$alias])) {
unset($visited[$alias]);
return [];
}
$data = [];
foreach ($this->aliasMap[$alias] as $field) {
$data[$field] = $row["{$alias}__{$field}"] ?? null;
}
$entity = $table->newEntity(
$data,
[
'associated' => [],
'markNew' => false,
'accessibleFields' => ['*' => true],
]
);
$out[$alias] = $entity;
foreach ($this->mappingStrategy[$alias] ?? [] as $type => $children) {
if (is_array($children)) {
foreach ($children as $childAlias => $assocData) {
if (!isset($this->aliasMap[$childAlias])) {
continue;
}
$childTable = $this->tableByAlias[$childAlias];
$tree = $this->buildEntityRecursive($childTable, $row, $visited);
if (!$tree) {
continue;
}
$childEntity = $tree[$childAlias];
if ($type === 'belongsToMany') {
$throughAlias = null;
if (is_array($assocData) && isset($assocData['through'])) {
$throughAlias = $assocData['through'];
}
if (is_string($throughAlias) && isset($this->aliasMap[$throughAlias])) {
$throughTable = $this->tableByAlias[$throughAlias];
$jTree = $this->buildEntityRecursive($throughTable, $row, $visited);
if ($jTree) {
$childEntity->set('_joinData', [$jTree[$throughAlias]]);
$out += $jTree;
}
}
}
$prop = null;
if (is_array($assocData) && isset($assocData['property'])) {
$prop = $assocData['property'];
}
if ($type === 'hasMany' || $type === 'belongsToMany') {
if (!is_string($prop)) {
$prop = $childAlias;
}
$list = $entity->get($prop);
if (!is_array($list)) {
$list = [];
}
$list[] = $childEntity;
$entity->set($prop, $list);
} else {
if (is_string($prop)) {
$entity->set($prop, $childEntity);
}
}
$out += $tree;
}
}
}
unset($visited[$alias]);
return $out;
}
protected function mergeEntityCollections(EntityInterface $into, EntityInterface $from): void
{
foreach ($from->toArray() as $prop => $val) {
if (!is_array($val)) {
continue;
}
$existing = $into->get($prop);
if (!is_array($existing)) {
$existing = [];
}
$merged = array_merge($existing, $val);
$unique = [];
$seen = [];
foreach ($merged as $child) {
if (!$child instanceof Entity) {
$unique[] = $child;
continue;
}
$alias = (string)$child->getSource();
$table = $this->resolveTableByAlias($alias);
if (!$table) {
$oid = spl_object_id($child);
if (!isset($seen[$oid])) {
$seen[$oid] = true;
$unique[] = $child;
}
continue;
}
$pk = $table->getPrimaryKey();
if (is_array($pk)) {
$complexPrimaryKey = function ($p) use ($child) {
/** @var string|null $primaryKeyValue */
$primaryKeyValue = $child->get($p);
return (string)$primaryKeyValue;
};
$value = implode('|', array_map($complexPrimaryKey, $pk));
} else {
/** @var string|null $primaryKeyValue */
$primaryKeyValue = $child->get($pk);
$value = (string)$primaryKeyValue;
}
if (!isset($seen[$value])) {
$seen[$value] = true;
$unique[] = $child;
}
}
$into->set($prop, $unique);
}
}
protected function entityKey(EntityInterface $e, Table $table): string
{
$pk = $table->getPrimaryKey();
if (is_array($pk)) {
$complexPrimaryKey = function ($p) use ($e) {
/** @var string|null $primaryKeyValue */
$primaryKeyValue = $e->get($p);
return (string)($primaryKeyValue ?? '');
};
return implode('|', array_map($complexPrimaryKey, $pk));
}
/** @var string|null $primaryKeyValue */
$primaryKeyValue = $e->get($pk);
return (string)($primaryKeyValue ?? spl_object_id($e));
}
}