Skip to content

Instantly share code, notes, and snippets.

@robclancy
Last active May 31, 2024 03:56
Show Gist options
  • Save robclancy/c4a590ac987a53a93d2f213cde9944fe to your computer and use it in GitHub Desktop.
Save robclancy/c4a590ac987a53a93d2f213cde9944fe to your computer and use it in GitHub Desktop.
enum without repeating match for every new method and scopes
<?php
namespace App\Enums;
use App\Enums\Concerns\InteractsWithFields;
use Filament\Support\Contracts\HasLabel;
enum BlockType: string implements HasLabel
{
use InteractsWithFields;
case RichText = 'rich-text';
case Quote = 'quote';
case Content = 'content';
case Banner = 'banner';
case HubspotForm = 'hubspot-form';
case Clients = 'clients';
case PagesGrid = 'pages-grid';
case DualQuotes = 'dual-quotes';
case DualCards = 'dual-cards';
case Questions = 'questions';
case Showcase = 'showcase';
case Callout = 'callout';
public function describe(): object
{
$description = match ($this) {
self::RichText => [
'label' => 'Rich Text',
],
self::Content => [
'label' => 'Content',
],
self::Banner => [
'label' => 'Banner',
],
self::HubspotForm => [
'label' => 'HubSpot Form',
],
self::Clients => [
'label' => 'Clients',
],
self::PagesGrid => [
'label' => 'Pages Grid',
],
self::Quote => [
'label' => 'Quote',
'scopes' => ['testimonials', 'success-stories'],
],
self::DualQuotes => [
'label' => 'Dual Quotes',
],
self::DualCards => [
'label' => 'Dual Cards',
],
self::Questions => [
'label' => 'FAQs',
],
self::Showcase => [
'label' => 'Showcase',
],
self::Callout => [
'label' => 'Callout',
],
};
if (empty($description['scopes'])) {
$description['scopes'] = ['*'];
}
return (object) array_merge($description, ['value' => $this->value]);
}
public function getLabel(): ?string
{
return $this->describe()->label;
}
public static function options(string $scope = '*'): array
{
return collect(self::cases())
->map(fn ($type) => $type->describe())
->filter(fn (object $type) => array_intersect(['*', $scope], $type->scopes))
->mapWithKeys(fn ($type) => [$type->value => $type->label])
->toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment