Download the PHP package spatie/enum without Composer
On this page you can find all versions of the php package spatie/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package enum
Short Description PHP Enums
License MIT
Homepage https://github.com/spatie/enum
Rated 1.59 based on 82 reviews
Informations about the package enum
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:
And this is how they are used:
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:
Usage
This is how an enum can be defined.
This is how they are used:
Autocompletion | Refactoring |
---|---|
Creating an enum from a value
When an enum value doesn't exist, you'll get a BadMethodCallException
. If you would prefer not catching an exception, you can use:
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:
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.
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
.
Enum labels
Enums can be given a label, you can do this by overriding the labels
method.
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:
Note that label
is a read-only property, it cannot be changed.
Comparing enums
Enums can be compared using the equals
method:
You can pass several enums to the equals
method, it will return true
if the current enum equals one of the given values.
Phpunit Assertions
This package provides an abstract class Spatie\Enum\Phpunit\EnumAssertions
with some basic/common assertions if you have to test enums.
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
.
Testing
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 [email protected] 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
- Brent Roose
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of enum with dependencies
ext-json Version *