Add EnumEnhancement
This commit is contained in:
parent
ffc259e4cb
commit
db772fab53
2 changed files with 94 additions and 0 deletions
|
@ -53,6 +53,7 @@
|
|||
"middlewares/trailing-slash": "^2.0",
|
||||
"middlewares/whoops": "^2.0",
|
||||
"monolog/monolog": "^3.6",
|
||||
"othyn/php-enum-enhancements": "^1.0",
|
||||
"php-di/slim-bridge": "^3.0",
|
||||
"psr/cache": "^1.0",
|
||||
"psr/container": "^1.0",
|
||||
|
|
93
src/Enums/EnumEnhancement.php
Normal file
93
src/Enums/EnumEnhancement.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Benzine\Enum;
|
||||
|
||||
use Othyn\PhpEnumEnhancements\Traits\EnumEnhancements as OthynEnumEnhancements;
|
||||
|
||||
trait EnumEnhancements
|
||||
{
|
||||
use OthynEnumEnhancements;
|
||||
|
||||
public static function fromName(\BackedEnum | string $seekName)
|
||||
{
|
||||
if ($seekName instanceof \BackedEnum) {
|
||||
return $seekName;
|
||||
}
|
||||
foreach (self::cases() as $name => $status) {
|
||||
$name = is_string($status) ? $name : $status->name;
|
||||
$value = is_string($status) ? $status : $status->value;
|
||||
if ($name === $seekName) {
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
||||
// If there is a default, return it instead of throwing an error
|
||||
$hasDefault = false;
|
||||
foreach (self::cases() as $enum) {
|
||||
if ($enum->name === 'DEFAULT') {
|
||||
$hasDefault = true;
|
||||
}
|
||||
}
|
||||
if ($hasDefault) {
|
||||
return self::DEFAULT;
|
||||
}
|
||||
|
||||
\Kint::dump(sprintf("'%s' is not a valid backing key for enum %s", $seekName, self::class));
|
||||
|
||||
throw new \ValueError(sprintf("'%s' is not a valid backing key for enum %s", $seekName, self::class));
|
||||
}
|
||||
|
||||
public static function fromValue(\BackedEnum | int | string $seekValue)
|
||||
{
|
||||
if ($seekValue instanceof \BackedEnum) {
|
||||
return $seekValue;
|
||||
}
|
||||
foreach (self::cases() as $name => $status) {
|
||||
$name = is_string($status) ? $name : $status->name;
|
||||
$value = is_string($status) ? $status : $status->value;
|
||||
if ($value === $seekValue) {
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
||||
\Kint::dump(sprintf("'%s' is not a valid backing value for enum %s.", $seekValue, self::class));
|
||||
|
||||
throw new \ValueError(sprintf("'%s' is not a valid backing value for enum %s", $seekValue, self::class));
|
||||
}
|
||||
|
||||
public static function isValidName(string $name): bool
|
||||
{
|
||||
try {
|
||||
self::fromName($name);
|
||||
} catch (\ValueError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function isValidValue(string $name): bool
|
||||
{
|
||||
try {
|
||||
self::fromValue($name);
|
||||
} catch (\ValueError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function values(): array
|
||||
{
|
||||
return self::cases();
|
||||
}
|
||||
|
||||
public static function random(): self
|
||||
{
|
||||
$values = self::values();
|
||||
|
||||
return $values[array_rand($values)];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue