-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Add ChoiceEnumTrait & SimpleChoiceEnum #49
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum; | ||
|
||
use Elao\Enum\Exception\LogicException; | ||
|
||
/** | ||
* Discover readable enumerated values by returning the enumerated values as keys and their labels as values | ||
* in {@link \Elao\Enum\ChoiceEnumTrait::choices()}, replacing the need to provide both: | ||
* - {@link \Elao\Enum\ReadableEnumInterface::readables()} | ||
* - {@link \Elao\Enum\ReadableEnumInterface::values()} | ||
* | ||
* Meant to be used within a {@link \Elao\Enum\ReadableEnumInterface} implementation. | ||
*/ | ||
trait ChoiceEnumTrait | ||
{ | ||
/** | ||
* @see EnumInterface::values() | ||
* | ||
* @return int[]|string[] | ||
*/ | ||
public static function values(): array | ||
{ | ||
self::checkForChoiceEnumTraitMisuses(); | ||
|
||
$values = array_keys(static::choices()); | ||
|
||
if (is_a(static::class, FlaggedEnum::class, true)) { | ||
$values = array_values(array_filter($values, function ($v): bool { | ||
return 0 === ($v & $v - 1); | ||
})); | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* @see ReadableEnumInterface::readables() | ||
* | ||
* @return string[] labels indexed by enumerated value | ||
*/ | ||
public static function readables(): array | ||
{ | ||
self::checkForChoiceEnumTraitMisuses(); | ||
|
||
return static::choices(); | ||
} | ||
|
||
/** | ||
* @return string[] The enumerated values as keys and their labels as values. | ||
*/ | ||
abstract protected static function choices(): array; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private static function checkForChoiceEnumTraitMisuses() | ||
{ | ||
if (!is_a(static::class, ReadableEnumInterface::class, true)) { | ||
throw new LogicException(sprintf( | ||
'The "%s" trait is meant to be used by "%s" implementations, but "%s" does not implement it.', | ||
ChoiceEnumTrait::class, | ||
ReadableEnumInterface::class, | ||
static::class | ||
)); | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "elao/enum" package. | ||
* | ||
* Copyright (C) Elao | ||
* | ||
* @author Elao <contact@elao.com> | ||
*/ | ||
|
||
namespace Elao\Enum; | ||
|
||
/** | ||
* An opinionated enum implementation: | ||
* | ||
* - auto-discovers enumerated values from public constants. | ||
* - implements {@link \Elao\Enum\ReadableEnumInterface} with default labels | ||
* identical to enumerated values's constant name. | ||
*/ | ||
class SimpleChoiceEnum extends ReadableEnum | ||
{ | ||
use AutoDiscoveredValuesTrait; | ||
use ChoiceEnumTrait { | ||
ChoiceEnumTrait::values insteadof AutoDiscoveredValuesTrait; | ||
} | ||
} |
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,112 @@ | ||
<?php | ||
|
||
/* | ||
* 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\ChoiceEnumTrait; | ||
use Elao\Enum\Enum; | ||
use Elao\Enum\FlaggedEnum; | ||
use Elao\Enum\ReadableEnum; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ChoiceEnumTraitTest extends TestCase | ||
{ | ||
public function testItProvidesValuesAndReadablesImplementations() | ||
{ | ||
$this->assertSame(['foo', 'bar', 'baz'], ChoiceEnum::values()); | ||
$this->assertSame([ | ||
ChoiceEnum::FOO => 'Foo label', | ||
ChoiceEnum::BAR => 'Bar label', | ||
ChoiceEnum::BAZ => 'Baz label', | ||
], ChoiceEnum::readables()); | ||
} | ||
|
||
public function testItFiltersValuesForFlaggedEnumImplementations() | ||
{ | ||
$this->assertSame([1, 2, 4], FlaggedEnumWithChoiceEnumTrait::values()); | ||
$this->assertSame([ | ||
FlaggedEnumWithChoiceEnumTrait::EXECUTE => 'Execute', | ||
FlaggedEnumWithChoiceEnumTrait::WRITE => 'Write', | ||
FlaggedEnumWithChoiceEnumTrait::READ => 'Read', | ||
FlaggedEnumWithChoiceEnumTrait::WRITE | FlaggedEnumWithChoiceEnumTrait::READ => 'Read & write', | ||
FlaggedEnumWithChoiceEnumTrait::EXECUTE | FlaggedEnumWithChoiceEnumTrait::READ => 'Read & execute', | ||
FlaggedEnumWithChoiceEnumTrait::ALL => 'All permissions', | ||
], FlaggedEnumWithChoiceEnumTrait::readables()); | ||
} | ||
|
||
/** | ||
* @expectedException \Elao\Enum\Exception\LogicException | ||
* @expectedExceptionMessage The "Elao\Enum\ChoiceEnumTrait" trait is meant to be used by "Elao\Enum\ReadableEnumInterface" implementations, but "Elao\Enum\Tests\Unit\NonReadableEnumWithChoiceEnumTrait" does not implement it. | ||
*/ | ||
public function testValuesThrowsOnNonReadable() | ||
{ | ||
NonReadableEnumWithChoiceEnumTrait::values(); | ||
} | ||
|
||
/** | ||
* @expectedException \Elao\Enum\Exception\LogicException | ||
* @expectedExceptionMessage The "Elao\Enum\ChoiceEnumTrait" trait is meant to be used by "Elao\Enum\ReadableEnumInterface" implementations, but "Elao\Enum\Tests\Unit\NonReadableEnumWithChoiceEnumTrait" does not implement it. | ||
*/ | ||
public function testReadableThrowsOnNonReadable() | ||
{ | ||
NonReadableEnumWithChoiceEnumTrait::readables(); | ||
} | ||
} | ||
|
||
final class ChoiceEnum extends ReadableEnum | ||
{ | ||
use ChoiceEnumTrait; | ||
|
||
const FOO = 'foo'; | ||
const BAR = 'bar'; | ||
const BAZ = 'baz'; | ||
|
||
protected static function choices(): array | ||
{ | ||
return [ | ||
static::FOO => 'Foo label', | ||
static::BAR => 'Bar label', | ||
static::BAZ => 'Baz label', | ||
]; | ||
} | ||
} | ||
|
||
final class FlaggedEnumWithChoiceEnumTrait extends FlaggedEnum | ||
{ | ||
use ChoiceEnumTrait; | ||
|
||
const EXECUTE = 1; | ||
const WRITE = 2; | ||
const READ = 4; | ||
|
||
const ALL = self::EXECUTE | self::WRITE | self::READ; | ||
|
||
public static function choices(): array | ||
{ | ||
return [ | ||
static::EXECUTE => 'Execute', | ||
static::WRITE => 'Write', | ||
static::READ => 'Read', | ||
static::WRITE | static::READ => 'Read & write', | ||
static::EXECUTE | static::READ => 'Read & execute', | ||
static::ALL => 'All permissions', | ||
]; | ||
} | ||
} | ||
|
||
final class NonReadableEnumWithChoiceEnumTrait extends Enum | ||
{ | ||
use ChoiceEnumTrait; | ||
|
||
protected static function choices(): array | ||
{ | ||
return ['foo' => 'Foo label']; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First time I see this kind of trait guarding. But I guess it is needed, since at this point trait relationships are starting to be complicated. So 👍