Throw exception when variant not found
This commit is contained in:
parent
a8dbfcbb9d
commit
08821b3731
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue