diff --git a/src/Meta/MetaProperty.php b/src/Meta/MetaProperty.php index e4fb44e..851665e 100644 --- a/src/Meta/MetaProperty.php +++ b/src/Meta/MetaProperty.php @@ -12,6 +12,11 @@ abstract class MetaProperty $this->value = $this->transform($value); } + public static function defaultValue(): mixed + { + return null; + } + public static function make(mixed $value): static { return new static($value); diff --git a/src/Meta/Reflection.php b/src/Meta/Reflection.php index 9c9b9ca..929ac61 100644 --- a/src/Meta/Reflection.php +++ b/src/Meta/Reflection.php @@ -73,6 +73,6 @@ class Reflection return $properties[0]->value; } - return null; + return $metaProperty::defaultValue() ?? null; } } diff --git a/tests/Pest.php b/tests/Pest.php index 4ca7350..576aebe 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -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; diff --git a/tests/Pest/MetadataTest.php b/tests/Pest/MetadataTest.php index d15bbb6..a5af477 100644 --- a/tests/Pest/MetadataTest.php +++ b/tests/Pest/MetadataTest.php @@ -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();