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: add PostObjectInterface::getIcon() which returns an IconInterface #1233

Merged
merged 5 commits into from
Jan 9, 2025
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
3 changes: 3 additions & 0 deletions library/Helper/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use ComponentLibrary\Integrations\Image\Image as ImageComponentContract;
use Municipio\PostObject\TermIcon\TryGetTermIcon;
use Municipio\PostObject\Decorators\BackwardsCompatiblePostObject;
use Municipio\PostObject\Decorators\IconResolvingPostObject;
use Municipio\PostObject\Decorators\PostObjectFromWpPost;
use Municipio\PostObject\Decorators\PostObjectWithTermIcons;
use Municipio\PostObject\Icon\Resolvers\NullIconResolver;
use Municipio\PostObject\PostObject;
use Municipio\PostObject\PostObjectInterface;

Expand Down Expand Up @@ -151,6 +153,7 @@ private static function convertWpPostToPostObject(WP_Post $post, string $cacheGr

$postObject = new PostObjectFromWpPost(new PostObject(), $post, $wpService);
$postObject = new PostObjectWithTermIcons($postObject, $wpService, new TryGetTermIcon());
$postObject = new IconResolvingPostObject($postObject, new NullIconResolver());
$postObject = new BackwardsCompatiblePostObject($postObject, $camelCasedPost);

self::$runtimeCache[$cacheGroup][$cacheKey] = $postObject;
Expand Down
9 changes: 9 additions & 0 deletions library/PostObject/Decorators/AbstractPostObjectDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;
use Municipio\PostObject\TermIcon\TermIconInterface;

Expand Down Expand Up @@ -67,4 +68,12 @@ public function getTermIcon(?string $taxonomy = null): ?TermIconInterface
{
return $this->postObject->getTermIcon($taxonomy);
}

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

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\Icon\Resolvers\IconResolverInterface;
use Municipio\PostObject\PostObjectInterface;

/**
* IconResolvingPostObject
*/
class IconResolvingPostObject extends AbstractPostObjectDecorator implements PostObjectInterface
{
/**
* Constructor.
*/
public function __construct(PostObjectInterface $inner, private IconResolverInterface $iconResolver)
{
$this->postObject = $inner;
}

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

namespace Municipio\PostObject\Decorators;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\Icon\Resolvers\IconResolverInterface;
use Municipio\PostObject\Icon\Resolvers\NullIconResolver;
use Municipio\PostObject\PostObject;
use Municipio\PostObject\PostObjectInterface;
use PHPUnit\Framework\TestCase;

class IconResolvingPostObjectTest extends TestCase
{
/**
* @testdox class can be instantiated
*/
public function testClassCanBeInstantiated()
{
$decorator = new IconResolvingPostObject(new PostObject(), new NullIconResolver());
$this->assertInstanceOf(IconResolvingPostObject::class, $decorator);
}

/**
* @testdox getIcon() calls provided icon resolver
*/
public function testGetIconCallsProvidedIconResolver()
{
$icon = $this->createMock(IconInterface::class);
$iconResolver = $this->createMock(IconResolverInterface::class);

$icon->method('getIcon')->willReturn('test-icon');
$iconResolver->method('resolve')->willReturn($icon);

$postObject = new IconResolvingPostObject(new PostObject(), $iconResolver);

$this->assertEquals('test-icon', $postObject->getIcon()->getIcon());
}
}
55 changes: 55 additions & 0 deletions library/PostObject/Icon/IconInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Municipio\PostObject\Icon;

/**
* Interface that adhere to the Icon component atttibutes.
* @see https://styleguide.getmunicipio.com/components/atoms/icon
*/
interface IconInterface
{
/**
* Sizes: xs, sm, md, lg, xl, xxl
*/
public function getSize(): string;

/**
* A label on the icon
*/
public function getLabel(): string;

/**
* Get the term icon.
*/
public function getIcon(): string;

/**
* Get the term color.
*/
public function getColor(): string;

/**
* A custom HEX color
*/
public function getCustomColor(): string;

/**
* Icon HTML tag
*/
public function getComponentElement(): string;

/**
* If the icons should be filled or not
*/
public function getFilled(): bool;

/**
* If the icon is decorative only or serves a purpose.
*/
public function getDecorative(): bool;

/**
* Get the term ID.
*/
public function toArray(): array;
}
15 changes: 15 additions & 0 deletions library/PostObject/Icon/Resolvers/IconResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Municipio\PostObject\Icon\Resolvers;

use Municipio\PostObject\Icon\IconInterface;

interface IconResolverInterface
{
/**
* Resolve icon.
*
* @return IconInterface|null
*/
public function resolve(): ?IconInterface;
}
19 changes: 19 additions & 0 deletions library/PostObject/Icon/Resolvers/NullIconResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Municipio\PostObject\Icon\Resolvers;

use Municipio\PostObject\Icon\IconInterface;

/**
* Null icon resolver.
*/
class NullIconResolver implements IconResolverInterface
{
/**
* @inheritDoc
*/
public function resolve(): ?IconInterface
{
return null;
}
}
25 changes: 25 additions & 0 deletions library/PostObject/Icon/Resolvers/NullIconResolver.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Municipio\PostObject\Icon\Resolvers;

use PHPUnit\Framework\TestCase;

class NullIconResolverTest extends TestCase
{
/**
* @testdox class can be instantiated
*/
public function testClassCanBeInstantiated()
{
$this->assertInstanceOf(NullIconResolver::class, new NullIconResolver());
}

/**
* @testdox resolve() returns null
*/
public function testResolveReturnsNull()
{
$resolver = new NullIconResolver();
$this->assertNull($resolver->resolve());
}
}
9 changes: 9 additions & 0 deletions library/PostObject/PostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Municipio\PostObject;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\PostObjectInterface;
use Municipio\PostObject\TermIcon\TermIconInterface;

Expand Down Expand Up @@ -65,4 +66,12 @@ public function getTermIcon(?string $taxonomy = null): ?TermIconInterface
{
return null;
}

/**
* @inheritDoc
*/
public function getIcon(): ?IconInterface
{
return null;
}
}
8 changes: 8 additions & 0 deletions library/PostObject/PostObject.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ public function testGetTermIconReturnsNull()
{
$this->assertNull($this->instance->getTermIcon());
}

/**
* @testdox getIcon() returns null
*/
public function testGetIconReturnsNull()
{
$this->assertNull($this->instance->getIcon());
}
}
8 changes: 8 additions & 0 deletions library/PostObject/PostObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Municipio\PostObject;

use Municipio\PostObject\Icon\IconInterface;
use Municipio\PostObject\TermIcon\TermIconInterface;

interface PostObjectInterface
Expand Down Expand Up @@ -54,4 +55,11 @@ public function getTermIcon(?string $taxonomy = null): ?TermIconInterface;
* Get the post type.
*/
public function getPostType(): string;

/**
* Get the post object icon.
*
* @return IconInterface|null The post object icon or null if none is found.
*/
public function getIcon(): ?IconInterface;
}
Loading