Skip to content

Commit 20021ce

Browse files
authored
Added aeon_datetime_create filder (#68)
1 parent 856630d commit 20021ce

File tree

7 files changed

+112
-24
lines changed

7 files changed

+112
-24
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"prefer-stable": true,
77
"minimum-stability": "dev",
88
"require": {
9-
"php": ">=7.4.2",
10-
"aeon-php/calendar": ">=0.18.0",
9+
"php": "^7.4.2 || ^8.0",
10+
"aeon-php/calendar": "^1.0",
1111
"twig/twig": "^1.43|^2.10|^3"
1212
},
1313
"license": "MIT",

composer.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Aeon/Twig/CalendarExtension.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function __construct(Calendar $calendar, string $defaultTimeZone = 'UTC',
4646
public function getFilters() : array
4747
{
4848
return [
49+
new TwigFilter('aeon_datetime_create', [$this, 'aeon_datetime_create']),
4950
new TwigFilter('aeon_datetime_format', [$this, 'aeon_datetime_format']),
5051
new TwigFilter('aeon_day_format', [$this, 'aeon_day_format']),
5152
new TwigFilter('aeon_time_format', [$this, 'aeon_time_format']),
@@ -78,6 +79,39 @@ public function getFunctions() : array
7879
];
7980
}
8081

82+
/**
83+
* @param \DateTimeInterface|int|string $dateTime
84+
* @param null|string $timezone
85+
*
86+
* @throws InvalidArgumentException
87+
*
88+
* @return DateTime
89+
*
90+
* @psalm-suppress RedundantConditionGivenDocblockType
91+
*/
92+
public function aeon_datetime_create($dateTime, string $timezone = null) : DateTime
93+
{
94+
if (\is_string($dateTime)) {
95+
$aeonDateTime = DateTime::fromString($dateTime);
96+
} elseif ($dateTime instanceof \DateTimeInterface) {
97+
$aeonDateTime = DateTime::fromDateTime($dateTime);
98+
} elseif (\is_int($dateTime)) {
99+
$aeonDateTime = DateTime::fromTimestampUnix($dateTime);
100+
} else {
101+
throw new InvalidArgumentException("Expected string, \DateTimeInterface or integer, got " . \gettype($dateTime));
102+
}
103+
104+
$tz = (\is_string($timezone) && TimeZone::isValid($timezone))
105+
? TimeZone::fromString($timezone)
106+
: null;
107+
108+
if ($tz instanceof TimeZone) {
109+
return $aeonDateTime->toTimeZone($tz);
110+
}
111+
112+
return $aeonDateTime;
113+
}
114+
81115
public function aeon_datetime_format(DateTime $dateTime, string $format = null, string $timezone = null) : string
82116
{
83117
$tz = (\is_string($timezone) && TimeZone::isValid($timezone))

tests/Aeon/Twig/Tests/Integration/CalendarExtensionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ public function test_extension() : void
2828
$this->assertInstanceOf(ExtensionInterface::class, new CalendarExtension($this->calendarStub));
2929
}
3030

31+
public function test_filter_aeon_datetime_create() : void
32+
{
33+
$twig = new Environment(
34+
new FilesystemLoader(
35+
[
36+
__DIR__ . '/Fixtures/filters',
37+
]
38+
)
39+
);
40+
$twig->addExtension(new CalendarExtension($this->calendarStub, 'UTC', 'Y-m-d H:i:sO', 'Y-m-d', 'H:i:s'));
41+
42+
$this->assertStringEqualsFile(
43+
__DIR__ . '/Fixtures/filters/aeon_datetime_create.txt',
44+
$twig->render('aeon_datetime_create.twig.txt', [
45+
'phpDateTime' => new \DateTimeImmutable('2020-01-01 00:00:00'),
46+
])
47+
);
48+
}
49+
3150
public function test_filter_aeon_datetime() : void
3251
{
3352
$twig = new Environment(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ '2020-01-01 00:00:00' | aeon_datetime_create | aeon_datetime_format }}
2+
{{ 1577836800 | aeon_datetime_create | aeon_datetime_format }}
3+
{{ phpDateTime | aeon_datetime_create | aeon_datetime_format }}
4+
{{ '2020-01-01 00:00:00' | aeon_datetime_create('America/Los_Angeles') | aeon_datetime_format(null, 'America/Los_Angeles') }}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2020-01-01 00:00:00+0000
2+
2020-01-01 00:00:00+0000
3+
2020-01-01 00:00:00+0000
4+
2019-12-31 16:00:00-0800

tests/Aeon/Twig/Tests/Unit/CalendarExtensionTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,33 @@ public function test_create_with_invalid_timezone() : void
2424
new CalendarExtension($calendar = new GregorianCalendarStub(TimeZone::UTC()), 'not_timezone');
2525
}
2626

27+
public function test_aeon_create_from_string() : void
28+
{
29+
$extension = new CalendarExtension($calendar = new GregorianCalendarStub(TimeZone::UTC()));
30+
31+
$this->assertEquals(DateTime::fromString('2002-01-01 00:00:00 UTC'), $extension->aeon_datetime_create('2002-01-01 00:00:00 UTC'));
32+
}
33+
34+
public function test_aeon_create_from_string_with_tz() : void
35+
{
36+
$extension = new CalendarExtension($calendar = new GregorianCalendarStub(TimeZone::UTC()));
37+
38+
$this->assertEquals(
39+
DateTime::fromString('2001-12-31 16:00:00 America/Los_Angeles'),
40+
$extension->aeon_datetime_create('2002-01-01 00:00:00 UTC', 'America/Los_Angeles')
41+
);
42+
}
43+
44+
public function test_aeon_create_from_invalid_type() : void
45+
{
46+
$extension = new CalendarExtension($calendar = new GregorianCalendarStub(TimeZone::UTC()));
47+
48+
$this->expectException(InvalidArgumentException::class);
49+
$this->expectExceptionMessage('Expected string, \DateTimeInterface or integer, got array');
50+
51+
$extension->aeon_datetime_create(['array'], 'America/Los_Angeles');
52+
}
53+
2754
public function test_aeon_now() : void
2855
{
2956
$extension = new CalendarExtension($calendar = new GregorianCalendarStub(TimeZone::UTC()));

0 commit comments

Comments
 (0)