-
-
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.
Replace ChoiceEnumTrait & SimpleChoiceEnum by AutoDiscoveredReadables…
…Trait
- Loading branch information
Showing
13 changed files
with
189 additions
and
387 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?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; | ||
|
||
/** | ||
* Auto-discover enumerated readable values from public constants. | ||
* | ||
* Meant to be used within a {@link \Elao\Enum\ReadableEnumInterface} implementation. | ||
*/ | ||
trait AutoDiscoveredReadablesTrait | ||
{ | ||
use AutoDiscoveredValuesTrait { | ||
AutoDiscoveredValuesTrait::values as autodiscoveredValues; | ||
} | ||
|
||
/** @internal */ | ||
private static $guessedReadables = []; | ||
|
||
/** @internal */ | ||
private static $called = 0; | ||
|
||
/** | ||
* @see ReadableEnumInterface::readables() | ||
* | ||
* @return string[] labels indexed by enumerated value | ||
*/ | ||
public static function readables(): array | ||
{ | ||
static::checkForChoiceEnumTraitMisuses(); | ||
|
||
return static::autodiscoveredReadables(); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private static function autodiscoveredReadables(): array | ||
{ | ||
$enumType = static::class; | ||
|
||
++self::$called; | ||
/** | ||
* The {@link AutoDiscoveredReadablesTrait::$called} property and the following is required | ||
* in order to preserve BC, especially for the FlaggedEnum. | ||
* It ensures overriding the {@link \Elao\Enum\ReadableEnum::values} method is taken into account | ||
* and prevents an infinite loop otherwise. | ||
* However, it's still a BC break in case of any parent::values call... | ||
* | ||
* @see \Elao\Enum\Tests\Unit\FlaggedEnumTest::testWithSpecifiedValues | ||
*/ | ||
$values = self::$called > 1 ? static::autodiscoveredValues() : static::values(); | ||
self::$called = 0; | ||
|
||
if (!isset(self::$guessedReadables[$enumType])) { | ||
$constants = (new \ReflectionClass($enumType))->getConstants(); | ||
foreach ($values as $value) { | ||
$constantName = array_search($value, $constants, true); | ||
self::$guessedReadables[$enumType][$value] = ucfirst(strtolower(str_replace('_', ' ', $constantName))); | ||
} | ||
} | ||
|
||
return self::$guessedReadables[$enumType]; | ||
} | ||
|
||
/** | ||
* @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.', | ||
self::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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.