Merge remote-tracking branch 'origin/master' into issue230_person

This commit is contained in:
nobohan
2022-01-04 13:50:27 +01:00
611 changed files with 13079 additions and 4316 deletions

View File

@@ -0,0 +1,188 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Resolver\ScopeResolverDispatcher;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Templating\Entity\ClosingMotiveRender;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTime;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
use function is_array;
class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private const IGNORE_FIRST_PASS_KEY = 'acc_period_ignore_first_pass';
private const PERIOD_NULL = [
'id',
'closingDate' => DateTime::class,
'confidential',
'confidentialText',
'createdAt' => DateTime::class,
'createdBy' => User::class,
'emergency',
'emergencyText',
'openingDate' => DateTime::class,
'origin' => AccompanyingPeriod\Origin::class,
'originText',
'requestorAnonymous',
'socialIssues',
'intensity',
'step',
'closingMotiveText',
'socialIssuesText',
'scopes' => Collection::class,
'scopesText',
'ref' => User::class,
'participations' => Collection::class,
'currentParticipations' => Collection::class,
'requestorPerson' => Person::class,
'requestorThirdParty' => ThirdParty::class,
'resources' => Collection::class,
];
private ClosingMotiveRender $closingMotiveRender;
private ScopeResolverDispatcher $scopeResolverDispatcher;
private SocialIssueRender $socialIssueRender;
private TranslatableStringHelper $translatableStringHelper;
private TranslatorInterface $translator;
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper,
SocialIssueRender $socialIssueRender,
ClosingMotiveRender $closingMotiveRender,
ScopeResolverDispatcher $scopeResolverDispatcher
) {
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->socialIssueRender = $socialIssueRender;
$this->closingMotiveRender = $closingMotiveRender;
$this->scopeResolverDispatcher = $scopeResolverDispatcher;
}
/**
* @param AccompanyingPeriod|null $period
* @param null|string $format
*/
public function normalize($period, $format = null, array $context = [])
{
if ($period instanceof AccompanyingPeriod) {
$scopes = $this->scopeResolverDispatcher->isConcerned($period) ? $this->scopeResolverDispatcher->resolveScope($period) : [];
if (!is_array($scopes)) {
$scopes = [$scopes];
}
$dateContext = array_merge($context, ['docgen:expects' => DateTime::class, 'groups' => 'docgen:read']);
$userContext = array_merge($context, ['docgen:expects' => User::class, 'groups' => 'docgen:read']);
$participationContext = array_merge($context, ['docgen:expects' => AccompanyingPeriodParticipation::class, 'groups' => 'docgen:read']);
return [
'id' => $period->getId(),
'type' => 'accompanying_period',
'isNull' => false,
'closingDate' => $this->normalizer->normalize($period->getClosingDate(), $format, $dateContext),
'confidential' => $period->isConfidential(),
'createdAt' => $this->normalizer->normalize($period->getCreatedAt(), $format, $dateContext),
'createdBy' => $this->normalizer->normalize($period->getCreatedBy(), $format, $userContext),
'emergency' => $period->isEmergency(),
'openingDate' => $this->normalizer->normalize($period->getOpeningDate(), $format, $dateContext),
'origin' => $this->normalizer->normalize($period->getOrigin(), $format, array_merge($context, ['docgen:expects' => AccompanyingPeriod\Origin::class])),
'participations' => $this->normalizer->normalize($period->getParticipations(), $format, $participationContext),
'currentParticipations' => $this->normalizer->normalize($period->getCurrentParticipations(), $format, $participationContext),
'requestorAnonymous' => $period->isRequestorAnonymous(),
'requestorPerson' => $this->normalizer->normalize($period->getRequestorPerson(), $format, array_merge($context, ['docgen:expects' => Person::class])),
'hasRequestorPerson' => $period->getRequestorPerson() !== null,
'requestorThirdParty' => $this->normalizer->normalize($period->getRequestorThirdParty(), $format, array_merge($context, ['docgen:expects' => ThirdParty::class])),
'hasRequestorThirdParty' => $period->getRequestorThirdParty() !== null,
'resources' => $this->normalizer->normalize($period->getResources(), $format, $context),
'scopes' => $this->normalizer->normalize($scopes, $format, array_merge($context, ['docgen:expects' => Scope::class, 'groups' => 'docgen:read'])),
'socialIssues' => $this->normalizer->normalize($period->getSocialIssues(), $format, $context),
'intensity' => $this->translator->trans($period->getIntensity()),
'step' => $this->translator->trans('accompanying_period.' . $period->getStep()),
'emergencyText' => $period->isEmergency() ? $this->translator->trans('accompanying_period.emergency') : '',
'confidentialText' => $period->isConfidential() ? $this->translator->trans('confidential') : '',
'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '',
'isClosed' => $period->getClosingDate() !== null,
'closingMotiveText' => null !== $period->getClosingMotive() ?
$this->closingMotiveRender->renderString($period->getClosingMotive(), []) : '',
'ref' => $this->normalizer->normalize($period->getUser(), $format, $userContext),
'hasRef' => $period->getUser() !== null,
'socialIssuesText' => implode(', ', array_map(function (SocialIssue $s) {
return $this->socialIssueRender->renderString($s, []);
}, $period->getSocialIssues()->toArray())),
'scopesText' => implode(', ', array_map(function (Scope $s) {
return $this->translatableStringHelper->localize($s->getName());
}, $scopes)),
'hasRequestor' => $period->getRequestor() !== null,
'requestorKind' => $period->getRequestorKind(),
];
}
if (null === $period) {
return array_merge(
(new NormalizeNullValueHelper($this->normalizer, 'type', 'accompanying_period'))
->normalize(self::PERIOD_NULL, $format, $context),
[
'hasRef' => false,
'requestorKind' => 'none',
'hasRequestor' => false,
'hasRequestorPerson' => false,
'hasRequestorThirdParty' => false,
'isClosed' => false,
'confidential' => false,
]
);
}
throw new InvalidArgumentException('This neither an accompanying period or null.');
}
public function supportsNormalization($data, $format = null, array $context = []): bool
{
if ('docgen' !== $format) {
return false;
}
if ($data instanceof AccompanyingPeriod) {
return true;
}
if (null === $data && AccompanyingPeriod::class === ($context['docgen:expects'] ?? null)) {
return true;
}
return false;
}
}

