ModelTrait uses trait:short
Provides a `model` reference exposing a {@see DocumentsModel} to the host class.
Mix this trait into a controller or service that operates on a single document model. It builds on DocumentsTrait to resolve the model from a direct instance or a DI container service id during initialization, and offers assertModel() to guard against using it before it is set.
Tags
Table of Contents
Properties
- $model : DocumentsModel|null
- The document model reference.
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.
- assertModel() : void
- Asserts that the `$model` property has been set.
- initializeModel() : static
- Initializes the `$model` reference from an initialization array.
Properties
$model
The document model reference.
public
DocumentsModel|null
$model
= null
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.
assertModel()
Asserts that the `$model` property has been set.
protected
assertModel() : void
Tags
initializeModel()
Initializes the `$model` reference from an initialization array.
protected
initializeModel([array<string|int, mixed> $init = [] ]) : static
The value is read from the ModelParam::MODEL key (falling back to the current $model)
and resolved through DocumentsTrait::getDocumentsModel(), which accepts either a ready
instance or a container service id.
Parameters
- $init : array<string|int, mixed> = []
-
Initialization options (key:
ModelParam::MODEL).
Tags
Return values
static —The current instance, for fluent chaining.