mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
48 lines
1.3 KiB
PHP
48 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\MainBundle\Doctrine\ORM\Hydration;
|
|
|
|
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
|
|
use Generator;
|
|
|
|
final class FlatHierarchyEntityHydrator extends ObjectHydrator
|
|
{
|
|
public const LIST = 'chill_flat_hierarchy_list';
|
|
|
|
protected function hydrateAllData()
|
|
{
|
|
return array_values(iterator_to_array($this->flatListGenerator($this->buildChildrenHashmap(parent::hydrateAllData()))));
|
|
}
|
|
|
|
private function flatListGenerator(array $hashMap, ?object $parent = null): Generator
|
|
{
|
|
$parent = null === $parent ? null : spl_object_id($parent);
|
|
$hashMap += [$parent => []];
|
|
|
|
foreach ($hashMap[$parent] as $node) {
|
|
yield spl_object_id($node) => $node;
|
|
yield from $this->flatListGenerator($hashMap, $node);
|
|
}
|
|
}
|
|
|
|
private function buildChildrenHashmap(array $nodes): array
|
|
{
|
|
return array_reduce(
|
|
$nodes,
|
|
static function (array $collect, $node): array {
|
|
$parentId = (null === $parent = $node->getParent()) ?
|
|
null :
|
|
spl_object_id($parent);
|
|
|
|
$collect[$parentId][] = $node;
|
|
|
|
return $collect;
|
|
},
|
|
[]
|
|
);
|
|
}
|
|
|
|
}
|