class TypedMap extends AbstractTypedMap (View source)

A TypedMap represents a map of elements where key and value are typed.

Each element is identified by a key with defined type and a value of defined type. The keys of the map must be unique. The values on the map can be= repeated but each with its own different key.

The most common case is to use a string type key, but it's not limited to this type of keys.

This is a direct implementation of TypedMapInterface, provided for the sake of convenience.

Example usage:

$map = new TypedMap('string', Foo::class);
$map['x'] = new Foo();
foreach ($map as $key => $value) {
    // do something with $key, it will be a Foo::class
}

// this will throw an exception since key must be string
$map[10] = new Foo();

// this will throw an exception since value must be a Foo
$map['bar'] = 'bar';

// initialize map with contents
$map = new TypedMap('string', Foo::class, [
    new Foo(), new Foo(), new Foo()
]);

It is preferable to subclass AbstractTypedMap to create your own typed map implementation:

class FooTypedMap extends AbstractTypedMap
{
    public function getKeyType()
    {
        return 'int';
    }

    public function getValueType()
    {
         return Foo::class;
    }
}

… but you also may use the TypedMap class:

class FooTypedMap extends TypedMap
{
    public function __constructor(array $data = [])
    {
        parent::__construct('int', Foo::class, $data);
    }
}

Traits

Provides functionality to check values for specific types.
Provides functionality to check values for specific types.
Provides functionality to express a value as string

Properties

protected array $data The items of this array. from AbstractArray

Methods

__construct(string $keyType, string $valueType, array $data = [])

Constructs a map object of the specified key and value types, optionally with the specified data.

getIterator()

Returns an iterator for this array.

bool
offsetExists(mixed $offset)

Returns true if the given offset exists in this array.

mixed|null
offsetGet(mixed $offset)

Returns the value at the specified offset.

void
offsetSet(mixed|null $offset, mixed $value)

Sets the given value to the given offset in the map.

void
offsetUnset(mixed $offset)

Removes the given offset and its value from the array.

string
serialize()

Returns a serialized string representation of this array object.

void
unserialize(string $serialized)

Converts a serialized string representation into an instance object.

int
count()

Returns the number of items in this array.

void
clear()

Removes all items from this array.

array
toArray()

Returns a native PHP array representation of this array object.

bool
isEmpty()

Returns true if this array is empty.

bool
containsKey(mixed $key)

Returns true if this map contains a mapping for the specified key.

bool
containsValue(mixed $value)

Returns true if this map maps one or more keys to the specified value.

array
keys()

Return an array of the keys contained in this map.

mixed|null
get(mixed $key, mixed $defaultValue = null)

Returns the value to which the specified key is mapped, null if this map contains no mapping for the key, or (optionally) $defaultValue if this map contains no mapping for the key.

mixed|null
put(mixed $key, mixed $value)

Associates the specified value with the specified key in this map.

mixed|null
putIfAbsent(mixed $key, mixed $value)

Associates the specified value with the specified key in this map only if it is not already set.

mixed|null
remove(mixed $key)

Removes the mapping for a key from this map if it is present.

bool
removeIf(mixed $key, mixed $value)

Removes the entry for the specified key only if it is currently mapped to the specified value.

mixed|null
replace(mixed $key, mixed $value)

Replaces the entry for the specified key only if it is currently mapped to some value.

bool
replaceIf(mixed $key, mixed $oldValue, mixed $newValue)

Replaces the entry for the specified key only if currently mapped to the specified value.

bool
checkType(string $type, mixed $value)

Returns true if value is of the specified type.

from TypeTrait
string
toolValueToString(mixed $value)

Returns a string representation of the value.

string
getKeyType()

Return the type used on the key.

string
getValueType()

Return the type forced on the values.

Details

__construct(string $keyType, string $valueType, array $data = [])

Constructs a map object of the specified key and value types, optionally with the specified data.

Parameters

string $keyType The data type of the map's keys.
string $valueType The data type of the map's values.
array $data The initial items to add to this array.

Traversable getIterator()

Returns an iterator for this array.

Return Value

Traversable

bool offsetExists(mixed $offset)

Returns true if the given offset exists in this array.

Parameters

mixed $offset The offset to check.

Return Value

bool

mixed|null offsetGet(mixed $offset)

Returns the value at the specified offset.

Parameters

mixed $offset The offset for which a value should be returned.

Return Value

