Oihana PHP

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
author

Marc Alcaraz (ekameleon)

since
1.0.0

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

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
throws
Error404

If the id is invalid, the existence check fails, or the document does not exist.

example
// Throws Error404 unless a product with id 105997 exists.
$this->assertExistInModel( 105997 , $productModel , 'product' );

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
throws
DependencyException

If the dependency cannot be resolved by the container.

NotFoundException

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

ContainerExceptionInterface

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

NotFoundExceptionInterface

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

example
// From a service id registered in the container
$model = $this->getDocumentsModel( ProductsModel::class );

// From an existing instance (returned as-is)
$model = $this->getDocumentsModel( $productsModel );
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
throws
UnexpectedValueException

If the 'model' property is not set.

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
throws
DependencyException

If the dependency cannot be resolved by the container.

NotFoundException

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

ContainerExceptionInterface

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

NotFoundExceptionInterface

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

Return values
static

The current instance, for fluent chaining.

On this page

Search results