FileAccess Orion

TypedMap extends AbstractTypedMap
in package
Uses TypeTrait

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);
    }
}

Table of Contents

$data  : array<string|int, mixed>
The items of this array.
$keyType  : string
The data type of keys stored in this collection.
$valueType  : string
The data type of values stored in this collection.
__construct()  : mixed
Constructs a map object of the specified key and value types, optionally with the specified data.
clear()  : void
Removes all items from this array.
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.
count()  : int
Returns the number of items in this array.
get()  : mixed|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.
getIterator()  : ArrayIterator<mixed, mixed>
Returns an iterator for this array.
getKeyType()  : string
Return the type used on the key.
getValueType()  : string
Return the type forced on the values.
isEmpty()  : bool
Returns `true` if this array is empty.
keys()  : array<string|int, mixed>
Return an array of the keys contained in this map.
offsetExists()  : bool
Returns `true` if the given offset exists in this array.
offsetGet()  : mixed|null
Returns the value at the specified offset.
offsetSet()  : void
Sets the given value to the given offset in the array.
offsetUnset()  : void
Removes the given offset and its value from the array.
put()  : mixed|null
Associates the specified value with the specified key in this map.
putIfAbsent()  : mixed|null
Associates the specified value with the specified key in this map only if it is not already set.
remove()  : mixed|null
Removes the mapping for a key from this map if it is present.
removeIf()  : bool
Removes the entry for the specified key only if it is currently mapped to the specified value.
replace()  : mixed|null
Replaces the entry for the specified key only if it is currently mapped to some value.
replaceIf()  : bool
Replaces the entry for the specified key only if currently mapped to the specified value.
serialize()  : string
Returns a serialized string representation of this array object.
toArray()  : array<string|int, mixed>
Returns a native PHP array representation of this array object.
unserialize()  : void
Converts a serialized string representation into an instance object.
checkType()  : bool
Returns `true` if value is of the specified type.
toolValueToString()  : string
Returns a string representation of the value.

Properties

$data

The items of this array.

protected array<string|int, mixed> $data = []

$keyType

The data type of keys stored in this collection.

private string $keyType

A map key's type is immutable once it is set. For this reason, this property is set private.

data type of the map key.

$valueType

The data type of values stored in this collection.

private string $valueType

A map values's type is immutable once it is set. For this reason, this property is set private.

data type of the map value.

Methods

__construct()

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

public __construct(string $keyType, string $valueType[, array<string|int, mixed> $data = [] ]) : mixed
Parameters
$keyType : string

The data type of the map's keys.

$valueType : string

The data type of the map's values.

$data : array<string|int, mixed> = []

The initial data to set for this map.

Return values
mixed

clear()

Removes all items from this array.

public clear() : void
Return values
void

containsKey()

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

public containsKey(mixed $key) : bool
Parameters
$key : mixed

The key to check in the map.

Return values
bool

containsValue()

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

public containsValue(mixed $value) : bool

This performs a strict type check on the value.

Parameters
$value : mixed

The value to check in the map.

Return values
bool

get()

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.

public get(mixed $key[, mixed $defaultValue = null ]) : mixed|null
Parameters
$key : mixed

The key to return from the map.

$defaultValue : mixed = null

The default value to use if $key is not found.

Return values
mixed|null

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

getKeyType()

Return the type used on the key.

public getKeyType() : string
Return values
string

getValueType()

Return the type forced on the values.

public getValueType() : string
Return values
string

isEmpty()

Returns `true` if this array is empty.

public isEmpty() : bool
Return values
bool

keys()

Return an array of the keys contained in this map.

public keys() : array<string|int, mixed>
Return values
array<string|int, mixed>

offsetExists()

Returns `true` if the given offset exists in this array.

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed

The offset to check.

Tags
link

ArrayAccess::offsetExists()

Return values
bool

offsetGet()

Returns the value at the specified offset.

public offsetGet(mixed $offset) : mixed|null
Parameters
$offset : mixed

The offset for which a value should be returned.

Tags
link

ArrayAccess::offsetGet()

Return values
mixed|null

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

offsetSet()

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

public offsetSet(mixed|null $offset, mixed $value) : void
Parameters
$offset : mixed|null

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

$value : mixed

The value to set at the given offset.

Tags
link

ArrayAccess::offsetSet()

Return values
void

offsetUnset()

Removes the given offset and its value from the array.

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed

The offset to remove from the array.

Tags
link

ArrayAccess::offsetUnset()

Return values
void

put()

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

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

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

Parameters
$key : mixed

The key to put or replace in the map.

$value : mixed

The value to store at $key.

Return values
mixed|null

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

putIfAbsent()

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

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

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

Parameters
$key : mixed

The key to put in the map.

$value : mixed

The value to store at $key.

Return values
mixed|null

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

remove()

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

public remove(mixed $key) : mixed|null
Parameters
$key : mixed

The key to remove from the map.

Return values
mixed|null

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

removeIf()

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

public removeIf(mixed $key, mixed $value) : bool

This performs a strict type check on the value.

Parameters
$key : mixed

The key to remove from the map.

$value : mixed

The value to match.

Return values
bool

true if the value was removed.

replace()

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

public replace(mixed $key, mixed $value) : mixed|null
Parameters
$key : mixed

The key to replace.

$value : mixed

The value to set at $key.

Return values
mixed|null

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

replaceIf()

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

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

This performs a strict type check on the value.

Parameters
$key : mixed

The key to remove from the map.

$oldValue : mixed

The value to match.

$newValue : mixed

The value to use as a replacement.

Return values
bool

true if the value was replaced.

toArray()

Returns a native PHP array representation of this array object.

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

unserialize()

Converts a serialized string representation into an instance object.

public unserialize(string $serialized) : void
Parameters
$serialized : string

A PHP serialized string to unserialize.

Tags
link

Serializable::unserialize()

phpcsSuppress

SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint

Return values
void

checkType()

Returns `true` if value is of the specified type.

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

The type to check the value against.

$value : mixed

The value to check.

Return values
bool

toolValueToString()

Returns a string representation of the value.

protected toolValueToString(mixed $value) : string
  • 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
$value : mixed

the value to return as a string.

Return values
string

Search results