mixed|null the value stored at the offset, or null if the offset does not exist.

void offsetSet(mixed|null $offset, mixed $value)

Sets the given value to the given offset in the map.

Parameters

mixed|null $offset The offset to set. If null, the value may be set at a numerically-indexed offset.
mixed $value The value to set at the given offset.

Return Value

void

Exceptions

InvalidArgumentException if the offset or value do not match the expected types.

void offsetUnset(mixed $offset)

Removes the given offset and its value from the array.

Parameters

mixed $offset The offset to remove from the array.

Return Value

void

string serialize()

Returns a serialized string representation of this array object.

Return Value

string a PHP serialized string.

void unserialize(string $serialized)

Converts a serialized string representation into an instance object.

Parameters

string $serialized A PHP serialized string to unserialize.

Return Value

void

int count()

Returns the number of items in this array.

Return Value

int

void clear()

Removes all items from this array.

Return Value

void

array toArray()

Returns a native PHP array representation of this array object.

Return Value

array

bool isEmpty()

Returns true if this array is empty.

Return Value

bool

bool containsKey(mixed $key)

Returns true if this map contains a mapping for the specified key.

Parameters

mixed $key The key to check in the map.

Return Value

bool

bool containsValue(mixed $value)

Returns true if this map maps one or more keys to the specified value.

This performs a strict type check on the value.

Parameters

mixed $value The value to check in the map.

Return Value

bool

array keys()

Return an array of the keys contained in this map.

Return Value

array

mixed|null get(mixed $key, mixed $defaultValue = null)

Returns the value to which the specified key is mapped, null if this map contains no mapping for the key, or (optionally) $defaultValue if this map contains no mapping for the key.

Parameters

mixed $key The key to return from the map.
mixed $defaultValue The default value to use if $key is not found.

Return Value

mixed|null the value or null if the key could not be found.

mixed|null put(mixed $key, mixed $value)

Associates the specified value with the specified key in this map.

If the map previously contained a mapping for the key, the old value is replaced by the specified value.

Parameters

mixed $key The key to put or replace in the map.
mixed $value The value to store at $key.

Return Value

mixed|null the previous value associated with key, or null if there was no mapping for $key.

mixed|null putIfAbsent(mixed $key, mixed $value)

Associates the specified value with the specified key in this map only if it is not already set.

If there is already a value associated with $key, this returns that value without replacing it.

Parameters

mixed $key The key to put in the map.
mixed $value The value to store at $key.

Return Value

mixed|null the previous value associated with key, or null if there was no mapping for $key.

mixed|null remove(mixed $key)

Removes the mapping for a key from this map if it is present.

Parameters

mixed $key The key to remove from the map.

Return Value

mixed|null the previous value associated with key, or null if there was no mapping for $key.

bool removeIf(mixed $key, mixed $value)

Removes the entry for the specified key only if it is currently mapped to the specified value.

This performs a strict type check on the value.

Parameters

mixed $key The key to remove from the map.
mixed $value The value to match.

Return Value

bool true if the value was removed.

mixed|null replace(mixed $key, mixed $value)

Replaces the entry for the specified key only if it is currently mapped to some value.

Parameters

mixed $key The key to replace.
mixed $value The value to set at $key.

Return Value

mixed|null the previous value associated with key, or null if there was no mapping for $key.

bool replaceIf(mixed $key, mixed $oldValue, mixed $newValue)

Replaces the entry for the specified key only if currently mapped to the specified value.

This performs a strict type check on the value.

Parameters

mixed $key The key to remove from the map.
mixed $oldValue The value to match.
mixed $newValue The value to use as a replacement.

Return Value

bool true if the value was replaced.

protected bool checkType(string $type, mixed $value)

Returns true if value is of the specified type.

Parameters

string $type The type to check the value against.
mixed $value The value to check.

Return Value

bool

protected string toolValueToString(mixed $value)

Returns a string representation of the value.

  • null value: 'NULL'
  • boolean: 'TRUE', 'FALSE'
  • array: 'Array'
  • scalar: converted-value
  • resource: '(type resource #number)'
  • object with __toString(): result of __toString()
  • object DateTime: ISO 8601 date
  • object: '(className Object)'
  • anonymous function: same as object

Parameters

mixed $value the value to return as a string.

Return Value

string

string getKeyType()

Return the type used on the key.

Return Value

string

string getValueType()

Return the type forced on the values.

Return Value

string