# DataStructure

The Chevere\Components\DataStructure namespace provides components for providing typed immutable-like data structures, is built on top of php-ds (opens new window).


Data structures are provisioned as stand-alone classes and/or traits, which adds data structure functionality directly.

# Map

The Map component is in charge of extending DS\Map (opens new window) functionality to provide the array replacement for Chevere.

# Creating Map

Create a Map by passing named arguments of any type.

use Chevere\Components\DataStructure\Map;

$map = new Map(foo: $foo, bar: $bar);

# Putting value

The withPut method is used to put a value to the Map at the given key.

$map = $map->withPut(foo: $foo, bar: $bar);

# Checking keys

The has method is used to check if the Map contains the given key(s).

$map->has(key: 'foo'); // true
$map->has(key: 'notFound'); // throws Throwable

# Asserting keys

The assertHas method is used to assert if the Map contains the given key(s).

$map->assertHas(key: 'foo'); // true
$map->assertHas(key: 'notFound'); // throws Throwable

# Get value

The get method is used to retrieve the Map value for the given key.

$getFoo = $map->get(key: 'foo'); // $foo
$getBar = $map->get(key: 'bar'); // $bar

# Typed Map

The MappedInterface describes the interface for a component supporting mapping, like Map which doesn't specify any typing.


⏩ Use MapTrait quickly implement the MappedInterface.

With this, expose your own setters and getters accordingly to your typing and internally simply rely in the functionality provided by the MapTrait.