Skip to content

erikaraujo/php-enhanced-enums

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Enhanced Enums

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:

EnhancedEnum

parse()

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

tryParse()

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

tryFromIgnoringCase()

// TODO

tryParseIgnoringCase()

An alias to tryFromIgnoreCase().

getNames()

This method returns an array with all the names of the enum cases. Here's an example:

Suit::getNames(); // Returns ['Hearts', 'Diamonds', 'Clubs', 'Spades']

getValues()

This method returns an array with all the values of the enum cases. Here's an example:

Suit::getValues(); // Returns ['hearts', 'diamonds', 'clubs', 'spades']

isDefined()

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

toString()

This method returns the value of the enum as string - even if it's an integer enum.

is()

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`

getBackingType()

// TODO: describe

Suits::getBackingType(); // returns the `ReflectionNamedType`. In this case, with `string` as a `name`.

getUnderlyingType()

An alias to getBackingType()

isIntEnum()

Returns if the enum's backing type is an integer.

Suit::isIntEnum(); // Returns false

isStringEnum()

Returns if the enum's backing type is a string.

Suit::isStringEnum(); // Returns true

getRandom()

Returns a random enum case.

getCaseByPosition()

tryGetCaseByPosition()

first()

last()

HasDescription

getDescription()

Returns the description of the given enum

Suit::Hearts->getDescription(); // Returns "The suit of hearts"

getDescriptions()

Returns an array of all the descriptions

Suit::getDescriptions();

tryFromDescription()

tryFromDescriptionIgnoringCase()

HasLabel

getLabel()

getLabels()

tryFromLabel()

tryFromLabelIgnoringCase()

shouldAutoGenerateLabelFromValue()

protected

IsSelectArray

asSelectArray()

About

Enhanced Enum methods for PHP Enums

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages