This commit is contained in:
Julien Fastré 2022-04-29 17:04:55 +02:00
parent fedcf7272c
commit 99413cf328

View File

@ -32,6 +32,7 @@ use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_map; use function array_map;
use function implode; use function implode;
use function in_array; use function in_array;
@ -43,6 +44,8 @@ class PersonDocGenNormalizer implements
{ {
use NormalizerAwareTrait; use NormalizerAwareTrait;
private const CIRCULAR_KEY = 'person:circular';
private PersonRenderInterface $personRender; private PersonRenderInterface $personRender;
private RelationshipRepository $relationshipRepository; private RelationshipRepository $relationshipRepository;
@ -53,8 +56,6 @@ class PersonDocGenNormalizer implements
private TranslatorInterface $translator; private TranslatorInterface $translator;
private const CIRCULAR_KEY = 'person:circular';
public function __construct( public function __construct(
PersonRenderInterface $personRender, PersonRenderInterface $personRender,
RelationshipRepository $relationshipRepository, RelationshipRepository $relationshipRepository,
@ -71,14 +72,13 @@ class PersonDocGenNormalizer implements
public function normalize($person, $format = null, array $context = []) public function normalize($person, $format = null, array $context = [])
{ {
try { try {
$context = $this->addCircularToContext($person, $context); $context = $this->addCircularToContext($person, $context);
} catch (CircularReferenceException $circularReferenceException) { } catch (CircularReferenceException $circularReferenceException) {
return [ return [
'isNull' => true, 'isNull' => true,
'isCircular' => true, 'isCircular' => true,
'text' => '' 'text' => '',
]; ];
} }
@ -192,23 +192,25 @@ class PersonDocGenNormalizer implements
); );
} }
private function addCircularToContext($person, $context) { private function addCircularToContext($person, $context)
{
if (null === $person) { if (null === $person) {
$key = 'n'; $key = 'n';
} else { } else {
$key = spl_object_hash($person); $key = spl_object_hash($person);
} }
if (!\array_key_exists(self::CIRCULAR_KEY, $context)) { if (!array_key_exists(self::CIRCULAR_KEY, $context)) {
$context[self::CIRCULAR_KEY] = [$key]; $context[self::CIRCULAR_KEY] = [$key];
return $context; return $context;
} }
$occurences = array_reduce($context[self::CIRCULAR_KEY], function ($carry, $item) use ($key) { $occurences = array_reduce($context[self::CIRCULAR_KEY], static function ($carry, $item) use ($key) {
if ($key === $item) { if ($key === $item) {
$carry++; ++$carry;
} }
return $carry; return $carry;
}, 0); }, 0);
@ -221,7 +223,6 @@ class PersonDocGenNormalizer implements
return $context; return $context;
} }
private function hasGroup($context, string $group): bool private function hasGroup($context, string $group): bool
{ {
$groups = $context[AbstractNormalizer::GROUPS] ?? []; $groups = $context[AbstractNormalizer::GROUPS] ?? [];