Skip to content

Commit

Permalink
Add methods to check backing type
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Oct 28, 2024
1 parent 2912629 commit c5d2492
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Concerns/SelfAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public static function isBacked(): bool
return is_subclass_of(self::class, BackedEnum::class);
}

/**
* Determine whether the enum is backed by integer.
*/
public static function isBackedByInteger(): bool
{
return (new ReflectionEnum(self::class))->getBackingType()?->getName() === 'int';
}

/**
* Determine whether the enum is backed by string.
*/
public static function isBackedByString(): bool
{
return (new ReflectionEnum(self::class))->getBackingType()?->getName() === 'string';
}

/**
* Retrieve all the meta names of the enum.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/BackedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Cerbero\Enum\CasesCollection;
use Cerbero\Enum\BackedEnum;
use Cerbero\Enum\Enums;
use Cerbero\Enum\PureEnum;
use Pest\Expectation;

it('determines whether the enum is pure')
Expand All @@ -13,6 +14,13 @@
->expect(BackedEnum::isBacked())
->toBeTrue();

it('determines whether the enum is backed by integer or string', function() {
expect(BackedEnum::isBackedByInteger())->toBeTrue();
expect(BackedEnum::isBackedByString())->toBeFalse();
expect(PureEnum::isBackedByInteger())->toBeFalse();
expect(PureEnum::isBackedByString())->toBeFalse();
});

it('retrieves all the names of the cases')
->expect(BackedEnum::names())
->toBe(['one', 'two', 'three']);
Expand Down

0 comments on commit c5d2492

Please sign in to comment.