Oihana PHP System

AlterMapPropertyTrait

Table of Contents

Methods

alterMapProperty()  : mixed
Provides a mechanism to alter a property of a document using a custom "map" callback.

Methods

alterMapProperty()

Provides a mechanism to alter a property of a document using a custom "map" callback.

public alterMapProperty(array<string|int, mixed>|object &$document, Container|null $container, string $key, mixed $value[, array<string|int, mixed> $params = [] ][, bool &$modified = false ]) : mixed

This allows applying arbitrary transformations to a specific property of an array or object, with access to the full document and optionally a DI container.

The first element of $params must be a callable (or string resolvable to a callable via resolveCallable), which will be invoked with the following signature:

function map(array|object $document, ?Container $container, string $key, mixed $value, array $params = []): mixed

The callable should return the new value for the property. Any modification will set $modified to true.

Parameters
$document : array<string|int, mixed>|object

The document (array or object) containing the property.

$container : Container|null

Optional DI container, for resolving services if needed.

$key : string

The key or property name being altered.

$value : mixed

The current value of the property.

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

Additional parameters, where the first element must be the callable.

$modified : bool = false

Output flag indicating whether the value was modified by the callable.

Tags
throws
InvalidArgumentException

If the callable cannot be resolved.

example
$document = ['price' => 10 , 'vat' => '0.2' ];
$callback = fn( array &$document , $container , $key, $value, $params) => $value + ( $value * ( $document['vat'] ?? 0 ) ) ;

$newValue = $this->alterMapProperty
(
     $document ,
     null ,
     'price',
     $document['price'],
     $callback ,
     $modified
);
// $newValue = 12
// $modified = true
Return values
mixed

The altered value for the property.


        
On this page

Search results