77 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Templating\Entity;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Monolog\DateTimeImmutable;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @implements ChillEntityRenderInterface<User>
*/
class UserRender implements ChillEntityRenderInterface
{
final public const DEFAULT_OPTIONS = [
'main_scope' => true,
'user_job' => true,
'absence' => true,
'at_date' => null,
];
public function __construct(private readonly TranslatableStringHelper $translatableStringHelper, private readonly \Twig\Environment $engine, private readonly TranslatorInterface $translator)
{
}
public function renderBox($entity, array $options): string
{
$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', [
'user' => $entity,
'opts' => $opts,
]);
}
public function renderString($entity, array $options): string
{
$opts = \array_merge(self::DEFAULT_OPTIONS, $options);
$immutableAtDate = $opts['at_date'] instanceOf \DateTime ? DateTimeImmutable::createFromMutable($opts['at_date']) : $opts['at_date'];
$str = $entity->getLabel();
if (null !== $entity->getUserJob($immutableAtDate) && $opts['user_job']) {
$str .= ' ('.$this->translatableStringHelper
->localize($entity->getUserJob($immutableAtDate)->getLabel()).')';
}
if (null !== $entity->getMainScope($immutableAtDate) && $opts['main_scope']) {
$str .= ' ('.$this->translatableStringHelper
->localize($entity->getMainScope($immutableAtDate)->getName()).')';
}
if ($entity->isAbsent() && $opts['absence']) {
$str .= ' ('.$this->translator->trans('absence.Absent').')';
}
return $str;
}
public function supports($entity, array $options): bool
{
return $entity instanceof User;
}
}