From b46883fe364504f2b3b24f900263a7aab1e435e6 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 30 Jan 2024 16:31:29 +0100 Subject: [PATCH] Implement clockInterface in renderString method --- .../Templating/Entity/UserRender.php | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php b/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php index 45aad79ee..77b4255cc 100644 --- a/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php +++ b/src/Bundle/ChillMainBundle/Templating/Entity/UserRender.php @@ -13,9 +13,13 @@ namespace Chill\MainBundle\Templating\Entity; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Templating\TranslatableStringHelper; +use DateTime; use Monolog\DateTimeImmutable; use Symfony\Component\Clock\ClockInterface; use Symfony\Contracts\Translation\TranslatorInterface; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; /** * @implements ChillEntityRenderInterface @@ -37,8 +41,11 @@ class UserRender implements ChillEntityRenderInterface /** * @param $entity - * @param array{main_scope?: bool, user_job?: bool, absence?: bool, at_date?: null|DateTimeImmutable|DateTimeMutable} $options + * @param array{main_scope?: bool, user_job?: bool, absence?: bool, at_date?: null|DateTimeImmutable|DateTime} $options * @return string + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ public function renderBox($entity, array $options): string { @@ -46,7 +53,7 @@ class UserRender implements ChillEntityRenderInterface if (null === $opts['at_date']) { $opts['at_date'] = $this->clock->now(); - } elseif ($opts['at_date'] instanceof \DateTime) { + } elseif ($opts['at_date'] instanceof DateTime) { $opts['at_date'] = DateTimeImmutable::createFromMutable($opts['at_date']); } @@ -65,18 +72,24 @@ class UserRender implements ChillEntityRenderInterface { $opts = \array_merge(self::DEFAULT_OPTIONS, $options); - $immutableAtDate = $opts['at_date'] instanceOf \DateTime ? DateTimeImmutable::createFromMutable($opts['at_date']) : $opts['at_date']; +// $immutableAtDate = $opts['at_date'] instanceOf DateTime ? DateTimeImmutable::createFromMutable($opts['at_date']) : $opts['at_date']; + + if (null === $opts['at_date']) { + $opts['at_date'] = $this->clock->now(); + } elseif ($opts['at_date'] instanceof DateTime) { + $opts['at_date'] = DateTimeImmutable::createFromMutable($opts['at_date']); + } $str = $entity->getLabel(); - if (null !== $entity->getUserJob($immutableAtDate) && $opts['user_job']) { + if (null !== $entity->getUserJob($opts['at_date']) && $opts['user_job']) { $str .= ' ('.$this->translatableStringHelper - ->localize($entity->getUserJob($immutableAtDate)->getLabel()).')'; + ->localize($entity->getUserJob($opts['at_date'])->getLabel()).')'; } - if (null !== $entity->getMainScope($immutableAtDate) && $opts['main_scope']) { + if (null !== $entity->getMainScope($opts['at_date']) && $opts['main_scope']) { $str .= ' ('.$this->translatableStringHelper - ->localize($entity->getMainScope($immutableAtDate)->getName()).')'; + ->localize($entity->getMainScope($opts['at_date'])->getName()).')'; } if ($entity->isAbsent() && $opts['absence']) {