View File

@@ -19,9 +19,12 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
*/
final class AccompanyingPeriodOriginNormalizer implements NormalizerInterface
{
public function normalize($origin, ?string $format = null, array $context = [])
/**
* @param Origin $origin
* @param null|string $format
*/
public function normalize($origin, $format = null, array $context = [])
{
/** @var Origin $origin */
return [
'type' => 'origin',
'id' => $origin->getId(),
@@ -30,7 +33,7 @@ final class AccompanyingPeriodOriginNormalizer implements NormalizerInterface
];
}
public function supportsNormalization($data, ?string $format = null): bool
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof Origin;
}

View File

@@ -17,11 +17,14 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class AccompanyingPeriodParticipationNormalizer implements NormalizerAwareInterface, NormalizerInterface
{
protected ?NormalizerInterface $normalizer = null;
private ?NormalizerInterface $normalizer = null;
public function normalize($participation, ?string $format = null, array $context = [])
/**
* @param AccompanyingPeriodParticipation $participation
* @param null|string $format
*/
public function normalize($participation, $format = null, array $context = [])
{
/** @var AccompanyingPeriodParticipation $participation */
return [
'id' => $participation->getId(),
'startDate' => $this->normalizer->normalize($participation->getStartDate(), $format),
@@ -35,8 +38,9 @@ class AccompanyingPeriodParticipationNormalizer implements NormalizerAwareInterf
$this->normalizer = $normalizer;
}
public function supportsNormalization($data, ?string $format = null): bool
public function supportsNormalization($data, $format = null): bool
{
// @TODO Fix this.
return false;
return $data instanceof AccompanyingPeriodParticipation;

View File

@@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use function array_key_exists;
use function array_merge;
use function count;
@@ -37,7 +38,7 @@ class AccompanyingPeriodResourceNormalizer implements DenormalizerAwareInterface
$this->repository = $repository;
}
public function denormalize($data, string $type, ?string $format = null, array $context = [])
public function denormalize($data, $type, $format = null, array $context = [])
{
$resource = $this->extractObjectToPopulate($type, $context);
@@ -86,7 +87,7 @@ class AccompanyingPeriodResourceNormalizer implements DenormalizerAwareInterface
return $resource;
}
public function supportsDenormalization($data, string $type, ?string $format = null)
public function supportsDenormalization($data, $type, $format = null)
{
return Resource::class === $type;
}

View File

@@ -20,6 +20,7 @@ use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use function array_key_exists;
use function array_merge;
use function in_array;
@@ -50,7 +51,7 @@ class AccompanyingPeriodWorkDenormalizer implements ContextAwareDenormalizerInte
$this->em = $em;
}
public function denormalize($data, string $type, ?string $format = null, array $context = [])
public function denormalize($data, $type, $format = null, array $context = [])
{
$work = $this->denormalizer->denormalize($data, $type, $format, array_merge(
$context,
@@ -64,7 +65,7 @@ class AccompanyingPeriodWorkDenormalizer implements ContextAwareDenormalizerInte
return $work;
}
public function supportsDenormalization($data, string $type, ?string $format = null, array $context = []): bool
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
return AccompanyingPeriodWork::class === $type
&& self::class !== ($context['skip'] ?? null)

View File

@@ -21,6 +21,7 @@ use Symfony\Component\Serializer\Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use function array_key_exists;
class MembersEditorNormalizer implements DenormalizerAwareInterface, DenormalizerInterface
@@ -34,7 +35,7 @@ class MembersEditorNormalizer implements DenormalizerAwareInterface, Denormalize
$this->factory = $factory;
}
public function denormalize($data, string $type, ?string $format = null, array $context = [])
public function denormalize($data, $type, $format = null, array $context = [])
{
// some test about schema first...
$this->performChecks($data);
@@ -49,7 +50,7 @@ class MembersEditorNormalizer implements DenormalizerAwareInterface, Denormalize
return $this->denormalizeMove($data, $type, $format, $context);
}
public function supportsDenormalization($data, string $type, ?string $format = null)
public function supportsDenormalization($data, $type, $format = null)
{
return MembersEditor::class === $type;
}
@@ -152,8 +153,10 @@ class MembersEditorNormalizer implements DenormalizerAwareInterface, Denormalize
private function performChecks($data): void
{
if (null === $data['concerned'] ?? null
&& false === ·\is_array('concerned')) {
if (
null === $data['concerned'] ?? null
&& false === ·\is_array('concerned')
) {
throw new Exception\UnexpectedValueException("The schema does not have any key 'concerned'");
}

View File

@@ -12,16 +12,22 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\Civility;
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\RelationshipRepository;
use Chill\PersonBundle\Templating\Entity\PersonRender;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_map;
use function implode;
@@ -33,31 +39,43 @@ 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;
}
public function normalize($person, ?string $format = null, array $context = [])
public function normalize($person, $format = null, array $context = [])
{
/** @var Person $person */
$dateContext = $context;
$dateContext['docgen:expects'] = DateTimeInterface::class;
$addressContext = array_merge($context, ['docgen:expects' => Address::class]);
if (null === $person) {
return $this->normalizeNullValue($format, $context);
}
return [
if (!$person instanceof Person) {
throw new UnexpectedValueException();
}
$data = [
'type' => 'person',
'isNull' => false,
'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expects' => Civility::class])),
'firstname' => $person->getFirstName(),
'lastname' => $person->getLastName(),
'altNames' => implode(
@@ -70,6 +88,7 @@ class PersonDocGenNormalizer implements
)
),
'text' => $this->personRender->renderString($person, []),
'age' => (int) $person->getAge(),
'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext),
'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext),
'gender' => $this->translator->trans($person->getGender()),
@@ -83,10 +102,37 @@ class PersonDocGenNormalizer implements
'placeOfBirth' => $person->getPlaceOfBirth(),
'memo' => $person->getMemo(),
'numberOfChildren' => (string) $person->getNumberOfChildren(),
'address' => $this->normalizer->normalize($person->getCurrentPersonAddress(), $format, $addressContext),
];
if ($context['docgen:person:with-household'] ?? false) {
$data['household'] = $this->normalizer->normalize(
$person->getCurrentHousehold(),
$format,
array_merge($context, [
'docgen:expects' => Household::class,
'docgen:person:with-household' => false,
'docgen:person:with-relations' => false,
])
);
}
if ($context['docgen:person:with-relations'] ?? false) {
$data['relations'] = $this->normalizer->normalize(
new ArrayCollection($this->relationshipRepository->findByPerson($person)),
$format,
array_merge($context, [
'docgen:person:with-household' => false,
'docgen:person:with-relation' => false,
'docgen:relationship:counterpart' => $person,
])
);
}
return $data;
}
public function supportsNormalization($data, ?string $format = null, array $context = [])
public function supportsNormalization($data, $format = null, array $context = [])
{
if ('docgen' !== $format) {
return false;
@@ -95,25 +141,37 @@ class PersonDocGenNormalizer implements
return
$data instanceof Person
|| (
array_key_exists('docgen:expects', $context)
&& Person::class === $context['docgen:expects']
null === $data
&& Person::class === ($context['docgen:expects'] ?? null)
);
}
private function normalizeNullValue(string $format, array $context)
{
$normalizer = new NormalizeNullValueHelper($this->normalizer);
$normalizer = new NormalizeNullValueHelper($this->normalizer, 'type', 'person');
$attributes = [
'firstname', 'lastname', 'altNames', 'text',
'civility' => Civility::class,
'birthdate' => DateTimeInterface::class,
'deathdate' => DateTimeInterface::class,
'gender', 'maritalStatus',
'maritalStatusDate' => DateTimeInterface::class,
'email', 'firstPhoneNumber', 'fixPhoneNumber', 'mobilePhoneNumber', 'nationality',
'placeOfBirth', 'memo', 'numberOfChildren',
'address' => Address::class,
];
return $normalizer->normalize($attributes, $format, $context);
if ($context['docgen:person:with-household'] ?? false) {
$attributes['household'] = Household::class;
}
$data = $normalizer->normalize($attributes, $format, $context);
if ($context['docgen:person:with-relations'] ?? false) {
$data['relations'] = [];
}
return $data;
}
}

