mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 21:04:03 +00:00
Extend functionality to work with pure enums (#6)
* Extend functionality to work with pure enums * Code review changes Altered `::values()` to act like `::names()` for backed enums, code style fixes, general cleanups
This commit is contained in:
parent
20fa78c2b1
commit
0c87357a6e
9 changed files with 109 additions and 16 deletions
|
|
@ -4,22 +4,24 @@ declare(strict_types=1);
|
|||
|
||||
namespace ArchTech\Enums;
|
||||
|
||||
use BackedEnum;
|
||||
|
||||
trait InvokableCases
|
||||
{
|
||||
/** Return the enum's value when it's $invoked(). */
|
||||
public function __invoke()
|
||||
{
|
||||
return $this->value;
|
||||
return $this instanceof BackedEnum ? $this->value : $this->name;
|
||||
}
|
||||
|
||||
/** Return the enum's value when it's called ::STATICALLY(). */
|
||||
/** Return the enum's value or name when it's called ::STATICALLY(). */
|
||||
public static function __callStatic($name, $args)
|
||||
{
|
||||
$cases = static::cases();
|
||||
|
||||
foreach ($cases as $case) {
|
||||
if ($case->name === $name) {
|
||||
return $case->value;
|
||||
return $case instanceof BackedEnum ? $case->value : $case->name;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue