\DreamscapesEnumeration

The Enumeration class

All enumerations should extend this class. Enumerated members should be defined as class constants.

Summary

Methods
Properties
Constants
getName()
withValue()
getValue()
named()
isDefined()
contains()
has()
defines()
allMembers()
toArray()
getType()
value()
__toString()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

getName()

getName(string  $value) : string

Get the name of the member that holds given value

This method is type-sensitive - see the example below.

Use case

You have a value that is defined in an enumeration and you would like to know the name of the enumeration member that holds this value.

Example

class Animal extends Dreamscapes\Enumeration
{
  const Horse = 0;
  const Dog = 1;
}

echo Animal::getName(0); // Prints 'Horse'
echo Animal::getName('0'); // Returns null, method is type-sensitive

Parameters

string $value

The member's expected value (type-sensitive)

Returns

string —

The name of the member that holds this value or null if not defined

withValue()

withValue(string  $value) : string

Semantic alias for Enumeration::getName()

Parameters

string $value

The member's expected value (type-sensitive)

Returns

string —

The name of the member that holds this value or null if not defined

getValue()

getValue(string|\Dreamscapes\Enumeration  $member) : mixed

Get the value of a given member's name

Use case

You have a string representation of the Enumeration member and you would like to know the value that member holds.

Example

class Animal extends Dreamscapes\Enumeration
{
  const Horse = 0;
  const Dog = 1;
}

echo Animal::getValue('Dog'); // Prints an integer, 1

Parameters

string|\Dreamscapes\Enumeration $member

The member's expected name

Returns

mixed —

The value of the member

named()

named(string|\Dreamscapes\Enumeration  $member) : mixed

Semantic alias for Enumeration::getValue()

Parameters

string|\Dreamscapes\Enumeration $member

The member's expected name

Returns

mixed —

The value of the member

isDefined()

isDefined(string  $member) : boolean

Does a member with this name exist in the enumeration?

Example

class Animal extends Dreamscapes\Enumeration
{
  const Horse = 0;
  const Dog = 1;
}

echo Animal::isDefined('Dog'); // Prints an integer, 1 (bool true)
echo Animal::isDefined('Cat'); // Prints nothing (bool false)

Parameters

string $member

The member's expected name

Returns

boolean —

true if such member is defined, false otherwise

contains()

contains(string  $member) : boolean

Semantic alias for Enumeration::isDefined()

Parameters

string $member

The member's expected name

Returns

boolean —

true if such member is defined, false otherwise

has()

has(string  $member) : boolean

Semantic alias for Enumeration::isDefined()

Parameters

string $member

The member's expected name

Returns

boolean —

true if such member is defined, false otherwise

defines()

defines(string  $member) : boolean

Semantic alias for Enumeration::isDefined()

Parameters

string $member

The member's expected name

Returns

boolean —

true if such member is defined, false otherwise

allMembers()

allMembers() : array

Get all members defined in this Enumeration

The returned array's order is determined by the order in which the constants are defined in the class.

Example

class Animal extends Dreamscapes\Enumeration
{
  const Horse = 0;
  const Dog = 1;
}

print_r(Animal::allMembers());
// Array
// (
//   0 => 'Horse'
//   1 => 'Dog'
// )

Returns

array —

An ordered list of all Enumeration members

toArray()

toArray() : array

Convert the Enumeration into an array

Example

class Animal extends Dreamscapes\Enumeration
{
  const Horse = 0;
  const Dog = 1;
}

print_r(Animal::toArray());
// Array
// (
//   'Horse' => 0
//   'Dog' => 1
// )

Returns

array

getType()

getType() : string

Get the string representation of the Enumeration, without namespace

Example

namespace Fauna;

class Animal extends \Dreamscapes\Enumeration {}

echo Animal::getType(); // Animal
echo \Dreamscapes\Enumeration::getType(); // Enumeration

Returns

string —

The name of the Enumeration class, without namespace

value()

value() : mixed

Get the value of the enumerated member represented by this instance

Returns

mixed —

Value of the enumerated member

__toString()

__toString() : string

Allow enumeration members to be typecast to strings

Returns

string —

The value of the enumeration member in a string representation