ease docgen normlisation for resources

This commit is contained in:
Julien Fastré 2022-03-22 22:37:19 +01:00
parent 89e7eb85ff
commit c5ffca22ff
3 changed files with 45 additions and 13 deletions

View File

@ -26,6 +26,7 @@ use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Household\PersonHouseholdAddress;
use Chill\PersonBundle\Entity\Person\PersonCurrentAddress;
use Chill\PersonBundle\Entity\Person\PersonResource;
use Chill\PersonBundle\Validator\Constraints\Household\HouseholdMembershipSequential;
use Chill\PersonBundle\Validator\Constraints\Person\Birthdate;
use Chill\PersonBundle\Validator\Constraints\Person\PersonHasCenter;
@ -451,6 +452,13 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
*/
private $proxyAccompanyingPeriodOpenState = false; //TO-DELETE ?
/**
* @ORM\OneToMany(targetEntity=PersonResource::class, mappedBy="personOwner")
*
* @var Collection|PersonResource[];
*/
private Collection $resources;
/**
* The person's spoken languages.
*
@ -493,6 +501,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
$this->maritalStatusComment = new CommentEmbeddable();
$this->periodLocatedOn = new ArrayCollection();
$this->accompanyingPeriodRequested = new ArrayCollection();
$this->resources = new ArrayCollection();
}
/**
@ -1301,6 +1310,14 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
return $this->placeOfBirth;
}
/**
* @return PersonResource[]|Collection
*/
public function getResources()
{
return $this->resources;
}
/**
* Get spokenLanguages.
*

View File

@ -19,7 +19,7 @@ use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@ -27,7 +27,7 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity
* @ORM\Table(name="chill_person_resource")
* @DiscriminatorMap(typeProperty="type", mapping={
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "personResource": personResource::class
* })
*/
@ -39,7 +39,7 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
/**
* @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="comment_")
* @Groups({"read", "docgen:read"})
* @Serializer\Groups({"read", "docgen:read"})
*/
private CommentEmbeddable $comment;
@ -67,7 +67,7 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
/**
* The person which host the owner of this resource.
*
* @ORM\ManyToOne(targetEntity=Person::class, inversedBy="personResources")
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=true)
* @Groups({"read", "docgen:read"})
*/
@ -76,7 +76,7 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
/**
* The person linked with this resource.
*
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\ManyToOne(targetEntity=Person::class, inversedBy="resources")
* @ORM\JoinColumn(nullable=false)
* @Groups({"read"})
*/
@ -230,5 +230,10 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface
$context->buildViolation('You must associate at least one entity')
->addViolation();
}
if ($this->person === $this->personOwner) {
$context->buildViolation('You cannot associate a resource with the same person')
->addViolation();
}
}
}

View File

@ -18,7 +18,6 @@ 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;
@ -32,6 +31,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
use function array_map;
use function implode;
use function in_array;
use function is_string;
class PersonDocGenNormalizer implements
ContextAwareNormalizerInterface,
@ -41,8 +42,6 @@ class PersonDocGenNormalizer implements
private PersonRenderInterface $personRender;
private PersonResourceRepository $personResourceRepository;
private RelationshipRepository $relationshipRepository;
private TranslatableStringHelper $translatableStringHelper;
@ -52,13 +51,11 @@ 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;
}
@ -73,9 +70,9 @@ class PersonDocGenNormalizer implements
'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
// when a person reference the same person... take care of circular references
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
return $this->normalize(null, $format, $context);
return $this->normalizer->normalize(null, $format, $context);
},
]);
@ -119,7 +116,7 @@ class PersonDocGenNormalizer implements
'memo' => $person->getMemo(),
'numberOfChildren' => (string) $person->getNumberOfChildren(),
'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext),
'resources' => $this->normalizer->normalize($this->personResourceRepository->findBy(['personOwner' => $person]), $format, $personResourceContext),
'resources' => $this->normalizer->normalize($person->getResources(), $format, $personResourceContext),
];
if ($context['docgen:person:with-household'] ?? false) {
@ -163,6 +160,17 @@ class PersonDocGenNormalizer implements
);
}
private function hasGroup($context, string $group): bool
{
$groups = $context[AbstractNormalizer::GROUPS] ?? [];
if (is_string($groups)) {
$groups = [$groups];
}
return in_array($group, $groups, true);
}
private function normalizeNullValue(string $format, array $context)
{
$normalizer = new NormalizeNullValueHelper($this->normalizer, 'type', 'person');
@ -185,6 +193,8 @@ class PersonDocGenNormalizer implements
$data = $normalizer->normalize($attributes, $format, $context);
$data['resources'] = [];
if ($context['docgen:person:with-relations'] ?? false) {
$data['relations'] = [];
}