Implement clockInterface in renderString method

This commit is contained in:
Julie Lenaerts 2024-01-30 16:31:29 +01:00
parent 8d58805abd
commit b46883fe36

View File

@ -13,9 +13,13 @@ namespace Chill\MainBundle\Templating\Entity;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use DateTime;
use Monolog\DateTimeImmutable; use Monolog\DateTimeImmutable;
use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Clock\ClockInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
/** /**
* @implements ChillEntityRenderInterface<User> * @implements ChillEntityRenderInterface<User>
@ -37,8 +41,11 @@ class UserRender implements ChillEntityRenderInterface
/** /**
* @param $entity * @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 * @return string
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/ */
public function renderBox($entity, array $options): string public function renderBox($entity, array $options): string
{ {
@ -46,7 +53,7 @@ class UserRender implements ChillEntityRenderInterface
if (null === $opts['at_date']) { if (null === $opts['at_date']) {
$opts['at_date'] = $this->clock->now(); $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']); $opts['at_date'] = DateTimeImmutable::createFromMutable($opts['at_date']);
} }
@ -65,18 +72,24 @@ class UserRender implements ChillEntityRenderInterface
{ {
$opts = \array_merge(self::DEFAULT_OPTIONS, $options); $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(); $str = $entity->getLabel();
if (null !== $entity->getUserJob($immutableAtDate) && $opts['user_job']) { if (null !== $entity->getUserJob($opts['at_date']) && $opts['user_job']) {
$str .= ' ('.$this->translatableStringHelper $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 $str .= ' ('.$this->translatableStringHelper
->localize($entity->getMainScope($immutableAtDate)->getName()).')'; ->localize($entity->getMainScope($opts['at_date'])->getName()).')';
} }
if ($entity->isAbsent() && $opts['absence']) { if ($entity->isAbsent() && $opts['absence']) {