Download the PHP package jac/enums without Composer
On this page you can find all versions of the php package jac/enums. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package enums
Enum implementation for PHP
Why
To avoid to have to install the SplEnum
, yet another extension...
Enums have a lot of benefits (in my own opinion):
- Allow type hinting
- Don't need to search in endless array definitions
- Avoid misspelling of values
- Can be enrich with methods
In this implementation the constructor is made private to avoid duplicating instance of the enum
Installation
How to use
For more complex example look at the example directory
Create an Enum
Usage
Enum having multiple keys with the same value[Multiple]
Instantiate an Enum from its value is possible calling the Devise::EUR()
for example,
and having multiple keys with the same value won't be harmful in that case. However, it
is different when we try to instantiate an Enum from its value. Let's take the following
example:
There is multiple ways a developer could use to help the builder decide which key should be preferred.
- Use the
__DEFAULT__
constant which is a reverted key => value array. This method will first be checked before the following two - Use the
@default
tag in the php doc associated to the constant, in the above example, for the chinese devise (RMB, CNY), in case we useDevise::from('RMB')
, the key RMB is going to be choose. - Use the
@deprecated
to exclude values. When a constant is set as deprecated, then its priority is lowered and it will be returned if and only if no other options is found.
In case there is no configuration set for the default value to choose or if there is still multiple value available, a warning is triggered and one of the found options is returned.
API
Comparison of enums works with == and ===
__toString()
To display the current enum : EnumName::EnumKey::EnumValue (can be override)getValue()
Return the value of the Enum (the value of the constant associated with the current key)getKey()
Return the key of the the current Enumequals(AbstractEnum|null):bool
Compare both enums using their values without taking the key into account.
Static method
enum(string)
Create an instance of the enum giving its keyfrom(mixed)
Create an enum from its value, see [Multiple] to understand more about its behaviortoArray()
return the list of key => value of the enuminEnum()
check if the parameter is either a key or a valuesearch(mixed): array
Search for all the keys having the given valuekeyExists(string): bool
Check if the key exists in the enumvalueExists(mixed): bool
Check if at leas one of the keys have the given valuekeys(): array
: The list of available keysvalues(): array
: The list of available none unique values
Serialization
The enum implements the JsonSerializable
interface. By default it will return the value as a string
A formatter is available: Jac\Enum\EnumJsonFormat
with several option implemented.
Will output
Will output