diff --git a/README.md b/README.md index d042891..1e28813 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ > [!IMPORTANT] > dot-twigrenderer is a wrapper on top of [mezzio/mezzio-twigrenderer](https://github.com/mezzio/mezzio-twigrenderer) > -> ![OSS Lifecycle](https://img.shields.io/osslifecycle/mezzio/mezzio-twigrenderer) +> ![Dynamic JSON Badge](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fmezzio%2Fmezzio-twigrenderer%2Fproperties%2Fvalues&query=%24%5B%3F(%40.property_name%3D%3D%22maintenance-mode%22)%5D.value&label=Maintenance%20Mode) DotKernel component providing twig extensions and customizations. diff --git a/composer.json b/composer.json index 4aeaa39..5ba6b50 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "mezzio/mezzio-twigrenderer": "^2.15", "laminas/laminas-view": "^2.33", "laminas/laminas-authentication": "^2.16", - "dotkernel/dot-navigation": "^3.4.2", + "dotkernel/dot-navigation": "^3.5.1", "dotkernel/dot-flashmessenger": "^3.4.2", "laminas/laminas-form": "^3.19.1", "dotkernel/dot-authorization": "^3.4.1" diff --git a/psalm-baseline.xml b/psalm-baseline.xml deleted file mode 100644 index 7c53d4a..0000000 --- a/psalm-baseline.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - twig_date_converter($env, $date) - twig_date_converter($env, $now) - - - diff --git a/psalm.xml b/psalm.xml index e6f472e..7272b57 100644 --- a/psalm.xml +++ b/psalm.xml @@ -7,7 +7,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" - errorBaseline="psalm-baseline.xml" > diff --git a/src/Extension/DateExtension.php b/src/Extension/DateExtension.php index 27f9bd2..02d0a95 100644 --- a/src/Extension/DateExtension.php +++ b/src/Extension/DateExtension.php @@ -4,13 +4,19 @@ namespace Dot\Twig\Extension; +use DateTime; +use DateTimeImmutable; use DateTimeInterface; use DateTimeZone; +use Exception; use Twig\Environment; use Twig\Extension\AbstractExtension; +use Twig\Extension\CoreExtension; use Twig\TwigFilter; -use function twig_date_converter; +use function ctype_digit; +use function strtolower; +use function substr; class DateExtension extends AbstractExtension { @@ -33,15 +39,24 @@ public function getFilters(): array /** * Filters for converting dates to a time ago string like Facebook and Twitter has. * If none given, the current time will be used. + * + * @throws Exception */ public function diff( Environment $env, string|DateTimeInterface|null $date, - string|DateTimeZone|null $now = null + string|DateTimeInterface|null $now = null, + string|DateTimeZone|null $timezone = null, ): string { + if (null === $timezone) { + $timezone = $env->getExtension(CoreExtension::class)->getTimezone(); + } elseif (! $timezone instanceof DateTimeZone) { + $timezone = new DateTimeZone($timezone); + } + // Convert both dates to DateTime instances. - $date = twig_date_converter($env, $date); - $now = twig_date_converter($env, $now); + $date = $this->convertDate($date, $timezone); + $now = $this->convertDate($now, $timezone); // Get the difference between the two DateTime objects. $diff = $date->diff($now); @@ -58,6 +73,40 @@ public function diff( return ''; } + /** + * @throws Exception + */ + protected function convertDate(string|DateTimeInterface|null $date, DateTimeZone $timezone): DateTimeInterface + { + if ($date instanceof DateTimeImmutable) { + return $date->setTimezone($timezone); + } + + if ($date instanceof DateTimeInterface) { + $date = clone $date; + $date->setTimezone($timezone); + + return $date; + } + + if (null === $date || 'now' === strtolower($date)) { + if (null === $date) { + $date = 'now'; + } + + return new DateTime($date, $timezone); + } + + if ( + ctype_digit($date) + || (! empty($date) && '-' === $date[0] && ctype_digit(substr($date, 1))) + ) { + return new DateTime('@' . $date, $timezone); + } else { + return new DateTime($date, $timezone); + } + } + public function getPluralizedInterval(mixed $count, int $invert, string $unit): string { if (1 !== $count) { diff --git a/test/Extension/DateExtensionTest.php b/test/Extension/DateExtensionTest.php index 2467403..a0c0e8d 100644 --- a/test/Extension/DateExtensionTest.php +++ b/test/Extension/DateExtensionTest.php @@ -4,6 +4,7 @@ namespace DotTest\Twig\Extension; +use DateTimeImmutable; use Dot\Twig\Extension\DateExtension; use Exception; use PHPUnit\Framework\MockObject\MockObject; @@ -38,7 +39,16 @@ public function testFilters(): void public function testDiffWillReturnString(): void { - $this->assertIsString($this->extension->diff($this->env, "2023-07-31")); + $this->assertIsString($this->extension->diff($this->env, '2023-07-31')); + $this->assertIsString( + $this->extension->diff( + $this->env, + new DateTimeImmutable('yesterday'), + new DateTimeImmutable(), + 'GMT+2' + ) + ); + $this->assertIsString($this->extension->diff($this->env, '802587600')); } public function testDiffWillReturnExceptionUnexpectedCharacters(): void