Download the PHP package ekvedaras/php-enum without Composer
On this page you can find all versions of the php package ekvedaras/php-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ekvedaras/php-enum
More information about ekvedaras/php-enum
Files in ekvedaras/php-enum
Package php-enum
Short Description PHP enum implementation
License MIT
Homepage https://github.com/ekvedaras/php-enum
Informations about the package php-enum
PHP Enum
Big thanks happy-types/enumerable-type for the original idea. Take a look if it suits your needs better.
This package adds meta
field, provides a few more methods like options
, keys
, json
, etc.
and there are simple php array, illuminate/collection
, arrayy
and doctrine
collection implementations to choose from.
Benefits
- Enums in general allow to avoid magic values
- By type hinting forces only allowed values to be passed to methods (or returned)
- Easy way to list all possible values
- More feature rich and flexible then other enum implementations
- Works with strict (
===
) operator - IDE friendly, so auto complete, usage analysis and refactorings all work
Defining enums
Create enums by extending one of:
EKvedaras\PHPEnum\PHPArray\Enum
EKvedaras\PHPEnum\Illuminate\Collection\Enum
EKvedaras\PHPEnum\Arrayy\Enum
EKvedaras\PHPEnum\Doctrine\Enum
Integers can be used as IDs instead of string values if you prefer.
Usage
Retrieving and comparing enum values
Accessing value properties
Listing enum values
There are two implementations provided:
PHP array
To use PHP array your enums should extend EKvedaras\PHPEnum\PHPArray\Enum
class
Illuminate Collection
Either illuminate/support
or illuminate/collections
package is required which is not installed by default.
To use Illuminate Collection your enums should extend EKvedaras\PHPEnum\Illuminate\Collection\Enum
class.
The only difference is enum
, options
and keys
methods will return Collection
instances instead of arrays.
The rest works exactly the same.
Meta
Meta field is intentionally left as mixed type as it could be used for various reasons. For example linking enum options with a specific implementation:
Resolving payment handler in Laravel:
Meta could also be used as a more in detail description of each option that could be displayed to users or some other object linking other classes, resources together.
Furthermore, in some cases it is useful to resolve enum option from meta. That is also possible:
Things to know
final public static function
Only methods marked as final public static
will be considered as possible values of enum. Other methods are still there, but
they will not be returned in enum
/ keys
/ options
, etc. results and won't be considered as valid values. However, this allows
to extend enums and make them more useful. For example:
from($id)
only allows valid IDs
Well, this is expected. Calling PaymentMethod::from('ideal')
will throw OutOfBoundsException
.
No serialization
Enum object instances cannot be serialized. Deserialized objects would get a different address in memory therefore, ===
would no longer work.
Calling serialize(PaymentMethod::stripe())
will throw a RuntimeException
.
As a workaround it is better to store the ID instead of object itself. You still get the bonus of setters only accepting valid values.
Don't mix implementations
Enum instances cache is stored in a static variable. Choose one implementation for your project and stick to it, otherwise you may unexpectedly get errors because types don't match.
You may create your own project enum class and extend your chosen implementation, so if it ever needs to be changed it can be done in one place only (if storage APIs match).
Related packages
Changelog
See changes in changelog files:
- v1 changelog
All versions of php-enum with dependencies
ext-json Version *