-
-
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.
Add ChoiceEnumTrait & SimpleChoiceEnum
- Loading branch information
Showing
8 changed files
with
395 additions
and
2 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
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.