Download the PHP package cerbero/enum without Composer
On this page you can find all versions of the php package cerbero/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package enum
Short Description PHP library to extend enum functionalities.
License MIT
Homepage https://github.com/cerbero90/enum
Informations about the package enum
๐ฒ Enum
Zero-dependencies PHP library to supercharge enum functionalities. Similar libraries worth mentioning are:
๐ฆ Install
Via Composer:
๐ฎ Usage
- Classification
- Comparison
- Keys resolution
- Hydration
- Elaborating cases
- Cases collection
To supercharge our enums with all functionalities provided by this package, we can simply use the Enumerates
trait in both pure enums and backed enums:
Classification
These methods determine whether an enum is pure or backed:
Comparison
We can check whether an enum includes some names or values. Pure enums check for names, whilst backed enums check for values:
Otherwise we can let cases determine whether they match with a name or a value:
Comparisons can also be performed within arrays:
Keys resolution
With the term "key" we refer to any element defined in an enum, such as names, values or methods implemented by cases. Take the following enum for example:
The keys defined in this enum are name
, value
(as it is a backed enum), color
and isOdd
. We can retrieve any key assigned to a case by calling get()
:
At first glance this method may seem an overkill as "keys" can be accessed directly by cases like this:
However get()
is useful to resolve keys dynamically as a key may be a property, a method or a closure. It often gets called internally for more advanced functionalities that we are going to explore very soon.
Hydration
An enum case can be instantiated from its own name, value (if backed) and keys:
While pure enums try to hydrate cases from names, backed enums can hydrate from both names and values. Even keys can be used to hydrate cases, cases are then wrapped into a CasesCollection
to allow further processing.
Elaborating cases
There is a bunch of operations that can be performed on the cases of an enum. If the result of an operation is a plain list of cases, they get wrapped into a CasesCollection
for additional elaboration, otherwise the final result of the operation is returned:
Cases collection
When a plain list of cases is returned by one of the cases operations, it gets wrapped into a CasesCollection
which provides a fluent API to perform further operations on the set of cases:
Cases can be collected by calling collect()
or any other cases operation returning a CasesCollection
:
We can iterate cases collections within any loop:
Obtaining the underlying plain list of cases is easy:
Sometimes we may need to extract only the first case of the collection:
For reference, here are all the operations available in CasesCollection
:
๐ Change log
Please see CHANGELOG for more information on what has changed recently.
๐งช Testing
๐ Contributing
Please see CODE_OF_CONDUCT for details.
๐งฏ Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
๐ Credits
- Andrea Marco Sartori
- All Contributors
โ๏ธ License
The MIT License (MIT). Please see License File for more information.