Oihana PHP

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
author

Marc Alcaraz (ekameleon)

since
1.0.0

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
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.

On this page

Search results