Enums for PHP

EnumTrait

Provides static methods for enumerations.

Tags
template

T

Table of Contents

assert()  : T
Returns the specified value if it exists in this enumeration, otherwise throws an exception.
coerce()  : T
Returns the specified value if it exists in this enumeration, otherwise returns the given default value.
getEntries()  : array<string, T>
Gets an associative array of the pairs of names and values of the constants in this enumeration.
getIndex()  : int
Gets the zero-based position of the constant in this enumeration that has the specified value.
getName()  : string
Gets the name of the constant in this enumeration that has the specified value.
getNames()  : array<string|int, string>
Gets an array of the names of the constants in this enumeration.
getValues()  : array<string|int, T>
Gets an array of the values of the constants in this enumeration.
isDefined()  : bool
Gets a value indicating whether a constant with the specified value exists in this enumeration.

Methods

assert()

Returns the specified value if it exists in this enumeration, otherwise throws an exception.

public final static assert(mixed $value) : T
Parameters
$value : mixed

The value to check.

Tags
throws
UnexpectedValueException

No such enumerated value was found.

Return values
T

The specified value if it exists in this enumeration.

coerce()

Returns the specified value if it exists in this enumeration, otherwise returns the given default value.

public final static coerce(mixed $value, T $defaultValue) : T
Parameters
$value : mixed

The value to coerce.

$defaultValue : T

The value to return if the specified constant does not exist.

Return values
T

The specified value if it exists in this enumeration, otherwise the given default value.

getEntries()

Gets an associative array of the pairs of names and values of the constants in this enumeration.

public final static getEntries() : array<string, T>
Return values
array<string, T>

The pairs of names and values of the constants in this enumeration.

getIndex()

Gets the zero-based position of the constant in this enumeration that has the specified value.

public final static getIndex(mixed $value) : int
Parameters
$value : mixed

The value of an enumerated constant.

Return values
int

The zero-based position of the constant in this enumeration that has the specified value, or -1 if no such constant is found.

getName()

Gets the name of the constant in this enumeration that has the specified value.

public final static getName(mixed $value) : string
Parameters
$value : mixed

The value of an enumerated constant.

Return values
string

The name of the constant in this enumeration that has the specified value, or an empty string if no such constant is found.

getNames()

Gets an array of the names of the constants in this enumeration.

public final static getNames() : array<string|int, string>
Return values
array<string|int, string>

The names of the constants in this enumeration.

getValues()

Gets an array of the values of the constants in this enumeration.

public final static getValues() : array<string|int, T>
Return values
array<string|int, T>

The values of the constants in this enumeration.

isDefined()

Gets a value indicating whether a constant with the specified value exists in this enumeration.

public final static isDefined(mixed $value) : bool
Parameters
$value : mixed

The value to check.

Return values
bool

true if a constant in this enumeration has the specified value, otherwise false.

Search results