From a6f61104e3abeac2531b64412d29da8208672a42 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 19 Oct 2024 21:27:03 +0200 Subject: [PATCH] Throw exception when variant not found --- app/View/Enums/Variant.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/View/Enums/Variant.php b/app/View/Enums/Variant.php index 121d284f..91ae3b66 100644 --- a/app/View/Enums/Variant.php +++ b/app/View/Enums/Variant.php @@ -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); } }