Oihana PHP

AlterGetDocumentPropertyTrait

Provides logic to retrieve a document using a Documents model based on a given value and definition.

This trait depends on the DocumentsTrait to access the document model.

The main method alterGetDocument() is typically used as part of a data transformation or hydration process where a scalar or identifier is replaced by a fully loaded document instance.

Usage example:

class MyMapper {
    use AlterGetDocumentPropertyTrait;
}

$mapper = new MyMapper();
$doc = $mapper->alterGetDocument(42, ['UserModel', 'id'], $modified);

if ($modified) {
    echo "Document was loaded successfully.";
}
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

alterGetDocument()  : mixed
Replaces an identifier value by the document it references, loaded through a Documents model.

Methods

alterGetDocument()

Replaces an identifier value by the document it references, loaded through a Documents model.

public alterGetDocument(mixed $value[, array<string|int, mixed> $definition = [] ][, Container|null $container = null ][, bool &$modified = false ]) : mixed

The model class name is taken from $definition[0] and resolved via getDocumentsModel() (optionally using the DI container). The model's get() method is then called with the lookup key $definition[1] and the current $value. A null value short-circuits and is returned untouched, and any failure raised while fetching the document is swallowed and turned into a null result.

Parameters
$value : mixed

The identifier to resolve. A null value is returned as-is.

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

The lookup definition: [ 0 => model class/identifier , 1 => lookup key field ]. Both default to null.

$container : Container|null = null

Optional DI container used by getDocumentsModel() to resolve the model instance from a service definition.

$modified : bool = false

Reference flag set to true once a document has been fetched.

Tags
throws
ContainerExceptionInterface

If an error occurs while retrieving an entry from the dependency-injection container.

DependencyException

If the dependency cannot be resolved by the container.

NotFoundException

If no entry is found for the given identifier in the container.

NotFoundExceptionInterface

If no entry is found for the requested identifier in the container.

example
class MyMapper
{
    use AlterGetDocumentPropertyTrait;
}

$mapper   = new MyMapper();
$modified = false;

// Replace a user id by the full user document loaded from the 'UserModel' service
$user = $mapper->alterGetDocument( 42 , [ 'UserModel' , 'id' ] , $container , $modified );
// $user === <document whose id === 42> (or null if not found)
// $modified === true

// A null identifier is returned untouched
$none = $mapper->alterGetDocument( null , [ 'UserModel' , 'id' ] , $container , $modified );
// $none === null
Return values
mixed

The loaded document, null when the lookup failed, or the original $value when it was null or no model could be resolved.

On this page

Search results