Skip to content

Commit

Permalink
Add static ::rule() method as a shortcut for the laravel enum validat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
hailwood authored Oct 4, 2023
1 parent 6148068 commit 1309a36
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/IsBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Webfox\LaravelBackedEnums;

use Illuminate\Validation\Rules\Enum as EnumValidationRule;

/**
* @implements \Webfox\LaravelBackedEnums\BackedEnum<string,string>
Expand Down Expand Up @@ -64,13 +65,21 @@ public static function labelFor(self $value): string
return app('translator')->has($lang_key) ? __($lang_key) : $value->value;
}

public static function rule(): Rules\Enum
{
static::ensureImplementsInterface();
return new EnumValidationRule(static::class);
}

public function label(): string
{
static::ensureImplementsInterface();
return static::labelFor($this);
}

public function withMeta(): array
{
static::ensureImplementsInterface();
return [];
}

Expand Down

0 comments on commit 1309a36

Please sign in to comment.