Skip to content

Commit 9fbb0c7

Browse files
authored
Merge pull request #2235 from Cyberking99/feature/forward-datetime-calls
Forward DateString method calls to DateTime instances
2 parents f2ea9cc + d80e269 commit 9fbb0c7

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ This serves two purposes:
6565
- The `publish:views` command is now interactive on Unix-like systems in [#2062](https://github.com/hydephp/develop/pull/2062)
6666
- You can now add custom posts to the blog post feed component when including it directly in [#1893](https://github.com/hydephp/develop/pull/1893)
6767
- You can now specify sidebar item priorities by adding a numeric prefix to documentation page source file names in [#1709](https://github.com/hydephp/develop/pull/1709)
68+
- You can now forward method calls to the underlying `DateTime` instance in `DateString` instances in [#2235](https://github.com/hydephp/develop/pull/2235)
6869

6970
### Changed
7071

packages/framework/src/Framework/Features/XmlGenerators/RssFeedGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function addDynamicItemData(SimpleXMLElement $item, MarkdownPost $post
6565
}
6666

6767
if (isset($post->date)) {
68-
$this->addChild($item, 'pubDate', $post->date->dateTimeObject->format(DATE_RSS));
68+
$this->addChild($item, 'pubDate', $post->date->format(DATE_RSS));
6969
}
7070

7171
if (isset($post->author)) {

packages/framework/src/Pages/MarkdownPost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class MarkdownPost extends BaseMarkdownPage implements BlogPostSchema
3737
public static function getLatestPosts(): PageCollection
3838
{
3939
return static::all()->sortByDesc(function (self $post): int {
40-
return $post->date?->dateTimeObject->getTimestamp() ?? 0;
40+
return $post->date?->getTimestamp() ?? 0;
4141
});
4242
}
4343

packages/framework/src/Support/Models/DateString.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateTime;
88
use Stringable;
9+
use BadMethodCallException;
910

1011
/**
1112
* Parse a date string and create normalized formats.
@@ -46,4 +47,13 @@ public function __toString(): string
4647
{
4748
return $this->short;
4849
}
50+
51+
public function __call(string $method, array $arguments): mixed
52+
{
53+
if (method_exists($this->dateTimeObject, $method)) {
54+
return $this->dateTimeObject->$method(...$arguments);
55+
}
56+
57+
throw new BadMethodCallException("Method {$method} does not exist on the DateTime object.");
58+
}
4959
}

packages/framework/tests/Unit/DateStringTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,36 @@ public function testItCanFormatDateStringIntoShortHumanReadableStringUsingMagicM
4848
$dateString = new DateString('2022-01-01 UTC');
4949
$this->assertSame('Jan 1st, 2022', (string) $dateString);
5050
}
51+
52+
public function testItCanForwardGetTimestampMethodToDateTimeObject()
53+
{
54+
$dateString = new DateString('2020-01-01 00:00:00 UTC');
55+
$expected = (new DateTime('2020-01-01 00:00:00 UTC'))->getTimestamp();
56+
$this->assertSame($expected, $dateString->getTimestamp());
57+
}
58+
59+
public function testItCanForwardModifyMethodToDateTimeObject()
60+
{
61+
$dateString = new DateString('2020-01-01 UTC');
62+
$modified = $dateString->modify('+1 day');
63+
$this->assertInstanceOf(DateTime::class, $modified);
64+
$this->assertSame('2020-01-02', $modified->format('Y-m-d'));
65+
}
66+
67+
public function testItCanForwardSetTimeMethodToDateTimeObject()
68+
{
69+
$dateString = new DateString('2020-01-01 UTC');
70+
$modified = $dateString->setTime(15, 30);
71+
$this->assertInstanceOf(DateTime::class, $modified);
72+
$this->assertSame('15:30:00', $modified->format('H:i:s'));
73+
}
74+
75+
public function testCallingUndefinedMethodThrowsException()
76+
{
77+
$this->expectException(\BadMethodCallException::class);
78+
$this->expectExceptionMessage('Method nonExistentMethod does not exist on the DateTime object.');
79+
80+
$dateString = new DateString('2020-01-01 UTC');
81+
$dateString->nonExistentMethod();
82+
}
5183
}

0 commit comments

Comments
 (0)