Skip to content

Commit 1309a36

Browse files
authored
Add static ::rule() method as a shortcut for the laravel enum validation.
1 parent 6148068 commit 1309a36

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/IsBackedEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Webfox\LaravelBackedEnums;
44

5+
use Illuminate\Validation\Rules\Enum as EnumValidationRule;
56

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

68+
public static function rule(): Rules\Enum
69+
{
70+
static::ensureImplementsInterface();
71+
return new EnumValidationRule(static::class);
72+
}
73+
6774
public function label(): string
6875
{
76+
static::ensureImplementsInterface();
6977
return static::labelFor($this);
7078
}
7179

7280
public function withMeta(): array
7381
{
82+
static::ensureImplementsInterface();
7483
return [];
7584
}
7685

0 commit comments

Comments
 (0)