Throw exception when variant not found

This commit is contained in:
philipp lang 2024-10-19 21:27:03 +02:00
parent bfeb5d30b6
commit a6f61104e3
1 changed files with 10 additions and 1 deletions

View File

@ -2,6 +2,9 @@
namespace App\View\Enums;
use Illuminate\Support\Collection;
use InvalidArgumentException;
enum Variant: string
{
@ -63,6 +66,12 @@ enum Variant: string
public static function fromString(string $input): self
{
return collect(static::cases())
->first(fn ($variant) => $variant->value === $input);
->first(fn ($variant) => $variant->value === $input)
?: throw new InvalidArgumentException("Unknown variant: {$input} - Available Variants: " . self::values()->implode(', '));
}
public static function values(): Collection
{
return collect(static::cases())->map(fn ($variant) => $variant->value);
}
}