-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Date values so that we maintain compatibility with twig templates and date objects. Fixes #97
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,15 +18,35 @@ | |
|
||
namespace Cake\TwigView\Twig\Extension; | ||
|
||
use Cake\Chronos\ChronosDate; | ||
use Cake\I18n\DateTime; | ||
use DateInterval; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\Extension\CoreExtension; | ||
use Twig\TwigFilter; | ||
use Twig\TwigFunction; | ||
|
||
/** | ||
* Class TimeExtension. | ||
*/ | ||
class TimeExtension extends AbstractExtension | ||
{ | ||
private ?CoreExtension $coreExt; | ||
|
||
/** | ||
* Get declared filters. | ||
* | ||
* @return array<\Twig\TwigFilter> | ||
*/ | ||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('date', [$this, 'formatDate']), | ||
]; | ||
} | ||
|
||
/** | ||
* Get declared functions. | ||
* | ||
|
@@ -35,10 +55,37 @@ class TimeExtension extends AbstractExtension | |
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('date', function ($time = null, $timezone = null) { | ||
return new DateTime($time, $timezone); | ||
}), | ||
new TwigFunction('time', function ($time = null, $timezone = null) { | ||
return new DateTime($time, $timezone); | ||
}), | ||
new TwigFunction('timezones', 'Cake\I18n\DateTime::listTimezones'), | ||
]; | ||
} | ||
|
||
/** | ||
* Format a date/datetime value | ||
* | ||
* Includes shims for \Chronos\ChronosDate as Twig doesn't. | ||
* | ||
* @param \Chronos\ChronosDate|\DateTimeInterface|\DateInterval|string $date The date to format. | ||
Check failure on line 73 in src/Twig/Extension/TimeExtension.php GitHub Actions / cs-stan / Coding Standard & Static AnalysisUndefinedDocblockClass
Check failure on line 73 in src/Twig/Extension/TimeExtension.php GitHub Actions / cs-stan / Coding Standard & Static AnalysisMismatchingDocblockParamType
|
||
* @param ?string $format The format to use, null to use the default. | ||
* @param \DateTimeZone|string|false|null $timezone The target timezone, null to use system. | ||
*/ | ||
public function formatDate( | ||
Check failure on line 77 in src/Twig/Extension/TimeExtension.php GitHub Actions / cs-stan / Coding Standard & Static Analysis
|
||
ChronosDate|DateTimeInterface|DateInterval|string $date, | ||
?string $format = null, | ||
DateTimeZone|string|false|null $timezone = null | ||
): string { | ||
if (!isset($this->coreExt)) { | ||
$this->coreExt = new CoreExtension(); | ||
} | ||
if ($date instanceof ChronosDate) { | ||
$date = $date->toDateTimeImmutable(); | ||
} | ||
|
||
return $this->coreExt->formatDate($date, $format, $timezone); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters