mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 14:25:00 +00:00
Add Doctrine extension.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
<?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(): array
|
||||
{
|
||||
$generator = $this->buildRecursively(
|
||||
parent::hydrateAllData(),
|
||||
static fn($value): iterable => $value->getChildren(),
|
||||
);
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($generator as $entity) {
|
||||
// We cannot expect anything from the object entity
|
||||
// so, we cannot expect it to have a ::getId() method.
|
||||
// So we use spl_object_hash() instead.
|
||||
$result += [spl_object_hash($entity) => $entity];
|
||||
}
|
||||
|
||||
return array_values($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<int, SocialIssue> $iterable
|
||||
* @param callable(SocialIssue): iterable<int, SocialIssue> $childrenAccessor
|
||||
*
|
||||
* @return Generator<int, SocialIssue>
|
||||
*/
|
||||
private function buildRecursively(iterable $iterable, callable $childrenAccessor): Generator
|
||||
{
|
||||
foreach ($iterable as $item)
|
||||
{
|
||||
yield $item;
|
||||
|
||||
yield from $this->buildRecursively($childrenAccessor($item), $childrenAccessor);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user