From 5e0a69310887183d1c434ddbe0a73790c7c2ddf0 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 17 Mar 2022 17:51:25 +0100 Subject: [PATCH 1/5] commentembeddable: add a condition for the early return in normalisation --- .../Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php index bd812e036..f5b10b9d8 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php @@ -39,7 +39,7 @@ class CommentEmbeddableDocGenNormalizer implements ContextAwareNormalizerInterfa */ public function normalize($object, ?string $format = null, array $context = []): array { - if (null === $object) { + if (null === $object or (null === $object->getComment() and null === $object->getUserId() and null === $object->getDate())) { return [ 'comment' => '', 'isNull' => true, From e7299bac4a58edae4e48e7af39b47d7e69304ce6 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 17 Mar 2022 19:07:11 +0100 Subject: [PATCH 2/5] person: add person ressource to person docgen normaliser --- .../Entity/Person/PersonResource.php | 21 +++++++++++++++++++ .../Normalizer/PersonDocGenNormalizer.php | 8 ++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php index 3cd82680e..0eeb6a293 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php @@ -117,6 +117,27 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this->person; } + /** + * @Groups({"read"}) + */ + public function getResourceKind(): string + { + if ($this->getPerson() instanceof Person) { + return 'person'; + } + + if ($this->getThirdParty() instanceof ThirdParty) { + return 'thirdparty'; + } + + if (null !== $this->getFreeText()) { + return 'freetext'; + } + + return 'none'; + } + + public function getPersonOwner(): ?Person { return $this->personOwner; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index c6d132992..c463bfd23 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -18,6 +18,7 @@ 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\PersonResourceRepository; use Chill\PersonBundle\Repository\Relationships\RelationshipRepository; use Chill\PersonBundle\Templating\Entity\PersonRenderInterface; use DateTimeInterface; @@ -41,6 +42,8 @@ class PersonDocGenNormalizer implements private RelationshipRepository $relationshipRepository; + private PersonResourceRepository $personResourceRepository; + private TranslatableStringHelper $translatableStringHelper; private TranslatorInterface $translator; @@ -48,11 +51,13 @@ class PersonDocGenNormalizer implements public function __construct( PersonRenderInterface $personRender, RelationshipRepository $relationshipRepository, + PersonResourceRepository $personResourceRepository, TranslatorInterface $translator, TranslatableStringHelper $translatableStringHelper ) { $this->personRender = $personRender; $this->relationshipRepository = $relationshipRepository; + $this->personResourceRepository = $personResourceRepository; $this->translator = $translator; $this->translatableStringHelper = $translatableStringHelper; } @@ -104,8 +109,9 @@ class PersonDocGenNormalizer implements 'memo' => $person->getMemo(), 'numberOfChildren' => (string) $person->getNumberOfChildren(), 'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext), + 'resources' => $this->personResourceRepository->findBy(['personOwner' => $person]), ]; - + dump($data); if ($context['docgen:person:with-household'] ?? false) { $data['household'] = $this->normalizer->normalize( $person->getCurrentHousehold(), From 3b8c329ff6cc5f80fb86c1282d5bb0bd31a67315 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 17 Mar 2022 19:08:13 +0100 Subject: [PATCH 3/5] upd CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff62b029c..b84ec4e80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to ## Unreleased +* [person] add person ressource to person docgen normaliser (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/517) * [person] AccompanyingPeriodWorkEvaluation: fix circular reference when serialising (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/495) * [person] order accompanying period by opening date in search persons, person and household period lists (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/493) * [parcours] autosave of the pinned comment for draft accompanying course (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/477) From 37a8928e41d3b57921b6fb5c7b6fe49f807a68a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 21 Mar 2022 16:19:27 +0100 Subject: [PATCH 4/5] remove dump --- .../Serializer/Normalizer/PersonDocGenNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index c463bfd23..c68062b21 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -111,7 +111,7 @@ class PersonDocGenNormalizer implements 'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext), 'resources' => $this->personResourceRepository->findBy(['personOwner' => $person]), ]; - dump($data); + if ($context['docgen:person:with-household'] ?? false) { $data['household'] = $this->normalizer->normalize( $person->getCurrentHousehold(), From 169d4dc41cde0bb90f837b581d784bcf96944848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 21 Mar 2022 17:33:01 +0100 Subject: [PATCH 5/5] finalize normalization on ressources --- .../Entity/Embeddable/CommentEmbeddable.php | 2 +- .../CommentEmbeddableDocGenNormalizer.php | 2 +- .../Tests/Controller/UserControllerTest.php | 4 ++- .../Entity/Person/PersonResource.php | 30 +++++++++++-------- .../Entity/Person/PersonResourceKind.php | 8 +++-- .../Normalizer/PersonDocGenNormalizer.php | 16 ++++++++-- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Entity/Embeddable/CommentEmbeddable.php b/src/Bundle/ChillMainBundle/Entity/Embeddable/CommentEmbeddable.php index 61ebce10b..a29ad8f29 100644 --- a/src/Bundle/ChillMainBundle/Entity/Embeddable/CommentEmbeddable.php +++ b/src/Bundle/ChillMainBundle/Entity/Embeddable/CommentEmbeddable.php @@ -61,7 +61,7 @@ class CommentEmbeddable public function isEmpty() { - return empty($this->getComment()); + return null === $this->getComment() || '' === $this->getComment(); } public function setComment(?string $comment) diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php index f5b10b9d8..10f43a2f6 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/CommentEmbeddableDocGenNormalizer.php @@ -39,7 +39,7 @@ class CommentEmbeddableDocGenNormalizer implements ContextAwareNormalizerInterfa */ public function normalize($object, ?string $format = null, array $context = []): array { - if (null === $object or (null === $object->getComment() and null === $object->getUserId() and null === $object->getDate())) { + if (null === $object || ($object->isEmpty())) { return [ 'comment' => '', 'isNull' => true, diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php index 8a3c355b0..42f541d56 100644 --- a/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php @@ -101,7 +101,9 @@ final class UserControllerTest extends WebTestCase // Check data in the show view $this->assertStringContainsString( - "Test_user", $crawler->text(), "page contains the name of the user" + 'Test_user', + $crawler->text(), + 'page contains the name of the user' ); //test the auth of the new client diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php index 0eeb6a293..1d2343067 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php @@ -39,13 +39,13 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="comment_") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private CommentEmbeddable $comment; /** * @ORM\Column(type="text", nullable=true) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?string $freeText = null; @@ -53,24 +53,29 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") + * @Groups({"read", "docgen:read"}) */ private ?int $id; /** * @ORM\ManyToOne(targetEntity=PersonResourceKind::class, inversedBy="personResources") * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ - private $kind; + private ?PersonResourceKind $kind = null; /** + * The person which host the owner of this resource. + * * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="personResources") * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?Person $person = null; /** + * The person linked with this resource. + * * @ORM\ManyToOne(targetEntity=Person::class) * @ORM\JoinColumn(nullable=false) * @Groups({"read"}) @@ -80,7 +85,7 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\ManyToOne(targetEntity=ThirdParty::class, inversedBy="personResources") * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?ThirdParty $thirdParty = null; @@ -117,8 +122,13 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this->person; } + public function getPersonOwner(): ?Person + { + return $this->personOwner; + } + /** - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ public function getResourceKind(): string { @@ -137,12 +147,6 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return 'none'; } - - public function getPersonOwner(): ?Person - { - return $this->personOwner; - } - public function getThirdParty(): ?ThirdParty { return $this->thirdParty; diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php index 4e65fda9f..f450997fa 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php @@ -12,10 +12,9 @@ declare(strict_types=1); namespace Chill\PersonBundle\Entity\Person; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Annotation as Serializer; /** - * **About denormalization**: this operation is operated by @see{AccompanyingPeriodResourdeNormalizer}. - * * @ORM\Entity * @ORM\Table(name="chill_person_resource_kind") */ @@ -25,8 +24,9 @@ class PersonResourceKind * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") + * @Serializer\Groups({"docgen:read"}) */ - private int $id; + private ?int $id = null; /** * @ORM\Column(type="boolean") @@ -35,6 +35,8 @@ class PersonResourceKind /** * @ORM\Column(type="json", length=255) + * @Serializer\Groups({"docgen:read"}) + * @Serializer\Context({"is-translatable": true}, groups={"docgen:read"}) */ private array $title; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index c68062b21..fb325ec4e 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -24,6 +24,7 @@ use Chill\PersonBundle\Templating\Entity\PersonRenderInterface; use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Serializer\Exception\UnexpectedValueException; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -40,10 +41,10 @@ class PersonDocGenNormalizer implements private PersonRenderInterface $personRender; - private RelationshipRepository $relationshipRepository; - private PersonResourceRepository $personResourceRepository; + private RelationshipRepository $relationshipRepository; + private TranslatableStringHelper $translatableStringHelper; private TranslatorInterface $translator; @@ -68,6 +69,15 @@ class PersonDocGenNormalizer implements $dateContext = $context; $dateContext['docgen:expects'] = DateTimeInterface::class; $addressContext = array_merge($context, ['docgen:expects' => Address::class]); + $personResourceContext = array_merge($context, [ + 'docgen:expects' => Person\PersonResource::class, + // we simplify the list of attributes for the embedded persons + AbstractNormalizer::GROUPS => ['docgen:read'], + // when a person reference the same person... take care of circulare references + AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) { + return $this->normalize(null, $format, $context); + }, + ]); if (null === $person) { return $this->normalizeNullValue($format, $context); @@ -109,7 +119,7 @@ class PersonDocGenNormalizer implements 'memo' => $person->getMemo(), 'numberOfChildren' => (string) $person->getNumberOfChildren(), 'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext), - 'resources' => $this->personResourceRepository->findBy(['personOwner' => $person]), + 'resources' => $this->normalizer->normalize($this->personResourceRepository->findBy(['personOwner' => $person]), $format, $personResourceContext), ]; if ($context['docgen:person:with-household'] ?? false) {