1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 21:04:03 +00:00
This commit is contained in:
Samuel Štancl 2022-02-20 20:58:57 +01:00
parent f9fe9e5cab
commit 41b423da38
11 changed files with 146 additions and 91 deletions

28
src/InvokableCases.php Normal file
View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace ArchTech\Enums;
trait InvokableCases
{
/** Return the enum's value when it's $invoked(). */
public function __invoke()
{
return $this->value;
}
/** Return the enum's value 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;
}
}
throw new Exceptions\UndefinedCaseError(static::class, $name);
}
}