-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #201 [Readable] Allow to configure a common suffix/prefix & u…
…se the enum case value/name as default label (ogizanagi) This PR was merged into the 2.x-dev branch. Discussion ---------- [Readable] Allow to configure a common suffix/prefix & use the enum case value/name as default label Eases the declaration of readable enum translations keys with common suffix/prefix: ```php #[ReadableEnum(prefix: 'suit.')] enum Suit: string implements ReadableEnumInterface { use ReadableEnumTrait; case Hearts = '♥︎'; case Diamonds = '♦︎'; case Clubs = '♣︎'; case Spades = '︎♠︎'; } Suit::Hearts->getReadable(); // returns 'suit.Hearts' Suit::Clubs->getReadable(); // returns 'suit.Clubs' ``` [Docs](https://github.com/Elao/PhpEnums/blob/readable-auto/README.md#configure-suffixprefix--default-value) ## Questions - Should we actually default on the `name` rather than the value by default when using this attribute? - Name this attribute `ConfigureReadableEnum`? `AutoReadableEnum`? - … Commits ------- deccd5a [Readable] Allow to configure a common suffix & prefix & use the enum case value/name as default label
- Loading branch information
Showing
4 changed files
with
252 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Attribute; | ||
|
||
/** | ||
* Autoconfigure a readable enum cases' labels, using the name or value + allow to configure a prefix and/or suffix for the key. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class ReadableEnum | ||
{ | ||
public function __construct( | ||
public readonly ?string $prefix = null, | ||
public readonly ?string $suffix = null, | ||
public readonly bool $useValueAsDefault = false | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum\Tests\Unit; | ||
|
||
use Elao\Enum\Attribute\EnumCase; | ||
use Elao\Enum\Attribute\ReadableEnum; | ||
use Elao\Enum\Exception\LogicException; | ||
use Elao\Enum\ReadableEnumInterface; | ||
use Elao\Enum\ReadableEnumTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ReadableEnumAttributeTest extends TestCase | ||
{ | ||
public function testReadableEnumAttribute(): void | ||
{ | ||
self::assertSame( | ||
'suit.Clubs.label', | ||
ReadableAttributeSuit::readableForValue(ReadableAttributeSuit::Clubs->value), | ||
'uses the name as default, with suffix and prefix', | ||
); | ||
self::assertSame( | ||
'suit.hearts.label', | ||
ReadableAttributeSuit::readableForValue(ReadableAttributeSuit::Hearts->value), | ||
'uses the explicit label value, with suffix and prefix', | ||
); | ||
} | ||
|
||
public function testReadableEnumAttributeWithoutSuffixPrefix(): void | ||
{ | ||
self::assertSame( | ||
'Clubs', | ||
ReadableAttributeSuitWithoutSuffixPrefix::readableForValue(ReadableAttributeSuitWithoutSuffixPrefix::Clubs->value), | ||
'uses the name as default, without any suffix or prefix', | ||
); | ||
self::assertSame( | ||
'hearts', | ||
ReadableAttributeSuitWithoutSuffixPrefix::readableForValue(ReadableAttributeSuitWithoutSuffixPrefix::Hearts->value), | ||
'uses the explicit label value, without any suffix or prefix', | ||
); | ||
} | ||
|
||
public function testReadableEnumAttributeWithValueAsDefault(): void | ||
{ | ||
self::assertSame( | ||
'suit.♣︎.label', | ||
ReadableSuitAttributeWithValueAsDefault::readableForValue(ReadableSuitAttributeWithValueAsDefault::Clubs->value), | ||
'uses the value as default, with suffix and prefix', | ||
); | ||
self::assertSame( | ||
'suit.hearts.label', | ||
ReadableSuitAttributeWithValueAsDefault::readableForValue(ReadableSuitAttributeWithValueAsDefault::Hearts->value), | ||
'uses the explicit label value, with suffix and prefix', | ||
); | ||
} | ||
|
||
public function testReadableEnumAttributeWithValueAsDefaultThrowsOnPureEnum(): void | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Cannot use "useValueAsDefault" with "#[Elao\Enum\Attribute\ReadableEnum]" attribute on enum "Elao\Enum\Tests\Unit\PureEnumWithReadableAttribute" as it\'s not a string backed enum.'); | ||
|
||
PureEnumWithReadableAttribute::Foo->getReadable(); | ||
} | ||
|
||
public function testReadableEnumAttributeWithValueAsDefaultThrowsOnIntBackedEnum(): void | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('Cannot use "useValueAsDefault" with "#[Elao\Enum\Attribute\ReadableEnum]" attribute on enum "Elao\Enum\Tests\Unit\IntBackedEnumWithReadableAttribute" as it\'s not a string backed enum.'); | ||
|
||
IntBackedEnumWithReadableAttribute::Foo->getReadable(); | ||
} | ||
} | ||
|
||
#[ReadableEnum(prefix: 'suit.', suffix: '.label')] | ||
enum ReadableAttributeSuit: string implements ReadableEnumInterface | ||
{ | ||
use ReadableEnumTrait; | ||
|
||
#[EnumCase('hearts')] | ||
case Hearts = '♥︎'; | ||
|
||
case Diamonds = '♦︎'; | ||
case Clubs = '♣︎'; | ||
case Spades = '︎♠︎'; | ||
} | ||
|
||
#[ReadableEnum] | ||
enum ReadableAttributeSuitWithoutSuffixPrefix: string implements ReadableEnumInterface | ||
{ | ||
use ReadableEnumTrait; | ||
|
||
#[EnumCase('hearts')] | ||
case Hearts = '♥︎'; | ||
|
||
case Diamonds = '♦︎'; | ||
case Clubs = '♣︎'; | ||
case Spades = '︎♠︎'; | ||
} | ||
|
||
#[ReadableEnum(prefix: 'suit.', suffix: '.label', useValueAsDefault: true)] | ||
enum ReadableSuitAttributeWithValueAsDefault: string implements ReadableEnumInterface | ||
{ | ||
use ReadableEnumTrait; | ||
|
||
#[EnumCase('hearts')] | ||
case Hearts = '♥︎'; | ||
|
||
case Diamonds = '♦︎'; | ||
case Clubs = '♣︎'; | ||
case Spades = '︎♠︎'; | ||
} | ||
|
||
#[ReadableEnum(useValueAsDefault: true)] | ||
enum PureEnumWithReadableAttribute implements ReadableEnumInterface | ||
{ | ||
use ReadableEnumTrait; | ||
|
||
case Foo; | ||
} | ||
|
||
#[ReadableEnum(useValueAsDefault: true)] | ||
enum IntBackedEnumWithReadableAttribute: int implements ReadableEnumInterface | ||
{ | ||
use ReadableEnumTrait; | ||
|
||
case Foo = 1; | ||
} |