Skip to content

Commit

Permalink
Add shim for date filter
Browse files Browse the repository at this point in the history
Handle Date values so that we maintain compatibility with twig
templates and date objects.

Fixes #97
  • Loading branch information
markstory committed Jun 30, 2024
1 parent d98f882 commit 3ecfcd7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Twig/Extension/TimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

UndefinedDocblockClass

src/Twig/Extension/TimeExtension.php:73:15: UndefinedDocblockClass: Docblock-defined class, interface or enum named Chronos\ChronosDate does not exist (see https://psalm.dev/200)

Check failure on line 73 in src/Twig/Extension/TimeExtension.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

MismatchingDocblockParamType

src/Twig/Extension/TimeExtension.php:73:15: MismatchingDocblockParamType: Parameter $date has wrong type 'Chronos\ChronosDate|DateInterval|DateTimeInterface|string', should be 'Cake\Chronos\ChronosDate|DateInterval|DateTimeInterface|string' (see https://psalm.dev/141)
* @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

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

PHPDoc tag @param for parameter $date with type Chronos\ChronosDate|DateInterval|DateTimeInterface|string is not subtype of native type Cake\Chronos\ChronosDate|DateInterval|DateTimeInterface|string.
ChronosDate|DateTimeInterface|DateInterval|string $date,

Check failure on line 78 in src/Twig/Extension/TimeExtension.php

View workflow job for this annotation

GitHub Actions / cs-stan / Coding Standard & Static Analysis

Parameter $date of method Cake\TwigView\Twig\Extension\TimeExtension::formatDate() has invalid type Chronos\ChronosDate.
?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);
}
}
6 changes: 6 additions & 0 deletions tests/TestCase/View/TwigViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Cake\Core\Configure;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Cake\I18n\I18n;
use Cake\TestSuite\TestCase;
use TestApp\View\AppView;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -144,10 +145,15 @@ public function testCellsShareTwig()
*/
public function testTwigDateFormat()
{
$restore = I18n::getLocale();
I18n::setLocale('fr');

$this->view->set('date', new Date('2024-06-24'));
$this->view->set('datetime', new DateTime('2024-06-24 12:13:14'));

$output = $this->view->render('date_format', false);
I18n::setLocale($restore);

$expected = <<<TEXT
Date: 2024/06/24
Datetime: 2024/06/24 12:13:14
Expand Down

0 comments on commit 3ecfcd7

Please sign in to comment.