Handle DateTime type for activity while DateTimeImmutable is expected

This commit is contained in:
Julie Lenaerts 2024-01-08 12:35:09 +01:00
parent fe695f1a14
commit fbbf421d8b
2 changed files with 11 additions and 6 deletions

View File

@ -274,7 +274,7 @@ class User implements UserInterface, \Stringable
return $this->mainLocation; return $this->mainLocation;
} }
public function getMainScope(\DateTime $atDate = null): ?Scope public function getMainScope(\DateTimeImmutable $atDate = null): ?Scope
{ {
$atDate ??= new \DateTimeImmutable('now'); $atDate ??= new \DateTimeImmutable('now');
@ -326,7 +326,7 @@ class User implements UserInterface, \Stringable
return $this->salt; return $this->salt;
} }
public function getUserJob(\DateTime $atDate = null): ?UserJob public function getUserJob(\DateTimeImmutable $atDate = null): ?UserJob
{ {
$atDate ??= new \DateTimeImmutable('now'); $atDate ??= new \DateTimeImmutable('now');

View File

@ -13,6 +13,7 @@ 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 Monolog\DateTimeImmutable;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
/** /**
@ -35,6 +36,8 @@ class UserRender implements ChillEntityRenderInterface
{ {
$opts = \array_merge(self::DEFAULT_OPTIONS, $options); $opts = \array_merge(self::DEFAULT_OPTIONS, $options);
$opts['at_date'] = $opts['at_date'] instanceOf \DateTime ? DateTimeImmutable::createFromMutable($opts['at_date']) : $opts['at_date'];
return $this->engine->render('@ChillMain/Entity/user.html.twig', [ return $this->engine->render('@ChillMain/Entity/user.html.twig', [
'user' => $entity, 'user' => $entity,
'opts' => $opts, 'opts' => $opts,
@ -45,16 +48,18 @@ 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'];
$str = $entity->getLabel(); $str = $entity->getLabel();
if (null !== $entity->getUserJob($opts['at_date']) && $opts['user_job']) { if (null !== $entity->getUserJob($immutableAtDate) && $opts['user_job']) {
$str .= ' ('.$this->translatableStringHelper $str .= ' ('.$this->translatableStringHelper
->localize($entity->getUserJob($opts['at_date'])->getLabel()).')'; ->localize($entity->getUserJob($immutableAtDate)->getLabel()).')';
} }
if (null !== $entity->getMainScope($opts['at_date']) && $opts['main_scope']) { if (null !== $entity->getMainScope($immutableAtDate) && $opts['main_scope']) {
$str .= ' ('.$this->translatableStringHelper $str .= ' ('.$this->translatableStringHelper
->localize($entity->getMainScope($opts['at_date'])->getName()).')'; ->localize($entity->getMainScope($immutableAtDate)->getName()).')';
} }
if ($entity->isAbsent() && $opts['absence']) { if ($entity->isAbsent() && $opts['absence']) {