mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 21:34:25 +00:00
Add line splitting in user render string functionality
Introduce a new option to split lines in user job and scope render strings based on a specified character limit. Implement a corresponding test to verify the correct behavior of this feature.
This commit is contained in:
parent
d6c55c830b
commit
5ad11041e0
@ -13,8 +13,6 @@ namespace Chill\MainBundle\Templating\Entity;
|
|||||||
|
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||||
use DateTime;
|
|
||||||
use 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\LoaderError;
|
||||||
@ -26,11 +24,17 @@ use Twig\Error\SyntaxError;
|
|||||||
*/
|
*/
|
||||||
class UserRender implements ChillEntityRenderInterface
|
class UserRender implements ChillEntityRenderInterface
|
||||||
{
|
{
|
||||||
|
public const SPLIT_LINE_BEFORE_CHARACTER = 'split_lines_before_characters';
|
||||||
final public const DEFAULT_OPTIONS = [
|
final public const DEFAULT_OPTIONS = [
|
||||||
'main_scope' => true,
|
'main_scope' => true,
|
||||||
'user_job' => true,
|
'user_job' => true,
|
||||||
'absence' => true,
|
'absence' => true,
|
||||||
'at_date' => null, // instanceof DateTimeInterface
|
'at_date' => null, // instanceof DateTimeInterface
|
||||||
|
/*
|
||||||
|
* when set, the jobs and service will be splitted in multiple lines. The line will be splitted
|
||||||
|
* before the given character. Only for renderString, renderBox is not concerned.
|
||||||
|
*/
|
||||||
|
self::SPLIT_LINE_BEFORE_CHARACTER => null,
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@ -65,8 +69,6 @@ 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'];
|
|
||||||
|
|
||||||
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) {
|
||||||
@ -89,6 +91,28 @@ class UserRender implements ChillEntityRenderInterface
|
|||||||
$str .= ' ('.$this->translator->trans('absence.Absent').')';
|
$str .= ' ('.$this->translator->trans('absence.Absent').')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (null !== $opts[self::SPLIT_LINE_BEFORE_CHARACTER]) {
|
||||||
|
if (!is_int($opts[self::SPLIT_LINE_BEFORE_CHARACTER])) {
|
||||||
|
throw new \InvalidArgumentException('Only integer for option split_lines_before_characters is allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
$characterPerLine = $opts[self::SPLIT_LINE_BEFORE_CHARACTER];
|
||||||
|
$exploded = explode(' ', $str);
|
||||||
|
$charOnLine = 0;
|
||||||
|
$str = '';
|
||||||
|
foreach ($exploded as $word) {
|
||||||
|
if ($charOnLine + strlen($word) > $characterPerLine) {
|
||||||
|
$str .= "\n";
|
||||||
|
$charOnLine = 0;
|
||||||
|
}
|
||||||
|
if ($charOnLine > 0) {
|
||||||
|
$str .= ' ';
|
||||||
|
}
|
||||||
|
$str .= $word;
|
||||||
|
$charOnLine += strlen($word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ class UserRenderTest extends TestCase
|
|||||||
public function testRenderUserWithJobAndScopeAtCertainDate(): void
|
public function testRenderUserWithJobAndScopeAtCertainDate(): void
|
||||||
{
|
{
|
||||||
// Create a user with a certain user job
|
// Create a user with a certain user job
|
||||||
|
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$userJobA = new UserJob();
|
$userJobA = new UserJob();
|
||||||
$scopeA = new Scope();
|
$scopeA = new Scope();
|
||||||
@ -106,4 +105,52 @@ class UserRenderTest extends TestCase
|
|||||||
$expectedStringC = 'BOB ISLA (directrice) (service B)';
|
$expectedStringC = 'BOB ISLA (directrice) (service B)';
|
||||||
$this->assertEquals($expectedStringC, $renderer->renderString($user, $optionsNoDate));
|
$this->assertEquals($expectedStringC, $renderer->renderString($user, $optionsNoDate));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRenderStringWithSplitLines(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
// Create a user with a certain user job
|
||||||
|
$user = new User();
|
||||||
|
$userJobA = new UserJob();
|
||||||
|
$scopeA = new Scope();
|
||||||
|
|
||||||
|
$userJobA->setLabel(['fr' => 'assistant social en maison de service accompagné'])
|
||||||
|
->setActive(true);
|
||||||
|
$scopeA->setName(['fr' => 'service de l\'assistant professionnel']);
|
||||||
|
$user->setLabel('Robert Van Zorrizzeen Gorikke');
|
||||||
|
|
||||||
|
$userJobHistoryA = (new User\UserJobHistory())
|
||||||
|
->setUser($user)
|
||||||
|
->setJob($userJobA)
|
||||||
|
->setStartDate(new \DateTimeImmutable('2023-11-01 12:00:00'))
|
||||||
|
->setEndDate(new \DateTimeImmutable('2023-11-30 00:00:00'));
|
||||||
|
|
||||||
|
$userScopeHistoryA = (new User\UserScopeHistory())
|
||||||
|
->setUser($user)
|
||||||
|
->setScope($scopeA)
|
||||||
|
->setStartDate(new \DateTimeImmutable('2023-11-01 12:00:00'))
|
||||||
|
->setEndDate(new \DateTimeImmutable('2023-11-30 00:00:00'));
|
||||||
|
|
||||||
|
$user->getUserJobHistories()->add($userJobHistoryA);
|
||||||
|
$user->getUserScopeHistories()->add($userScopeHistoryA);
|
||||||
|
|
||||||
|
// Create renderer
|
||||||
|
$translatableStringHelperMock = $this->prophesize(TranslatableStringHelperInterface::class);
|
||||||
|
$translatableStringHelperMock->localize(Argument::type('array'))->will(fn ($args) => $args[0]['fr']);
|
||||||
|
|
||||||
|
$engineMock = $this->createMock(Environment::class);
|
||||||
|
$translatorMock = $this->createMock(TranslatorInterface::class);
|
||||||
|
$clock = new MockClock(new \DateTimeImmutable('2023-11-15 12:00:00'));
|
||||||
|
|
||||||
|
$renderer = new UserRender($translatableStringHelperMock->reveal(), $engineMock, $translatorMock, $clock);
|
||||||
|
|
||||||
|
$actual = $renderer->renderString($user, ['split_lines_before_characters' => 30]);
|
||||||
|
self::assertEquals(<<<'STR'
|
||||||
|
Robert Van Zorrizzeen Gorikke
|
||||||
|
(assistant social en maison de
|
||||||
|
service accompagné) (service de
|
||||||
|
l'assistant professionnel)
|
||||||
|
STR, $actual);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user