Oihana PHP System

UniqueModelRule extends ExistModelRule

Rule: Validates that a given value is **unique** within a model managed by the DI container.

This rule extends ExistModelRule and inverts its logic: it passes only if the provided value does not already exist in the target model.

The model must implement ExistModel, which defines the exist(array $criteria): bool method used for lookups.


Usage

use oihana\validations\rules\UniqueModelRule;
use Somnambulist\Components\Validation\Validator;
use Psr\Container\ContainerInterface;

// Assume $container provides access to models implementing ExistModel.

$rule = new UniqueModelRule
(
    $container,
    [
        UniqueModelRule::MODEL => 'user.model',
        UniqueModelRule::KEY   => 'email',
    ]
);

$validator = new Validator
(
    ['email' => 'john@example.com'],
    ['email' => [$rule]]
);

$validator->passes(); // true if no existing user with this email
$validator->fails();  // true if a user with this email already exists

Behavior

  • Inherits all initialization logic from ExistModelRule.
  • Calls ExistModelRule::check() internally and returns its logical negation.
  • Requires that:
    • the model exists in the DI container,
    • the model implements ExistModel,
    • the model correctly responds to exist([ModelParam::KEY => ..., ModelParam::VALUE => ...]).

Custom Error Messages

$rule = new UniqueModelRule($container, 'user.model', 'email');
$rule->message(':attribute already exists in :model.');

Tags
see
ExistModelRule

The base class that checks for existence in a model.

ExistModel

Interface that must be implemented by model classes.

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.
LOGGABLE  : string = 'loggable'
The 'loggable' parameter constant.
LOGGER  : string = 'logger'
The 'logger' parameter constant.
MODEL  : string = 'model'
The 'model' parameter key.

Properties

$loggable  : bool
The loggable flag.
$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.
__toString()  : string
Returns a String representation of the object.
alert()  : void
Action must be taken immediately.
check()  : bool
Checks whether the given value satisfies the condition.
critical()  : void
Critical conditions.
debug()  : void
Detailed debug information.
emergency()  : void
System is unusable.
error()  : void
Runtime errors that do not require immediate action but should typically be logged and monitored.
getLogger()  : LoggerInterface|null
Returns the logger reference.
info()  : void
Interesting events.
initializeLoggable()  : static
Initialize the loggable flag.
initializeLogger()  : static
Initializes the logger reference for the current instance.
key()  : $this
Defines the optional key to find the ressource in the model.
log()  : void
Logs with an arbitrary level.
model()  : $this
Defines the model identifier to find it in the DI container.
notice()  : void
Normal but significant events.
warning()  : void
Exceptional occurrences that are not errors.

Constants

DEFAULT_KEY

The default 'key' value.

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

LOGGABLE

The 'loggable' parameter constant.

public string LOGGABLE = 'loggable'

LOGGER

The 'logger' parameter constant.

public string LOGGER = 'logger'

Properties

$loggable

The loggable flag.

public bool $loggable = false

$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 must be unique in the model ':model', the value ':value' already exist."

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
NotFoundExceptionInterface

__toString()

Returns a String representation of the object.

public __toString() : string
Tags
throws
ReflectionException
Return values
string

A string representation of the object.

alert()

Action must be taken immediately.

public alert(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void

Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.

Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

check()

Checks whether the given value satisfies the condition.

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

The value to check.

Tags
throws
ContainerExceptionInterface
NotFoundExceptionInterface
ParameterException
Return values
bool

True if the value satisfies the condition.

critical()

Critical conditions.

public critical(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void

Example: Application component unavailable, unexpected exception.

Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

debug()

Detailed debug information.

public debug(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

emergency()

System is unusable.

public emergency(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

error()

Runtime errors that do not require immediate action but should typically be logged and monitored.

public error(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

getLogger()

Returns the logger reference.

public getLogger() : LoggerInterface|null
Return values
LoggerInterface|null

info()

Interesting events.

public info(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void

Example: User logs in, SQL logs.

Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

initializeLoggable()

Initialize the loggable flag.

public initializeLoggable([bool|array<string|int, mixed>|null $init = null ][, ContainerInterface|null $container = null ][, bool|array<string|int, mixed>|null $defaultValue = false ]) : static
Parameters
$init : bool|array<string|int, mixed>|null = null

The definition to initialize the loggable property.

$container : ContainerInterface|null = null
$defaultValue : bool|array<string|int, mixed>|null = false

The default value if the $init argument is not defined.

Tags
throws
ContainerExceptionInterface
NotFoundExceptionInterface
Return values
static

initializeLogger()

Initializes the logger reference for the current instance.

public initializeLogger([array<string|int, mixed>|LoggerInterface|string|null $init = null ][, ContainerInterface|null $container = null ][, bool $useDefault = true ]) : static

This method accepts either:

  • A LoggerInterface instance
  • An associative array containing a logger reference under the static::LOGGER key
  • A string representing a service ID or class name resolvable by the container
  • null or an empty value, which will default to LoggerInterface::class depending on the $useDefault parameter.

If a dependency injection container is provided, the method will attempt to resolve the logger service from it. If no valid logger can be resolved, the $this->logger property will be set to null.

Parameters
$init : array<string|int, mixed>|LoggerInterface|string|null = null

Logger initialization data. May be an instance, an array with a logger entry, a string service ID/class name, or null.

$container : ContainerInterface|null = null

Optional dependency injection container used to resolve the logger service.

$useDefault : bool = true

Whether to use LoggerInterface::class as a fallback if $init does not provide a valid logger string. Defaults to true.

Tags
throws
DependencyException

If there is a dependency resolution error.

NotFoundException

If the specified service is not found in the container.

NotFoundExceptionInterface

If the specified service is not found in the container.

ContainerExceptionInterface

If the container encounters a general error.

Return values
static

Returns the current instance for method chaining.

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

log()

Logs with an arbitrary level.

public log(mixed $level, string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$level : mixed
$message : string|Stringable
$context : array<string|int, mixed> = []

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

notice()

Normal but significant events.

public notice(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []

warning()

Exceptional occurrences that are not errors.

public warning(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void

Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.

Parameters
$message : string|Stringable
$context : array<string|int, mixed> = []
On this page

Search results