From 2b2924fd53545fb4e3e1b69634ebf7d7da080445 Mon Sep 17 00:00:00 2001 From: bidi47 Date: Fri, 19 Jul 2024 14:06:13 +0300 Subject: [PATCH 1/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From 88f8b2429bc42d51be510bce37fc1ffb97afca9c Mon Sep 17 00:00:00 2001 From: Jurj-Bogdan Date: Wed, 24 Jul 2024 12:12:09 +0300 Subject: [PATCH 2/4] psalm fix, replaced deprecated fuction call --- psalm-baseline.xml | 9 +-------- src/Extension/DateExtension.php | 10 ++++------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7c53d4a..7bf39a8 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,9 +1,2 @@ - - - - twig_date_converter($env, $date) - twig_date_converter($env, $now) - - - + diff --git a/src/Extension/DateExtension.php b/src/Extension/DateExtension.php index 27f9bd2..a96036c 100644 --- a/src/Extension/DateExtension.php +++ b/src/Extension/DateExtension.php @@ -5,13 +5,11 @@ namespace Dot\Twig\Extension; use DateTimeInterface; -use DateTimeZone; use Twig\Environment; use Twig\Extension\AbstractExtension; +use Twig\Extension\CoreExtension; use Twig\TwigFilter; -use function twig_date_converter; - class DateExtension extends AbstractExtension { public static array $units = [ @@ -37,11 +35,11 @@ public function getFilters(): array public function diff( Environment $env, string|DateTimeInterface|null $date, - string|DateTimeZone|null $now = null + string|DateTimeInterface|null $now = null ): string { // Convert both dates to DateTime instances. - $date = twig_date_converter($env, $date); - $now = twig_date_converter($env, $now); + $date = $env->getExtension(CoreExtension::class)->convertDate($date); + $now = $env->getExtension(CoreExtension::class)->convertDate($now); // Get the difference between the two DateTime objects. $diff = $date->diff($now); From c89b60d52b4bf892f2689f40bb476d9cfac08f26 Mon Sep 17 00:00:00 2001 From: Jurj-Bogdan Date: Wed, 31 Jul 2024 17:04:46 +0300 Subject: [PATCH 3/4] issue-46 Signed-off-by: Jurj-Bogdan --- composer.json | 2 +- psalm.xml | 1 - src/Extension/DateExtension.php | 57 ++++++++++++++++++++++++++-- test/Extension/DateExtensionTest.php | 12 +++++- 4 files changed, 66 insertions(+), 6 deletions(-) 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.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 a96036c..02d0a95 100644 --- a/src/Extension/DateExtension.php +++ b/src/Extension/DateExtension.php @@ -4,12 +4,20 @@ 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 ctype_digit; +use function strtolower; +use function substr; + class DateExtension extends AbstractExtension { public static array $units = [ @@ -31,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|DateTimeInterface|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 = $env->getExtension(CoreExtension::class)->convertDate($date); - $now = $env->getExtension(CoreExtension::class)->convertDate($now); + $date = $this->convertDate($date, $timezone); + $now = $this->convertDate($now, $timezone); // Get the difference between the two DateTime objects. $diff = $date->diff($now); @@ -56,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 From 95f5177f2f11f05571e07bbd20fdd2c1cbc17713 Mon Sep 17 00:00:00 2001 From: Jurj-Bogdan Date: Wed, 31 Jul 2024 18:22:14 +0300 Subject: [PATCH 4/4] removed psalm-baseline.xml Signed-off-by: Jurj-Bogdan --- psalm-baseline.xml | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 psalm-baseline.xml diff --git a/psalm-baseline.xml b/psalm-baseline.xml deleted file mode 100644 index 7bf39a8..0000000 --- a/psalm-baseline.xml +++ /dev/null @@ -1,2 +0,0 @@ - -