spatie / enum
PHP Enums
Fund package maintenance!
spatie
spatie.be/open-source/support-us
Installs: 17 941 681
Dependents: 78
Suggesters: 1
Security: 0
Stars: 780
Watchers: 9
Forks: 67
Open Issues: 0
Requires
- php: ^8.0
- ext-json: *
Requires (Dev)
- fakerphp/faker: ^1.9.1
- larapack/dd: ^1.1
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^4.3
Suggests
- fakerphp/faker: To use the enum faker provider
- phpunit/phpunit: To use the enum assertions
- dev-main
- 3.13.0
- 3.12.0
- 3.11.1
- 3.11.0
- 3.10.0
- 3.9.0
- 3.8.0
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.4
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.0
- v2.x-dev
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- v1.x-dev
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.1
- dev-php-8.1
This package is auto-updated.
Last update: 2024-12-08 08:50:27 UTC
README
PHP Enum
This package offers strongly typed enums in PHP. In this package, enums are always objects, never constant values on their own. This allows for proper static analysis and refactoring in IDEs.
Here's how enums are created with this package:
use \Spatie\Enum\Enum; /** * @method static self draft() * @method static self published() * @method static self archived() */ class StatusEnum extends Enum { }
And this is how they are used:
public function setStatus(StatusEnum $status): void { $this->status = $status; } // ... $class->setStatus(StatusEnum::draft());
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/enum
Usage
This is how an enum can be defined.
/** * @method static self draft() * @method static self published() * @method static self archived() */ class StatusEnum extends Enum { }
This is how they are used:
public function setStatus(StatusEnum $status) { $this->status = $status; } // ... $class->setStatus(StatusEnum::draft());
Creating an enum from a value
$status = StatusEnum::from('draft');
When an enum value doesn't exist, you'll get a BadMethodCallException
. If you would prefer not catching an exception, you can use:
$status = StatusEnum::tryFrom('draft');
When an enum value doesn't exist in this case, $status
will be null
.
The only time you want to construct an enum from a value is when unserializing them from eg. a database.
If you want to get the value of an enum to store it, you can do this:
$status->value;
Note that value
is a read-only property, it cannot be changed.
Enum values
By default, the enum value is its method name. You can however override it, for example if you want to store enums as integers in a database, instead of using their method name.
/** * @method static self draft() * @method static self published() * @method static self archived() */ class StatusEnum extends Enum { protected static function values(): array { return [ 'draft' => 1, 'published' => 2, 'archived' => 3, ]; } }
An enum value doesn't have to be a string
, as you can see in the example it can also be an int
.
Note that you don't need to override all values. Rather, you only need to override the ones that you want to be different from the default.
If you have a logic that should be applied to all method names to get the value, like lowercase them, you can return a Closure
.
/** * @method static self DRAFT() * @method static self PUBLISHED() * @method static self ARCHIVED() */ class StatusEnum extends Enum { protected static function values(): Closure { return function(string $name): string|int { return mb_strtolower($name); }; } }
Enum labels
Enums can be given a label, you can do this by overriding the labels
method.
/** * @method static self draft() * @method static self published() * @method static self archived() */ class StatusEnum extends Enum { protected static function labels(): array { return [ 'draft' => 'my draft label', ]; } }
Note that you don't need to override all labels, the default label will be the enum's value.
If you have a logic that should be applied to all method names to get the label, like lowercase them, you can return a Closure
as in the value example.
You can access an enum's label like so:
$status->label;
Note that label
is a read-only property, it cannot be changed.
Comparing enums
Enums can be compared using the equals
method:
$status->equals(StatusEnum::draft());
You can pass several enums to the equals
method, it will return true
if the current enum equals one of the given values.
$status->equals(StatusEnum::draft(), StatusEnum::archived());
Phpunit Assertions
This package provides an abstract class Spatie\Enum\Phpunit\EnumAssertions
with some basic/common assertions if you have to test enums.
use Spatie\Enum\Phpunit\EnumAssertions; EnumAssertions::assertIsEnum($post->status); // checks if actual extends Enum::class EnumAssertions::assertIsEnumValue(StatusEnum::class, 'draft'); // checks if actual is a value of given enum EnumAssertions::assertIsEnumLabel(StatusEnum::class, 'draft'); // checks if actual is a label of given enum EnumAssertions::assertEqualsEnum(StatusEnum::draft(), 'draft'); // checks if actual (transformed to enum) equals expected EnumAssertions::assertSameEnum(StatusEnum::draft(), $post->status); // checks if actual is same as expected EnumAssertions::assertSameEnumValue(StatusEnum::draft(), 1); // checks if actual is same value as expected EnumAssertions::assertSameEnumLabel(StatusEnum::draft(), 'draft'); // checks if actual is same label as expected
Faker Provider
Possibly you are using faker and want to generate random enums.
Because doing so with default faker is a lot of copy'n'paste we've got you covered with a faker provider Spatie\Enum\Faker\FakerEnumProvider
.
use Spatie\Enum\Faker\FakerEnumProvider; use Faker\Generator as Faker; /** @var Faker|FakerEnumProvider $faker */ $faker = new Faker(); $faker->addProvider(new FakerEnumProvider($faker)); $enum = $faker->randomEnum(StatusEnum::class); $value = $faker->randomEnumValue(StatusEnum::class); $label = $faker->randomEnumLabel(StatusEnum::class);
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
License
The MIT License (MIT). Please see License File for more information.