Merge branch '167-social-issue-recursive-list' into 'master'

[API] Social Issue recursive sort

See merge request Chill-Projet/chill-bundles!99
This commit is contained in:
2021-08-05 09:49:13 +00:00
3 changed files with 75 additions and 3 deletions

View File

@@ -32,9 +32,8 @@ use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray;
use Chill\MainBundle\Doctrine\DQL\Similarity;
use Chill\MainBundle\Doctrine\DQL\OverlapsI;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Chill\MainBundle\Doctrine\DQL\Replace;
use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator;
use Chill\MainBundle\Doctrine\Type\NativeDateIntervalType;
use Chill\MainBundle\Doctrine\Type\PointType;
use Symfony\Component\HttpFoundation\Request;
@@ -186,6 +185,9 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
'OVERLAPSI' => OverlapsI::class,
],
],
'hydrators' => [
'chill_flat_hierarchy_list' => FlatHierarchyEntityHydrator::class,
],
],
],
);

View File

@@ -0,0 +1,47 @@
<?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;
},
[]
);
}
}