Oihana PHP

ExistModelRule extends ContainerRule

Rule: Checks if a given value exists in a model retrieved from a DI container.

This rule validates that a specific value is present in a model implementing the ExistModel interface. The model is retrieved from a PSR-11 compatible container, allowing dynamic resolution of dependencies.

Usage

use DI\Container;
use oihana\validations\rules\models\ExistModelRule;
use tests\oihana\models\mocks\MockDocumentsModel;

$model = new MockDocumentsModel();
$model->addDocument(['id' => 1, 'name' => 'John']);

$container = new Container();
$container->set('model', $model);

$rule = new ExistModelRule($container, ['model' => 'model']);
$rule->check(1);       // true
$rule->check('Alice'); // false

// With custom key
$rule = new ExistModelRule($container, ['model' => 'model', 'key' => 'name']);
$rule->check('John');  // true
$rule->check('Alice'); // false

Constructor Parameters

  • ContainerInterface $container : PSR-11 container reference
  • string|array $init : Model identifier or array of initialization parameters
  • ?string $key : Optional key to use if $init is a string

Options

The rule accepts the following keys in $init:

  • ExistModelRule::MODEL : The container identifier of the model
  • ExistModelRule::KEY : The key in the model to check against (default: Schema::ID)

Exceptions

Throws ParameterException if required parameters (MODEL or KEY) are missing. Throws ContainerExceptionInterface or NotFoundExceptionInterface if container access fails.

Tags
see
ExistModel

The interface that the model must implement to support existence checking

ContainerRule

The abstract base class providing container access

author

Marc Alcaraz

since
1.0.0

Table of Contents

Constants

DEFAULT_KEY  : string = \org\schema\constants\Schema::ID
The default 'key' value.
KEY  : string = 'key'
The 'key' parameter key.
MODEL  : string = 'model'
The 'model' parameter key.

Properties

$container  : ContainerInterface
The DI container reference.
$fillableParams  : array<string|int, mixed>
The internal list of fillable parameters.
$message  : string
The internal message pattern.

Methods

__construct()  : mixed
Creates a new ExistModelRule instance.
check()  : bool
Checks whether the given value satisfies the condition.
key()  : $this
Defines the optional key to find the ressource in the model.
model()  : $this
Defines the model identifier to find it in the DI container.

Constants

DEFAULT_KEY

The default 'key' value.

public string DEFAULT_KEY = \org\schema\constants\Schema::ID

Properties

$container

The DI container reference.

protected ContainerInterface $container

$fillableParams

The internal list of fillable parameters.

protected array<string|int, mixed> $fillableParams = [self::MODEL, self::KEY]

$message

The internal message pattern.

protected string $message = ":attribute is not registered in the model ':model' with the value ':value'"

Methods

__construct()

Creates a new ExistModelRule instance.

public __construct(ContainerInterface $container[, array<string|int, mixed>|string $init = [] ][, string|null $key = null ]) : mixed
Parameters
$container : ContainerInterface

The DI container reference.

$init : array<string|int, mixed>|string = []

The options to passed-in the rule.

$key : string|null = null
Tags
throws
ContainerExceptionInterface

If the container encounters an error while retrieving the entry.

NotFoundExceptionInterface

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

check()

Checks whether the given value satisfies the condition.

public check(mixed $value) : bool
Parameters
$value : mixed

The value to check.

Tags
throws
ContainerExceptionInterface

If the container encounters an error while retrieving the entry.

NotFoundExceptionInterface

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

ParameterException

If a required rule parameter is missing or invalid.

Return values
bool

True if the value satisfies the condition.

key()

Defines the optional key to find the ressource in the model.

public key([string|null $value = null ]) : $this
Parameters
$value : string|null = null

The key value.

Return values
$this

Returns $this to allow method chaining.

model()

Defines the model identifier to find it in the DI container.

public model([string|null $value = null ]) : $this
Parameters
$value : string|null = null

The identifier of the model definition in the DI container.

Return values
$this

Returns $this to allow method chaining.

On this page

Search results