chill-bundles/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php

169 lines
7.7 KiB
PHP

<?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\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 DateTime;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
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 in_array;
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,
'originText' => '',
'requestorAnonymous' => false,
'socialIssues' => [],
'intensity' => '',
'step' => '',
'closingMotiveText' => '',
'socialIssuesText' => '',
'scopes' => [],
'scopesText' => '',
'ref' => User::class,
'participations' => [],
];
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
*/
public function normalize($period, ?string $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(),
'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(),
'requestor' => $this->normalizer->normalize($period->getRequestor(), $format, array_merge($context, ['docgen:expects' => Person::class])),
'resources' => $this->normalizer->normalize($period->getResources(), $format, $context),
'scopes' => $this->normalizer->normalize($period->getScopes(), $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)),
//'scopes' => $scopes,
'hasRequestor' => $period->getRequestor() !== null,
];
} elseif (null === $period) {
return self::PERIOD_NULL;
}
throw new InvalidArgumentException('this neither an accompanying period or null');
}
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
if ('docgen' !== $format) {
return false;
}
if ($data instanceof AccompanyingPeriod) {
/*if (array_key_exists(self::IGNORE_FIRST_PASS_KEY, $context)
&& in_array(spl_object_hash($data), $context[self::IGNORE_FIRST_PASS_KEY], true)) {
return false;
}*/
return true;
}
if (null === $data && AccompanyingPeriod::class === ($context['docgen:expects'] ?? null)) {
return true;
}
return false;
}
}