full generation for accompanying period

This commit is contained in:
2021-12-01 15:43:34 +01:00
parent 9d0e1a82e7
commit 7719d2b073
14 changed files with 248 additions and 108 deletions

View File

@@ -1,5 +1,14 @@
<?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;
@@ -10,42 +19,33 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
use Chill\PersonBundle\Templating\Entity\ClosingMotiveRender;
use Chill\PersonBundle\Templating\Entity\SocialIssueRender;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use DateTime;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
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 TranslatorInterface $translator;
private TranslatableStringHelper $translatableStringHelper;
private SocialIssueRender $socialIssueRender;
private ClosingMotiveRender $closingMotiveRender;
private ScopeResolverDispatcher $scopeResolverDispatcher;
private const IGNORE_FIRST_PASS_KEY = 'acc_period_ignore_first_pass';
private const PERIOD_NULL = [
'id' => "",
'closingDate' => \DateTime::class,
'confidential' => "",
'id' => '',
'closingDate' => DateTime::class,
'confidential' => '',
'confidentialText' => '',
'createdAt' => \DateTime::class,
'createdAt' => DateTime::class,
'createdBy' => User::class,
'emergency' => "",
'emergency' => '',
'emergencyText' => '',
'openingDate' => \DateTime::class,
'openingDate' => DateTime::class,
'originText' => '',
'requestorAnonymous' => false,
'socialIssues' => [],
@@ -59,6 +59,16 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
'participations' => [],
];
private ClosingMotiveRender $closingMotiveRender;
private ScopeResolverDispatcher $scopeResolverDispatcher;
private SocialIssueRender $socialIssueRender;
private TranslatableStringHelper $translatableStringHelper;
private TranslatorInterface $translator;
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper,
@@ -73,38 +83,19 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
$this->scopeResolverDispatcher = $scopeResolverDispatcher;
}
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])) {
return false;
}
return true;
} elseif (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriod::class) {
return true;
}
return false;
}
/**
* @param AccompanyingPeriod|null $period
*/
public function normalize($period, string $format = null, array $context = [])
public function normalize($period, ?string $format = null, array $context = [])
{
if ($period instanceof AccompanyingPeriod) {
$ignored = $context[self::IGNORE_FIRST_PASS_KEY] ?? [];
$ignored[] = spl_object_hash($period);
$initial =
$this->normalizer->normalize($period, $format, \array_merge($context,
[self::IGNORE_FIRST_PASS_KEY => $ignored, AbstractNormalizer::GROUPS => 'docgen:read']));
$this->normalizer->normalize($period, $format, array_merge(
$context,
[self::IGNORE_FIRST_PASS_KEY => $ignored, AbstractNormalizer::GROUPS => 'docgen:read']
));
// some transformation
$user = $initial['user'];
@@ -122,14 +113,14 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
// and add data custom
[
'intensity' => $this->translator->trans($period->getIntensity()),
'step' => $this->translator->trans('accompanying_period.'.$period->getStep()),
'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()) : '',
//'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '',
'closingMotiveText' => null !== $period->getClosingMotive() ?
$this->closingMotiveRender->renderString($period->getClosingMotive(), []) : '',
'ref' => $user,
'socialIssuesText' => implode(', ', array_map(function(SocialIssue $s) {
'socialIssuesText' => implode(', ', array_map(function (SocialIssue $s) {
return $this->socialIssueRender->renderString($s, []);
}, $period->getSocialIssues()->toArray())),
'scopesText' => implode(', ', array_map(function (Scope $s) {
@@ -142,6 +133,28 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf
return self::PERIOD_NULL;
}
throw new InvalidArgumentException("this neither an accompanying period or 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;
}
}