Skip to content

Commit d8be666

Browse files
author
Stephan Wentz
committed
fix: Add parsePeriodIdentifier and monthPeriod twig functions
1 parent 1850412 commit d8be666

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Twig/Extension/PeriodExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public function __construct(private readonly PeriodFactory $periodFactory)
2626
public function getFunctions(): array
2727
{
2828
return [
29+
new TwigFunction('parse_period_identifier', [$this, 'parsePeriodIdentifier'], ['is_safe' => ['html']]),
30+
new TwigFunction('month_period', [$this, 'monthPeriod'], ['is_safe' => ['html']]),
2931
new TwigFunction('current_day_period', [$this, 'currentDayPeriod'], ['is_safe' => ['html']]),
3032
new TwigFunction('current_week_period', [$this, 'currentWeekPeriod'], ['is_safe' => ['html']]),
3133
new TwigFunction('current_month_period', [$this, 'currentMonthPeriod'], ['is_safe' => ['html']]),
@@ -52,6 +54,16 @@ public function getFunctions(): array
5254
];
5355
}
5456

57+
public function parsePeriodIdentifier(string $periodIdentifier): Period
58+
{
59+
return $this->periodFactory->fromIdentifier($periodIdentifier);
60+
}
61+
62+
public function monthPeriod($periodString): MonthPeriod|null
63+
{
64+
return $periodString ? MonthPeriod::createFromPeriodString($periodString) : null;
65+
}
66+
5567
public function currentDayPeriod(): DayPeriod
5668
{
5769
return $this->periodFactory->currentDay();

tests/Twig/Extension/PeriodExtensionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ public function testGetFunctions(): void
3939
$this->assertContainsOnlyInstancesOf(TwigFunction::class, $this->extension->getFunctions());
4040
}
4141

42+
public function testParsePeriodIdentifier(): void
43+
{
44+
$this->assertEquals($this->periodFactory->currentDay(), $this->extension->parsePeriodIdentifier('day#2018-06-15'));
45+
$this->assertEquals($this->periodFactory->currentWeek(), $this->extension->parsePeriodIdentifier('week#2018-24'));
46+
$this->assertEquals($this->periodFactory->currentMonth(), $this->extension->parsePeriodIdentifier('month#2018-06'));
47+
$this->assertEquals($this->periodFactory->currentYear(), $this->extension->parsePeriodIdentifier('year#2018'));
48+
$this->assertEquals($this->periodFactory->currentRunningWeek(), $this->extension->parsePeriodIdentifier('running-week#2018-24'));
49+
$this->assertEquals($this->periodFactory->currentRunningMonth(), $this->extension->parsePeriodIdentifier('running-month#2018-06'));
50+
$this->assertEquals($this->periodFactory->currentRunningYear(), $this->extension->parsePeriodIdentifier('running-year#2018'));
51+
}
52+
53+
public function testMonthPeriod(): void
54+
{
55+
$this->assertEquals($this->periodFactory->currentMonth(), $this->extension->monthPeriod('2018-06'));
56+
}
57+
4258
public function testCurrent(): void
4359
{
4460
$this->assertEquals($this->periodFactory->currentDay(), $this->extension->currentDayPeriod());

0 commit comments

Comments
 (0)