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\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_map;
use function implode;
use function in_array;
@ -43,6 +44,8 @@ class PersonDocGenNormalizer implements
{
use NormalizerAwareTrait;
private const CIRCULAR_KEY = 'person:circular';
private PersonRenderInterface $personRender;
private RelationshipRepository $relationshipRepository;
@ -53,8 +56,6 @@ class PersonDocGenNormalizer implements
private TranslatorInterface $translator;
private const CIRCULAR_KEY = 'person:circular';
public function __construct(
PersonRenderInterface $personRender,
RelationshipRepository $relationshipRepository,
@ -71,14 +72,13 @@ class PersonDocGenNormalizer implements
public function normalize($person, $format = null, array $context = [])
{
try {
$context = $this->addCircularToContext($person, $context);
} catch (CircularReferenceException $circularReferenceException) {
return [
'isNull' => 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) {
$key = 'n';
} else {
$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];
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) {
$carry++;
++$carry;
}
return $carry;
}, 0);
@ -221,7 +223,6 @@ class PersonDocGenNormalizer implements
return $context;
}
private function hasGroup($context, string $group): bool
{
$groups = $context[AbstractNormalizer::GROUPS] ?? [];