Download the PHP package greg0ire/enum without Composer
On this page you can find all versions of the php package greg0ire/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download greg0ire/enum
More information about greg0ire/enum
Files in greg0ire/enum
Informations about the package enum
Enums
This package holds a simple class that may be used as an ancestor for your enum classes.
Installation
composer require greg0ire/enum
Usage
Basic usage
Extend the Greg0ire\Enum\AbstractEnum, define your enum key values as constants,
and Bob's your uncle. You can make the class abstract or final, as you see fit.
Then, you may use the DaysOfWeek class for input validation:
Both methods have an assert* counterpart that will throw a
Greg0ire\Enum\Exception\InvalidEnumValue exception:
Additionally, you may get all the constants in your class as a hash:
You may also get all the keys in your class as an array:
Or the key with the enum class prefix:
If you would like to get the keys from a value:
If you have many keys with the same value you will get an array, and a value otherwise.
Advanced usage
If you need to get the constants from a class you cannot modify, or from an
interface, or even from several classes / interfaces, you may override
AbstractEnum::getEnumTypes().
For example, if you have the following class and interface :
You can get all three constants by creating this Enum :
Alternatively, you can specify a prefix for each type to avoid getting FQCNs in the hash keys.
Get label from a service
If you want to get the constant label behind an enum value, you can instantiate
the GetLabel class and invoke it.
To enable translation, require the symfony/translation component
and pass a Symfony\Contracts\Translation\TranslationInterface instance on the
GetLabel constructor
If you're using Symfony, alias the service and simply inject it.
If translations are enabled, the TranslatorInterface will be automatically injected.
Integration with other libraries
greg0ire/enum integrates with other libraries. The list is available in the
suggest section of the Composer dependency manifest.
Symfony validator
This package provides a "ready to use" symfony validator.
You have to require the symfony/validator package to get it working.
Another example with annotations:
Note: You will have to get doctrine/annotations and doctrine/cache packages to get it working.
Symfony form
This package provides a "ready to use" symfony form type.
You have to require the symfony/form package to get it working.
Twig extension
This package comes with an EnumExtension Twig class. It contains a filter and some functions.
You have to require the twig/twig package to get it working.
Filter
The enum_label filter will try to return the constant label corresponding to the given value.
This filter relies on the Greg0ire\Enum\Bridge\Symfony\Translator\GetLabel service.
It will try to translate it if possible. To enable translation, require the symfony/translation component
and pass a Symfony\Contracts\Translation\TranslationInterface instance on the GetLabel constructor.
GetLabel instance will be injected on the EnumExtension constructor.
If translation is not available, you will have the default label with class prefixing.
Usage:
Functions
The 3 available twig functions are ports of some AbstractEnum methods that can be useful in a twig template:
enum_get_constants=>AbstractEnum::getConstantsenum_get_keys=>AbstractEnum::getKeysenum_get_class_prefixed_keys=>AbstractEnum::getClassPrefixedKeys
The arguments are exactly the same except you have to specify the targeted class first (as enum_label filter).
Here is a concrete example with enum_get_constants function:
Twig extension as a service
On Symfony projects, the extension can be autoloaded.
First, you have to require the symfony/framework-bundle and symfony/twig-bundle packages, or use Symfony fullstack.
Then, register the bundle in the kernel of your application:
That's all. You can now directly use the filter.
Contributing
see CONTRIBUTING.md
Credits
This is a shameless rip-off of this Stack Overflow answer, with one
modification: the getConstants method has been made public so that it is
available for building choice widgets, for instance. If you want to give credit
to someone for this, give it to Brian Cline