1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 08:24:05 +00:00

feat: Add support for default values on metadata properties (#23)

refactor: Remove comment
This commit is contained in:
Owen Conti 2024-07-15 08:04:31 -06:00 committed by GitHub
parent a99ee1a7e0
commit 5be8a58974
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 2 deletions

View file

@ -29,6 +29,17 @@ class Instructions extends MetaProperty
}
}
#[Attribute]
class IsActive extends MetaProperty
{
public static string $method = 'isActive';
public static function defaultValue(): mixed
{
return false;
}
}
/**
* @method string description()
* @method string color()
@ -62,6 +73,25 @@ enum Role
case GUEST;
}
#[Meta([IsActive::class])]
enum ReferenceType: int
{
use InvokableCases, Options, Names, Values, From, Metadata, Comparable;
#[IsActive(true)]
case ACTIVE_TYPE = 1;
case INACTIVE_TYPE = 0;
}
#[Meta([Desc::class])]
enum RoleWithoutAttribute
{
use InvokableCases, Options, Names, Values, From, Metadata, Comparable;
case ADMIN;
}
enum MultiWordSnakeCaseEnum
{
use Options;

View file

@ -60,4 +60,12 @@ test('fromMeta throws an exception when the enum cannot be instantiated', functi
test('tryFromMeta silently fails when the enum cannot be instantiated')
->expect(Role::tryFromMeta(Color::make('foobar')))
->toBe(null);
->toBeNull();
test('metadata properties return null if missing on the case')
->expect(RoleWithoutAttribute::ADMIN->desc())
->toBeNull();
test('metadata can have default values')
->expect(ReferenceType::INACTIVE_TYPE->isActive())
->toBeFalse();