mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-07 23:34:58 +00:00
docgen normalization for relation
This commit is contained in:
@@ -16,6 +16,8 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\PersonAltName;
|
||||
use Chill\PersonBundle\Repository\Relationships\RelationRepository;
|
||||
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use DateTimeInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||
@@ -34,16 +36,20 @@ class PersonDocGenNormalizer implements
|
||||
|
||||
private PersonRender $personRender;
|
||||
|
||||
private RelationshipRepository $relationshipRepository;
|
||||
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
PersonRender $personRender,
|
||||
RelationshipRepository $relationshipRepository,
|
||||
TranslatorInterface $translator,
|
||||
TranslatableStringHelper $translatableStringHelper
|
||||
) {
|
||||
$this->personRender = $personRender;
|
||||
$this->relationshipRepository = $relationshipRepository;
|
||||
$this->translator = $translator;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
@@ -94,6 +100,18 @@ class PersonDocGenNormalizer implements
|
||||
);
|
||||
}
|
||||
|
||||
if ($context['docgen:person:with-relations'] ?? false) {
|
||||
$data['relations'] = $this->normalizer->normalize(
|
||||
$this->relationshipRepository->findByPerson($person),
|
||||
$format,
|
||||
array_merge($context, [
|
||||
'docgen:person:with-household' => false,
|
||||
'docgen:person:with-relation' => false,
|
||||
'docgen:relationship:counterpart' => $person
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\PersonBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relation;
|
||||
use Chill\PersonBundle\Entity\Relationships\Relationship;
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\ExceptionInterface;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
private TranslatableStringHelperInterface $translatableStringHelper;
|
||||
|
||||
public function __construct(TranslatableStringHelperInterface $translatableStringHelper)
|
||||
{
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Relationship $relation
|
||||
*/
|
||||
public function normalize($relation, string $format = null, array $context = [])
|
||||
{
|
||||
$counterpart = $context['docgen:relationship:counterpart'] ?? null;
|
||||
$contextPerson = array_merge($context, [
|
||||
'docgen:person:with-relation' => false,
|
||||
'docgen:relationship:counterpart' => null,
|
||||
'docgen:expects' => Person::class,
|
||||
]);
|
||||
|
||||
if (null !== $counterpart) {
|
||||
$opposite = $relation->getOpposite($counterpart);
|
||||
} else {
|
||||
$opposite = null;
|
||||
}
|
||||
|
||||
if (null === $relation) {
|
||||
return [
|
||||
"id" => "",
|
||||
"fromPerson" => $nullPerson = $this->normalizer->normalize(null, $format, $contextPerson),
|
||||
'toPerson' => $nullPerson,
|
||||
'opposite' => $nullPerson,
|
||||
'text' => '',
|
||||
'relationId' => '',
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $relation->getId(),
|
||||
'fromPerson' => $this->normalizer->normalize(
|
||||
$relation->getFromPerson(),
|
||||
$format,
|
||||
$contextPerson
|
||||
),
|
||||
'toPerson' => $this->normalizer->normalize(
|
||||
$relation->getToPerson(),
|
||||
$format,
|
||||
$contextPerson
|
||||
),
|
||||
'text' => $relation->getReverse() ?
|
||||
$this->translatableStringHelper->localize($relation->getRelation()->getReverseTitle()) :
|
||||
$this->translatableStringHelper->localize($relation->getRelation()->getTitle()),
|
||||
'opposite' => $this->normalizer->normalize($opposite, $format, $contextPerson),
|
||||
'relationId' => $relation->getRelation()->getId(),
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, string $format = null, array $context = [])
|
||||
{
|
||||
if ('docgen' !== $format) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data instanceof Relationship || (null === $data
|
||||
&& ($context['docgen:expects'] ?? null) === Relationship::class);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user