Download the PHP package intrfce/enum-attribute-descriptors without Composer
On this page you can find all versions of the php package intrfce/enum-attribute-descriptors. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download intrfce/enum-attribute-descriptors
More information about intrfce/enum-attribute-descriptors
Files in intrfce/enum-attribute-descriptors
Package enum-attribute-descriptors
Short Description Use attributes to give your PHP enums titles and descriptions.
License MIT
Homepage https://github.com/intrfce/enum-attribute-descriptors
Informations about the package enum-attribute-descriptors
Attribute based Enum descriptors.
Have you ever written an enum for something and wanted to have a "nice" version of the enum name, so you write something like this:
But the problem is, for each new case, you have to add something to the match statement, or HOPE that it'll print something out that's legible using the default fallback?
With this package, you can co-locate titles, and even descriptions, with your enum cases like so:
Neat huh!
You can also attach arbitrary key-value metadata to your enum cases using the KeyValue attribute:
The KeyValue attribute is repeatable, so you can attach as many key-value pairs as you need to each case. Values can be any type (strings, integers, booleans, etc.).
Installation
You can install the package via composer:
Usage
Just add the Intrfce\EnumAttributeDescriptors\Concerns\HasAttributeDescriptors trait to your enum, and you're good to go!
Defining fallbacks.
If you have a situation where you don't want to (or can't) define a description or title for each case, you can override
the titleFallback() and descriptionFallback() methods on your enum class.
Simple!
Credits
- Dan Matthews
- All Contributors
License
The MIT License (MIT). Please see License File for more information.