mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
[person] prevent circular references in PersonDocGenNormalizer
This commit is contained in:
@@ -23,6 +23,7 @@ use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\DataFixtures\Exception\CircularReferenceException;
|
||||
use libphonenumber\PhoneNumber;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
@@ -52,6 +53,8 @@ class PersonDocGenNormalizer implements
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
private const CIRCULAR_KEY = 'person:circular';
|
||||
|
||||
public function __construct(
|
||||
PersonRenderInterface $personRender,
|
||||
RelationshipRepository $relationshipRepository,
|
||||
@@ -68,6 +71,17 @@ 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' => ''
|
||||
];
|
||||
}
|
||||
|
||||
/** @var Person $person */
|
||||
$dateContext = $context;
|
||||
$dateContext['docgen:expects'] = DateTimeInterface::class;
|
||||
@@ -178,6 +192,36 @@ class PersonDocGenNormalizer implements
|
||||
);
|
||||
}
|
||||
|
||||
private function addCircularToContext($person, $context) {
|
||||
if (null === $person) {
|
||||
$key = 'n';
|
||||
} else {
|
||||
$key = spl_object_hash($person);
|
||||
}
|
||||
|
||||
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) {
|
||||
if ($key === $item) {
|
||||
$carry++;
|
||||
}
|
||||
return $carry;
|
||||
}, 0);
|
||||
|
||||
if (2 <= $occurences) {
|
||||
throw new CircularReferenceException();
|
||||
}
|
||||
|
||||
$context[self::CIRCULAR_KEY][] = $key;
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
|
||||
private function hasGroup($context, string $group): bool
|
||||
{
|
||||
$groups = $context[AbstractNormalizer::GROUPS] ?? [];
|
||||
|
Reference in New Issue
Block a user