Skip to content

Commit

Permalink
feat: post object date format
Browse files Browse the repository at this point in the history
  • Loading branch information
NiclasNorin committed Feb 5, 2025
1 parent 775ef54 commit 86bcefa
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 9 deletions.
11 changes: 8 additions & 3 deletions library/Helper/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Municipio\PostObject\Date\CachedArchiveDateSettingResolver;
use Municipio\PostObject\Date\CachedTimestampResolver;
use Municipio\PostObject\Date\TimestampResolver;
use Municipio\PostObject\Decorators\PostObjectArchiveDateFormat;
use Municipio\PostObject\Decorators\PostObjectArchiveDateTimestamp;

/**
Expand Down Expand Up @@ -168,9 +169,13 @@ private static function convertWpPostToPostObject(WP_Post $post, string $cacheGr

$archiveDateSettingResolver = new ArchiveDateSettingResolver($postObject, $wpService);
$archiveDateSettingResolver = new CachedArchiveDateSettingResolver($postObject, $wpService, $archiveDateSettingResolver);
$timestampResolver = new TimestampResolver($postObject, $wpService, $archiveDateSettingResolver);
$timestampResolver = new CachedTimestampResolver($postObject, $wpService, $timestampResolver);
$postObject = new PostObjectArchiveDateTimestamp($postObject, $wpService, $timestampResolver);

$postObject = new PostObjectArchiveDateFormat($postObject, $archiveDateSettingResolver);

$timestampResolver = new TimestampResolver($postObject, $wpService, $archiveDateSettingResolver);
$timestampResolver = new CachedTimestampResolver($postObject, $wpService, $timestampResolver);

$postObject = new PostObjectArchiveDateTimestamp($postObject, $timestampResolver);

$iconResolver = new TermIconResolver($postObject, $wpService, new Term($wpService, AcfService::get()), new NullIconResolver());
$iconResolver = new PostIconResolver($postObject, $acfService, $iconResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,12 @@ public function getArchiveDateTimestamp(): int
{
return $this->postObject->getArchiveDateTimestamp();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->postObject->getArchiveDateFormat();
}
}
8 changes: 8 additions & 0 deletions library/PostObject/Decorators/IconResolvingPostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,12 @@ public function getArchiveDateTimestamp(): int
{
return $this->postObject->getArchiveDateTimestamp();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->postObject->getArchiveDateFormat();
}
}
118 changes: 118 additions & 0 deletions library/PostObject/Decorators/PostObjectArchiveDateFormat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;
use Municipio\PostObject\Date\ArchiveDateSettingResolverInterface;

/**
* PostObjectWithSeoRedirect class.
*
* Applies the SEO redirect to the post object permalink if a redirect is set.
*/
class PostObjectArchiveDateFormat implements PostObjectInterface
{
/**
* Constructor.
*/
public function __construct(
private PostObjectInterface $postObject,
private ArchiveDateSettingResolverInterface $dateSettingResolver
) {
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getId(): int
{
return $this->postObject->getId();
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getTitle(): string
{
return $this->postObject->getTitle();
}

/**
* @inheritDoc
*/
public function getPermalink(): string
{
return $this->postObject->getPermalink();
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getCommentCount(): int
{
return $this->postObject->getCommentCount();
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getPostType(): string
{
return $this->postObject->getPostType();
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getBlogId(): int
{
return $this->postObject->getBlogId();
}

/**
* @inheritDoc
* @codeCoverageIgnore
*/
public function getIcon(): ?IconInterface
{
return $this->postObject->getIcon();
}

/**
* @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);
}

/**
* @inheritDoc
*/
public function getArchiveDateTimestamp(): int
{
return $this->postObject->getArchiveDateTimestamp();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->dateSettingResolver->resolve();
}
}
34 changes: 34 additions & 0 deletions library/PostObject/Decorators/PostObjectArchiveDateFormat.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Date\ArchiveDateSettingResolverInterface;
use Municipio\PostObject\PostObject;
use PHPUnit\Framework\TestCase;
use WpService\Implementations\FakeWpService;

class PostObjectArchiveDateFormatTest extends TestCase
{
/**
* @testdox class can be instantiated
*/
public function testClassCanBeInstantiated()
{
$resolver = $this->createMock(ArchiveDateSettingResolverInterface::class);
$decorator = new PostObjectArchiveDateFormat(new PostObject(new FakeWpService()), $resolver);
$this->assertInstanceOf(PostObjectArchiveDateFormat::class, $decorator);
}

/**
* @testdox getDateTimestamp returns unix timestamp
*/
public function testReturnsFormat()
{
$resolver = $this->createMock(ArchiveDateSettingResolverInterface::class);
$resolver->method('resolve')->willReturn('H:i');

$decorator = new PostObjectArchiveDateFormat(new PostObject(new FakeWpService()), $resolver);

$this->assertEquals('H:i', $decorator->getArchiveDateFormat());
}
}
12 changes: 8 additions & 4 deletions library/PostObject/Decorators/PostObjectArchiveDateTimestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;
use WpService\Contracts\GetPostMeta;
use Municipio\PostObject\Date\TimestampResolverInterface;

/**
Expand All @@ -14,14 +13,11 @@
*/
class PostObjectArchiveDateTimestamp implements PostObjectInterface
{
private static $archiveDateSettings = [];

/**
* Constructor.
*/
public function __construct(
private PostObjectInterface $postObject,
private GetPostMeta $wpService,
private TimestampResolverInterface $timestampResolver
) {
}
Expand Down Expand Up @@ -111,4 +107,12 @@ public function getArchiveDateTimestamp(): int
{
return $this->timestampResolver->resolve();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->postObject->getArchiveDateFormat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PostObjectArchiveDateTimestampTest extends TestCase
public function testClassCanBeInstantiated()
{
$resolver = $this->createMock(TimestampResolverInterface::class);
$decorator = new PostObjectArchiveDateTimestamp(new PostObject(new FakeWpService()), new FakeWpService(), $resolver);
$decorator = new PostObjectArchiveDateTimestamp(new PostObject(new FakeWpService()), $resolver);
$this->assertInstanceOf(PostObjectArchiveDateTimestamp::class, $decorator);
}

Expand All @@ -27,7 +27,7 @@ public function testReturnUnixTimestamp()
$resolver = $this->createMock(TimestampResolverInterface::class);
$resolver->method('resolve')->willReturn(123);

$decorator = new PostObjectArchiveDateTimestamp(new PostObject(new FakeWpService()), new FakeWpService(), $resolver);
$decorator = new PostObjectArchiveDateTimestamp(new PostObject(new FakeWpService()), $resolver);

$this->assertEquals(123, $decorator->getArchiveDateTimestamp());
}
Expand Down
8 changes: 8 additions & 0 deletions library/PostObject/Decorators/PostObjectFromOtherBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ public function getArchiveDateTimestamp(): int
{
return $this->postObject->getArchiveDateTimestamp();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->postObject->getArchiveDateFormat();
}
}
8 changes: 8 additions & 0 deletions library/PostObject/Decorators/PostObjectFromWpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public function getArchiveDateTimestamp(): int
{
return $this->postObject->getArchiveDateTimestamp();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->postObject->getArchiveDateFormat();
}
}
8 changes: 8 additions & 0 deletions library/PostObject/Decorators/PostObjectWithSeoRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,12 @@ public function getArchiveDateTimestamp(): int
{
return $this->postObject->getArchiveDateTimestamp();
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return $this->postObject->getArchiveDateFormat();
}
}
8 changes: 8 additions & 0 deletions library/PostObject/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,12 @@ public function getArchiveDateTimestamp(): int
{
return 0;
}

/**
* @inheritDoc
*/
public function getArchiveDateFormat(): string
{
return 'Y-m-d H:i';
}
}
16 changes: 16 additions & 0 deletions library/PostObject/PostObject.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ public function testGetBlogIdReturns1()
{
$this->assertEquals(1, $this->instance->getBlogId());
}

/**
* @testdox getArchiveDateTimestamp() returns 0
*/
public function testGetArchiveDateTimestampReturns0()
{
$this->assertEquals(0, $this->instance->getArchiveDateTimestamp());
}

/**
* @testdox getArchiveDateFormat() returns default format
*/
public function testGetArchiveDateFormatReturnsDefaultFormat()
{
$this->assertEquals('Y-m-d H:i', $this->instance->getArchiveDateFormat());
}
}
7 changes: 7 additions & 0 deletions library/PostObject/PostObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ public function getModifiedTime(bool $gmt = false): int;
* @return int
*/
public function getArchiveDateTimestamp(): int;

/**
* Get the post object date format.
*
* @return string
*/
public function getArchiveDateFormat(): string;
}

0 comments on commit 86bcefa

Please sign in to comment.