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
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
nullvalue is returned as-is. - $definition : array<string|int, mixed> = []
-
The lookup definition:
[ 0 => model class/identifier , 1 => lookup key field ]. Both default tonull. - $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
trueonce a document has been fetched.
Tags
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.