DocumentsTrait uses \oihana\traits\ContainerTrait
Provides helpers to resolve and validate document models through the DI container.
Mix this trait into controllers or services that depend on a DocumentsModel. It can:
- resolve a model from either a ready instance or a container service id (getDocumentsModel());
- assert that a referenced document actually exists in a given ExistModel, raising an Error404 otherwise (assertExistInModel()).
It relies on ContainerTrait for container access.
Tags
Table of Contents
Methods
- assertExistInModel() : void
- Asserts that a document (identified by an id) exists in the given model.
- getDocumentsModel() : DocumentsModel|null
- Resolves a {@see DocumentsModel} instance, either directly or via the DI container.
Methods
assertExistInModel()
Asserts that a document (identified by an id) exists in the given model.
public
assertExistInModel(object|string|int|null $document, ExistModel $model[, string $name = Char::EMPTY ][, string|null $key = 'id' ]) : void
The id is taken from $document->{$key} when an object is passed, or used directly when a
scalar is passed. The model is queried through ExistModel::exist() with the id bound
under $key. Any failure — invalid id, lookup exception, or non-existent document — results
in an Error404.
Parameters
- $document : object|string|int|null
-
The document instance or the raw id to validate.
- $model : ExistModel
-
The model used to check existence.
- $name : string = Char::EMPTY
-
Human-readable resource name, injected into the error message.
- $key : string|null = 'id'
-
The property/bind key holding the identifier. Defaults to
'id'.
Tags
getDocumentsModel()
Resolves a {@see DocumentsModel} instance, either directly or via the DI container.
public
getDocumentsModel(DocumentsModel|string|null $documents) : DocumentsModel|null
When a string is passed and the container holds a matching entry, the service is resolved.
The resulting value is returned only if it is a DocumentsModel; otherwise null.
Parameters
- $documents : DocumentsModel|string|null
-
A ready model instance, a container service id, or
null.
Tags
Return values
DocumentsModel|null —The resolved model, or null when it cannot be resolved.