Throw exception when variant not found

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

View File

@ -2,6 +2,9 @@
namespace App\View\Enums; namespace App\View\Enums;
use Illuminate\Support\Collection;
use InvalidArgumentException;
enum Variant: string enum Variant: string
{ {
@ -63,6 +66,12 @@ enum Variant: string
public static function fromString(string $input): self public static function fromString(string $input): self
{ {
return collect(static::cases()) 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);
} }
} }