Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #47

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 0 additions & 9 deletions psalm-baseline.xml

This file was deleted.

1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
<projectFiles>
<directory name="src" />
Expand Down
57 changes: 53 additions & 4 deletions src/Extension/DateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion test/Extension/DateExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DotTest\Twig\Extension;

use DateTimeImmutable;
use Dot\Twig\Extension\DateExtension;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -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
Expand Down
Loading