View File

@@ -14,11 +14,11 @@ namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\PersonAltName;
use Chill\PersonBundle\Repository\PersonRepository;
use DateTime;
use DateTimeImmutable;
use LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
@@ -59,11 +59,11 @@ class PersonJsonNormalizer implements
$this->centerResolverManager = $centerResolverManager;
}
public function denormalize($data, string $type, ?string $format = null, array $context = [])
public function denormalize($data, $type, $format = null, array $context = [])
{
$person = $this->extractObjectToPopulate($type, $context);
if (array_key_exists('id', $data)) {
if (array_key_exists('id', $data) && null === $person) {
$person = $this->repository->find($data['id']);
if (null === $person) {
@@ -115,18 +115,14 @@ class PersonJsonNormalizer implements
case 'birthdate':
$object = $this->denormalizer->denormalize($data[$item], DateTime::class, $format, $context);
if ($object instanceof DateTime) {
$person->setBirthdate($object);
}
$person->setBirthdate($object);
break;
case 'deathdate':
$object = $this->denormalizer->denormalize($data[$item], DateTime::class, $format, $context);
$object = $this->denormalizer->denormalize($data[$item], DateTimeImmutable::class, $format, $context);
if ($object instanceof DateTime) {
$person->setDeathdate($object);
}
$person->setDeathdate($object);
break;
@@ -158,12 +154,14 @@ class PersonJsonNormalizer implements
return $person;
}
public function normalize($person, ?string $format = null, array $context = [])
/**
* @param Person $person
* @param null|string $format
*/
public function normalize($person, $format = null, array $context = [])
{
/** @var Household $household */
$household = $person->getCurrentHousehold();
/** @var Person $person */
return [
'type' => 'person',
'id' => $person->getId(),
@@ -182,12 +180,12 @@ class PersonJsonNormalizer implements
];
}
public function supportsDenormalization($data, string $type, ?string $format = null)
public function supportsDenormalization($data, $type, $format = null)
{
return Person::class === $type && 'person' === ($data['type'] ?? null);
}
public function supportsNormalization($data, ?string $format = null): bool
public function supportsNormalization($data, $format = null): bool
{
return $data instanceof Person && 'json' === $format;
}

View File

@@ -0,0 +1,91 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relationship;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(TranslatableStringHelperInterface $translatableStringHelper)
{
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @param Relationship $relation
* @param null|string $format
*/
public function normalize($relation, $format = null, array $context = [])
{
$counterpart = $context['docgen:relationship:counterpart'] ?? null;
$contextPerson = array_merge($context, [
'docgen:person:with-relations' => 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, $format = null, array $context = [])
{
if ('docgen' !== $format) {
return false;
}
return $data instanceof Relationship || (null === $data
&& Relationship::class === ($context['docgen:expects'] ?? null));
}
}

View File

@@ -28,20 +28,52 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte
$this->render = $render;
}
public function normalize($socialAction, ?string $format = null, array $context = [])
public function normalize($socialAction, $format = null, array $context = [])
{
return [
'id' => $socialAction->getId(),
'type' => 'social_work_social_action',
'text' => $this->render->renderString($socialAction, []),
'parent' => $this->normalizer->normalize($socialAction->getParent()),
'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()),
'title' => $socialAction->getTitle(),
];
switch ($format) {
case 'json':
return [
'id' => $socialAction->getId(),
'type' => 'social_work_social_action',
'text' => $this->render->renderString($socialAction, []),
'parent' => $this->normalizer->normalize($socialAction->getParent(), $format, $context),
'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate(), $format, $context),
'title' => $socialAction->getTitle(),
'issue' => $this->normalizer->normalize($socialAction->getIssue(), $format, $context),
];
case 'docgen':
if (null === $socialAction) {
return ['id' => 0, 'title' => '', 'text' => ''];
}
return [
'id' => $socialAction->getId(),
'text' => $this->render->renderString($socialAction, []),
'title' => $socialAction->getTitle(),
];
default:
throw new \Symfony\Component\Serializer\Exception\RuntimeException('format not supported');
}
}
public function supportsNormalization($data, ?string $format = null)
public function supportsNormalization($data, $format = null, array $context = [])
{
return $data instanceof SocialAction;
if ($data instanceof SocialAction && 'json' === $format) {
return true;
}
if ('docgen' === $format) {
if ($data instanceof SocialAction) {
return true;
}
if (null === $data && SocialAction::class === ($context['docgen:expects'] ?? null)) {
return true;
}
}
return false;
}
}

View File

@@ -13,11 +13,11 @@ namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
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 SocialIssueNormalizer implements NormalizerAwareInterface, NormalizerInterface
class SocialIssueNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
@@ -28,21 +28,51 @@ class SocialIssueNormalizer implements NormalizerAwareInterface, NormalizerInter
$this->render = $render;
}
public function normalize($socialIssue, ?string $format = null, array $context = [])
public function normalize($socialIssue, $format = null, array $context = [])
{
/** @var SocialIssue $socialIssue */
return [
'type' => 'social_issue',
'id' => $socialIssue->getId(),
'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null,
'children_ids' => $socialIssue->getChildren()->map(static function (SocialIssue $si) { return $si->getId(); }),
'title' => $socialIssue->getTitle(),
'text' => $this->render->renderString($socialIssue, []),
];
switch ($format) {
case 'json':
return [
'type' => 'social_issue',
'id' => $socialIssue->getId(),
'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null,
'children_ids' => $socialIssue->getChildren()->map(static function (SocialIssue $si) {
return $si->getId();
}),
'title' => $socialIssue->getTitle(),
'text' => $this->render->renderString($socialIssue, []),
];
case 'docgen':
if (null === $socialIssue) {
return ['id' => 0, 'title' => '', 'text' => ''];
}
return [
'id' => $socialIssue->getId(),
'title' => $socialIssue->getTitle(),
'text' => $this->render->renderString($socialIssue, []),
];
}
}
public function supportsNormalization($data, ?string $format = null): bool
public function supportsNormalization($data, $format = null, array $context = [])
{
return $data instanceof SocialIssue;
if ($data instanceof SocialIssue && 'json' === $format) {
return true;
}
if ('docgen' === $format) {
if ($data instanceof SocialIssue) {
return true;
}
if (null === $data && SocialIssue::class === ($context['docgen:expects'] ?? null)) {
return true;
}
}
return false;
}
}