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

feat: timestamp getters for PostObjectInterface #1283

Merged
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
38 changes: 21 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions library/Controller/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ public function getDate($posts, $archiveProps)
);
}

$post->archiveDate = $this->tryFormatDateToUnixTimestamp($post->archiveDate);
$post->archiveDateFormat = $archiveProps->dateFormat ?? 'default';

$preparedPosts[] = $post;
Expand All @@ -672,6 +673,41 @@ public function getDate($posts, $archiveProps)
return $preparedPosts;
}

/**
* Try to format a date to a unix timestamp
*
* @param mixed $date The date to format
* @return int|null The formatted date as a unix timestamp or null if the date could not be formatted
*/
public function tryFormatDateToUnixTimestamp(mixed $date): ?int
{
if (is_int($date)) {
return $date;
}

$date = str_ireplace($this->getLiteralDateStringReplaceMap()[0], $this->getLiteralDateStringReplaceMap()[1], $date);

return strtotime($date) ?: null;
}

/**
* Get the literal date string replace map
* @return array
*/
private function getLiteralDateStringReplaceMap(): array
{
$wpService = \Municipio\Helper\WpService::get();
$literals = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ];
$literalsShort = array_map(fn($literal) => substr($literal, 0, 3), $literals);
$translated = array_map(fn($literal) => $wpService->__($literal), $literals);
$translatedShort = array_map(fn($literal) => $wpService->__(substr($literal, 0, 3)), $literals);

$search = array_merge($translated, $translatedShort);
$replace = array_merge($literals, $literalsShort);

return [$search, $replace];
}

/**
* Switch between different date formats
*
Expand Down
113 changes: 113 additions & 0 deletions library/Controller/Archive.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Municipio\Controller;

use Municipio\Helper\WpService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use WpService\Implementations\FakeWpService;

class ArchiveTest extends TestCase
{
/**
* @testdox tryFormatDateToUnixTimestamp() returns a unix timestamp when given a valid date string
* @dataProvider provideValidDateStrings
*/
public function testTryFormatDateToUnixTimestampReturnsUnixTimestampFromString($dateString)
{
WpService::set($this->getWpServiceWithTranslatedDateStrings());
$archive = $this->getArchiveWithoutConstructor();
$this->assertIsInt($archive->tryFormatDateToUnixTimestamp($dateString));
}

/**
* @testdox tryFormatDateToUnixTimestamp() returns the same int as provided
*/
public function testTryFormatDateToUnixTimestampReturnsSameIntAsProvided()
{
$archive = $this->getArchiveWithoutConstructor();
$this->assertEquals(1234567890, $archive->tryFormatDateToUnixTimestamp(1234567890));
}

/**
* @testdox tryFormatDateToUnixTimestamp() returns null on invalid date string
*/
public function testTryFormatDateToUnixTimestampReturnsNullOnInvalidString()
{
$archive = $this->getArchiveWithoutConstructor();
$this->assertNull($archive->tryFormatDateToUnixTimestamp('invalid date string'));
}

private function getWpServiceWithTranslatedDateStrings()
{
return new FakeWpService(['__' => fn($string) =>
match ($string) {
'January' => 'Januari',
'February' => 'Februari',
'March' => 'Mars',
'April' => 'April',
'May' => 'Maj',
'June' => 'Juni',
'July' => 'Juli',
'August' => 'Augusti',
'September' => 'September',
'October' => 'Oktober',
'November' => 'November',
'December' => 'December',
'Jan' => 'Jan',
'Feb' => 'Feb',
'Mar' => 'Mar',
'Apr' => 'Apr',
'May' => 'Maj',
'Jun' => 'Jun',
'Jul' => 'Jul',
'Aug' => 'Aug',
'Sep' => 'Sep',
'Oct' => 'Okt',
'Nov' => 'Nov',
'Dec' => 'Dec',
'Monday' => 'Måndag',
'Tuesday' => 'Tisdag',
'Wednesday' => 'Onsdag',
'Thursday' => 'Torsdag',
'Friday' => 'Fredag',
'Saturday' => 'Lördag',
'Sunday' => 'Söndag',
'Mon' => 'Mån',
'Tue' => 'Tis',
'Wed' => 'Ons',
'Thu' => 'Tor',
'Fri' => 'Fre',
'Sat' => 'Lör',
'Sun' => 'Sön',
}
]);
}

public function provideValidDateStrings(): array
{
return [
'2020-01-01' => ['2020-01-01'],
'2020-01-01 00:00:00' => ['2020-01-01 00:00:00'],
'2020-01-01 00:00:00.000' => ['2020-01-01 00:00:00.000'],
'2020-01-01T00:00:00' => ['2020-01-01T00:00:00'],
'2020-01-01T00:00:00.000' => ['2020-01-01T00:00:00.000'],
'January 1, 2020' => ['January 1, 2020'],
'Januari 1, 2020' => ['Januari 1, 2020'],
'Okt 1, 2020' => ['Okt 1, 2020'],
'Monday, January 1, 2020' => ['Monday, January 1, 2020'],
'Tisdag, Januari 2, 2020' => ['Tisdag, Januari 2, 2020'],
'Mon, January 1, 2020' => ['Mon, January 1, 2020'],
'Mån, Januari 27, 2025' => ['Mån, Januari 27, 2025'],
'mån, januari 27, 2025' => ['mån, januari 27, 2025'],
];
}

private function getArchiveWithoutConstructor(): Archive|MockObject
{
return $this->getMockBuilder(Archive::class)
->disableOriginalConstructor()
->onlyMethods(['__construct'])
->getMock();
}
}
16 changes: 16 additions & 0 deletions library/PostObject/Decorators/BackwardsCompatiblePostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,20 @@ public function getBlogId(): int
{
return $this->postObject->getBlogId();
}

/**
* @inheritDoc
*/
public function getPublishedTime(bool $gmt = false): int
{
return $this->postObject->getPublishedTime($gmt);
}

/**
* @inheritDoc
*/
public function getModifiedTime(bool $gmt = false): int
{
return $this->postObject->getModifiedTime($gmt);
}
}
16 changes: 16 additions & 0 deletions library/PostObject/Decorators/IconResolvingPostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ public function getIcon(): ?IconInterface
{
return $this->iconResolver->resolve();
}

/**
* @inheritDoc
*/
public function getPublishedTime(bool $gmt = false): int
{
return $this->postObject->getPublishedTime($gmt);
}

/**
* @inheritDoc
*/
public function getModifiedTime(bool $gmt = false): int
{
return $this->postObject->getModifiedTime($gmt);
}
}
16 changes: 16 additions & 0 deletions library/PostObject/Decorators/PostObjectFromOtherBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ private function restore(): void
{
$this->wpService->restoreCurrentBlog();
}

/**
* @inheritDoc
*/
public function getPublishedTime(bool $gmt = false): int
{
return $this->postObject->getPublishedTime($gmt);
}

/**
* @inheritDoc
*/
public function getModifiedTime(bool $gmt = false): int
{
return $this->postObject->getModifiedTime($gmt);
}
}
16 changes: 16 additions & 0 deletions library/PostObject/Decorators/PostObjectFromWpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ public function getBlogId(): int
{
return $this->postObject->getBlogId();
}

/**
* @inheritDoc
*/
public function getPublishedTime(bool $gmt = false): int
{
return strtotime($gmt ? $this->wpPost->post_date_gmt : $this->wpPost->post_date);
}

/**
* @inheritDoc
*/
public function getModifiedTime(bool $gmt = false): int
{
return strtotime($gmt ? $this->wpPost->post_modified_gmt : $this->wpPost->post_modified);
}
}
Loading
Loading