finalize normalization on ressources

This commit is contained in:
Julien Fastré 2022-03-21 17:33:01 +01:00
parent b4add2de95
commit 169d4dc41c
6 changed files with 40 additions and 22 deletions

View File

@ -61,7 +61,7 @@ class CommentEmbeddable
public function isEmpty() public function isEmpty()
{ {
return empty($this->getComment()); return null === $this->getComment() || '' === $this->getComment();
} }
public function setComment(?string $comment) public function setComment(?string $comment)

View File

@ -39,7 +39,7 @@ class CommentEmbeddableDocGenNormalizer implements ContextAwareNormalizerInterfa
*/ */
public function normalize($object, ?string $format = null, array $context = []): array 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 [ return [
'comment' => '', 'comment' => '',
'isNull' => true, 'isNull' => true,

View File

@ -101,7 +101,9 @@ final class UserControllerTest extends WebTestCase
// Check data in the show view // Check data in the show view
$this->assertStringContainsString( $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 //test the auth of the new client

View File

@ -39,13 +39,13 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
/** /**
* @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="comment_") * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="comment_")
* @Groups({"read"}) * @Groups({"read", "docgen:read"})
*/ */
private CommentEmbeddable $comment; private CommentEmbeddable $comment;
/** /**
* @ORM\Column(type="text", nullable=true) * @ORM\Column(type="text", nullable=true)
* @Groups({"read"}) * @Groups({"read", "docgen:read"})
*/ */
private ?string $freeText = null; private ?string $freeText = null;
@ -53,24 +53,29 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* @Groups({"read", "docgen:read"})
*/ */
private ?int $id; private ?int $id;
/** /**
* @ORM\ManyToOne(targetEntity=PersonResourceKind::class, inversedBy="personResources") * @ORM\ManyToOne(targetEntity=PersonResourceKind::class, inversedBy="personResources")
* @ORM\JoinColumn(nullable=true) * @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\ManyToOne(targetEntity=Person::class, inversedBy="personResources")
* @ORM\JoinColumn(nullable=true) * @ORM\JoinColumn(nullable=true)
* @Groups({"read"}) * @Groups({"read", "docgen:read"})
*/ */
private ?Person $person = null; private ?Person $person = null;
/** /**
* The person linked with this resource.
*
* @ORM\ManyToOne(targetEntity=Person::class) * @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=false) * @ORM\JoinColumn(nullable=false)
* @Groups({"read"}) * @Groups({"read"})
@ -80,7 +85,7 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
/** /**
* @ORM\ManyToOne(targetEntity=ThirdParty::class, inversedBy="personResources") * @ORM\ManyToOne(targetEntity=ThirdParty::class, inversedBy="personResources")
* @ORM\JoinColumn(nullable=true) * @ORM\JoinColumn(nullable=true)
* @Groups({"read"}) * @Groups({"read", "docgen:read"})
*/ */
private ?ThirdParty $thirdParty = null; private ?ThirdParty $thirdParty = null;
@ -117,8 +122,13 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
return $this->person; return $this->person;
} }
public function getPersonOwner(): ?Person
{
return $this->personOwner;
}
/** /**
* @Groups({"read"}) * @Groups({"read", "docgen:read"})
*/ */
public function getResourceKind(): string public function getResourceKind(): string
{ {
@ -137,12 +147,6 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
return 'none'; return 'none';
} }
public function getPersonOwner(): ?Person
{
return $this->personOwner;
}
public function getThirdParty(): ?ThirdParty public function getThirdParty(): ?ThirdParty
{ {
return $this->thirdParty; return $this->thirdParty;

View File

@ -12,10 +12,9 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Entity\Person; namespace Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\Mapping as ORM; 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\Entity
* @ORM\Table(name="chill_person_resource_kind") * @ORM\Table(name="chill_person_resource_kind")
*/ */
@ -25,8 +24,9 @@ class PersonResourceKind
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* @Serializer\Groups({"docgen:read"})
*/ */
private int $id; private ?int $id = null;
/** /**
* @ORM\Column(type="boolean") * @ORM\Column(type="boolean")
@ -35,6 +35,8 @@ class PersonResourceKind
/** /**
* @ORM\Column(type="json", length=255) * @ORM\Column(type="json", length=255)
* @Serializer\Groups({"docgen:read"})
* @Serializer\Context({"is-translatable": true}, groups={"docgen:read"})
*/ */
private array $title; private array $title;

View File

@ -24,6 +24,7 @@ use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
@ -40,10 +41,10 @@ class PersonDocGenNormalizer implements
private PersonRenderInterface $personRender; private PersonRenderInterface $personRender;
private RelationshipRepository $relationshipRepository;
private PersonResourceRepository $personResourceRepository; private PersonResourceRepository $personResourceRepository;
private RelationshipRepository $relationshipRepository;
private TranslatableStringHelper $translatableStringHelper; private TranslatableStringHelper $translatableStringHelper;
private TranslatorInterface $translator; private TranslatorInterface $translator;
@ -68,6 +69,15 @@ class PersonDocGenNormalizer implements
$dateContext = $context; $dateContext = $context;
$dateContext['docgen:expects'] = DateTimeInterface::class; $dateContext['docgen:expects'] = DateTimeInterface::class;
$addressContext = array_merge($context, ['docgen:expects' => Address::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) { if (null === $person) {
return $this->normalizeNullValue($format, $context); return $this->normalizeNullValue($format, $context);
@ -109,7 +119,7 @@ class PersonDocGenNormalizer implements
'memo' => $person->getMemo(), 'memo' => $person->getMemo(),
'numberOfChildren' => (string) $person->getNumberOfChildren(), 'numberOfChildren' => (string) $person->getNumberOfChildren(),
'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext), '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) { if ($context['docgen:person:with-household'] ?? false) {