EnumMap
in package
implements
Serializable, IteratorAggregate
A specialized map implementation for use with enum type keys.
All of the keys in an enum map must come from a single enum type that is specified, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.
Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collection views self::getIterator() and self::values().
Iterators returned by the collection views are not consistent: They may or may not show the effects of modifications to the map that occur while the iteration is in progress.
Interfaces, Classes, Traits and Enums
- Serializable
- IteratorAggregate
Table of Contents
- $allowNullValues : bool
- $keyType : string
- The class name of the key.
- $keyUniverse : array<int, AbstractEnum>
- All of the constants comprising the enum, cached for performance.
- $size : int
- $values : array<int, mixed>
- Array representation of this map. The ith element is the value to which universe[i] is currently mapped, or null if it isn't mapped to anything, or NullValue if it's mapped to null.
- $valueType : string
- The type of the value.
- __construct() : mixed
- Creates a new enum map.
- clear() : void
- Removes all mappings from this map.
- containsKey() : bool
- Returns true if this map contains a mapping for the specified key.
- containsValue() : bool
- Returns true if this map maps one or more keys to the specified value.
- equals() : bool
- Compares the specified map with this map for quality.
- expect() : void
- Checks whether the map types match the supplied ones.
- get() : mixed
- Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
- getIterator() : Traversable
- put() : mixed
- Associates the specified value with the specified key in this map.
- remove() : mixed
- Removes the mapping for this key frm this map if present.
- serialize() : string
- size() : int
- Returns the number of key-value mappings in this map.
- unserialize() : void
- values() : array<string|int, mixed>
- Returns the values contained in this map.
- checkKeyType() : void
- isValidValue() : bool
- maskNull() : mixed
- unmaskNull() : mixed
Properties
$allowNullValues
private
bool
$allowNullValues
$keyType
The class name of the key.
private
string
$keyType
$keyUniverse
All of the constants comprising the enum, cached for performance.
private
array<int, AbstractEnum>
$keyUniverse
$size
private
int
$size
= 0
$values
Array representation of this map. The ith element is the value to which universe[i] is currently mapped, or null if it isn't mapped to anything, or NullValue if it's mapped to null.
private
array<int, mixed>
$values
$valueType
The type of the value.
private
string
$valueType
Methods
__construct()
Creates a new enum map.
public
__construct(string $keyType, string $valueType, bool $allowNullValues) : mixed
Parameters
- $keyType : string
-
the type of the keys, must extend AbstractEnum
- $valueType : string
-
the type of the values
- $allowNullValues : bool
-
whether to allow null values
Tags
Return values
mixed —clear()
Removes all mappings from this map.
public
clear() : void
Return values
void —containsKey()
Returns true if this map contains a mapping for the specified key.
public
containsKey(AbstractEnum $key) : bool
Parameters
- $key : AbstractEnum
Return values
bool —containsValue()
Returns true if this map maps one or more keys to the specified value.
public
containsValue(mixed $value) : bool
Parameters
- $value : mixed
Return values
bool —equals()
Compares the specified map with this map for quality.
public
equals(self $other) : bool
Returns true if the two maps represent the same mappings.
Parameters
- $other : self
Return values
bool —expect()
Checks whether the map types match the supplied ones.
public
expect(string $keyType, string $valueType, bool $allowNullValues) : void
You should call this method when an EnumMap is passed to you and you want to ensure that it's made up of the correct types.
Parameters
- $keyType : string
- $valueType : string
- $allowNullValues : bool
Tags
Return values
void —get()
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
public
get(AbstractEnum $key) : mixed
More formally, if this map contains a mapping from a key to a value, then this method returns the value; otherwise it returns null (there can be at most one such mapping).
A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that hte map explicitly maps the key to null. The self::containsKey() operation may be used to distinguish these two cases.
Parameters
- $key : AbstractEnum
Return values
mixed —getIterator()
public
getIterator() : Traversable
Return values
Traversable —put()
Associates the specified value with the specified key in this map.
public
put(AbstractEnum $key, mixed $value) : mixed
If the map previously contained a mapping for this key, the old value is replaced.
Parameters
- $key : AbstractEnum
- $value : mixed
Tags
Return values
mixed —the previous value associated with the specified key, or null if there was no mapping for the key. (a null return can also indicate that the map previously associated null with the specified key.)
remove()
Removes the mapping for this key frm this map if present.
public
remove(AbstractEnum $key) : mixed
Parameters
- $key : AbstractEnum
Return values
mixed —the previous value associated with the specified key, or null if there was no mapping for the key. (a null return can also indicate that the map previously associated null with the specified key.)
serialize()
public
serialize() : string
Return values
string —size()
Returns the number of key-value mappings in this map.
public
size() : int
Return values
int —unserialize()
public
unserialize(mixed $serialized) : void
Parameters
- $serialized : mixed
Return values
void —values()
Returns the values contained in this map.
public
values() : array<string|int, mixed>
The array will contain the values in the order their corresponding keys appear in the map, which is their natural order (the order in which the num constants are declared).
Return values
array<string|int, mixed> —checkKeyType()
private
checkKeyType(AbstractEnum $key) : void
Parameters
- $key : AbstractEnum
Tags
Return values
void —isValidValue()
private
isValidValue(mixed $value) : bool
Parameters
- $value : mixed
Return values
bool —maskNull()
private
maskNull(mixed $value) : mixed
Parameters
- $value : mixed
Return values
mixed —unmaskNull()
private
unmaskNull(mixed $value) : mixed
Parameters
- $value : mixed