Enhanced Enum methods for PHP Enums
In this package, we have 4 traits: EnhancedEnum
, HasDescription
, HasLabel
and IsSelectArray
. Here are all the methods that each trait provides:
This method is simply an alias to the native from()
method. Here's an example:
Suit::parse('hearts'); // Returns the Suit::Hearts case
Suit::parse('invalid'); // Throws an exception
This method is an improved alias to the tryFrom()
method. The difference here, is that, instead of only having the $value
parameter, it also has a $ignoreCase
boolean parameter to allow the method to ignore the case of the value before parsing it. Here's an example:
Suit::tryParse('hearts'); // Returns the Suit::Hearts case
Suit::tryParse('invalid'); // Returns null
Suit::tryParse('hEaRtS'); // Returns null
Suit::tryParse('hEaRtS', ignoreCase: true); // Returns the Suit::Hearts case
// TODO
An alias to tryFromIgnoreCase()
.
This method returns an array with all the names of the enum cases. Here's an example:
Suit::getNames(); // Returns ['Hearts', 'Diamonds', 'Clubs', 'Spades']
This method returns an array with all the values of the enum cases. Here's an example:
Suit::getValues(); // Returns ['hearts', 'diamonds', 'clubs', 'spades']
This method checks if a given value is defined in the enum. Here's an example:
Suit::isDefined('hearts'); // Returns true
Suit::isDefined('invalid'); // Returns false
Suit::isDefined('hEaRtS'); // Returns false
Suit::isDefined('hEaRtS', ignoreCase: true); // Returns true
This method returns the value of the enum as string - even if it's an integer enum.
TODO: describe
Suit::Hearts->is(Suit::Spades); // Returns `false`
Suit::Hearts->is('spades'); // Returns `false`
Suit::Hearts->is(Suit::Hearts); // Returns `true`
Suit::Hearts->is('hearts'); // Returns `true`
Suit::Hearts->is('HeArTs'); // Returns `false`
Suit::Hearts->is('HeArTs', ignoreCase: true); // Returns `true`
// TODO: describe
Suits::getBackingType(); // returns the `ReflectionNamedType`. In this case, with `string` as a `name`.
An alias to getBackingType()
Returns if the enum's backing type is an integer.
Suit::isIntEnum(); // Returns false
Returns if the enum's backing type is a string.
Suit::isStringEnum(); // Returns true
Returns a random enum case.
Returns the description of the given enum
Suit::Hearts->getDescription(); // Returns "The suit of hearts"
Returns an array of all the descriptions
Suit::getDescriptions();
protected