Skip to content

Commit

Permalink
Merge pull request #48 from Jurj-Bogdan/ossbadge
Browse files Browse the repository at this point in the history
psalm fix, replaced deprecated fuction call
  • Loading branch information
bidi47 authored Aug 1, 2024
2 parents 2b2924f + 95f5177 commit e668892
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
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

0 comments on commit e668892

Please sign in to comment.