Fr3nch13/CakePHP Utilities

RulesChecker extends RulesChecker
in package

ORM flavoured rules checker.

Adds ORM related features to the RulesChecker class.

Tags
see
RulesChecker

Table of Contents

Constants

CREATE  = 'create'
Indicates that the checking rules to apply are those used for creating entities
DELETE  = 'delete'
Indicates that the checking rules to apply are those used for deleting entities
UPDATE  = 'update'
Indicates that the checking rules to apply are those used for updating entities

Properties

$_createRules  : array<string|int, RuleInvoker>
The list of rules to check during create operations
$_deleteRules  : array<string|int, RuleInvoker>
The list of rules to check during delete operations
$_options  : array<string|int, mixed>
List of options to pass to every callable rule
$_rules  : array<string|int, RuleInvoker>
The list of rules to be checked on both create and update operations
$_updateRules  : array<string|int, RuleInvoker>
The list of rules to check during update operations
$_useI18n  : bool
Whether to use I18n functions for translating default error messages

Methods

__construct()  : mixed
Constructor. Takes the options to be passed to all rules.
add()  : $this
Adds a rule that will be applied to the entity both on create and update operations.
addCreate()  : $this
Adds a rule that will be applied to the entity on create operations.
addDelete()  : $this
Adds a rule that will be applied to the entity on delete operations.
addUpdate()  : $this
Adds a rule that will be applied to the entity on update operations.
check()  : bool
Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules to be applied are depended on the $mode parameter which can only be RulesChecker::CREATE, RulesChecker::UPDATE or RulesChecker::DELETE
checkCreate()  : bool
Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'create'
checkDelete()  : bool
Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'delete'
checkUpdate()  : bool
Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'update'
existsIn()  : RuleInvoker
Returns a callable that can be used as a rule for checking that the values extracted from the entity to check exist as the primary key in another table.
isLinkedTo()  : RuleInvoker
Validates whether links to the given association exist.
isNotLinkedTo()  : RuleInvoker
Validates whether links to the given association do not exist.
isUnique()  : RuleInvoker
Returns a callable that can be used as a rule for checking the uniqueness of a value in the table.
validCount()  : RuleInvoker
Validates the count of associated records.
_addError()  : RuleInvoker
Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
_addLinkConstraintRule()  : RuleInvoker
Adds a link constraint rule.
_checkRules()  : bool
Used by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all.

Constants

CREATE

Indicates that the checking rules to apply are those used for creating entities

public string CREATE = 'create'

DELETE

Indicates that the checking rules to apply are those used for deleting entities

public string DELETE = 'delete'

UPDATE

Indicates that the checking rules to apply are those used for updating entities

public string UPDATE = 'update'

Properties

$_createRules

The list of rules to check during create operations

protected array<string|int, RuleInvoker> $_createRules = []

$_deleteRules

The list of rules to check during delete operations

protected array<string|int, RuleInvoker> $_deleteRules = []

$_options

List of options to pass to every callable rule

protected array<string|int, mixed> $_options = []

$_rules

The list of rules to be checked on both create and update operations

protected array<string|int, RuleInvoker> $_rules = []

$_updateRules

The list of rules to check during update operations

protected array<string|int, RuleInvoker> $_updateRules = []

$_useI18n

Whether to use I18n functions for translating default error messages

protected bool $_useI18n = false

Methods

__construct()

Constructor. Takes the options to be passed to all rules.

public __construct([array<string, mixed> $options = [] ]) : mixed
Parameters
$options : array<string, mixed> = []

The options to pass to every rule

add()

Adds a rule that will be applied to the entity both on create and update operations.

public add(callable $rule[, array<string|int, mixed>|string|null $name = null ][, array<string, mixed> $options = [] ]) : $this

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.
Parameters
$rule : callable

A callable function or object that will return whether the entity is valid or not.

$name : array<string|int, mixed>|string|null = null

The alias for a rule, or an array of options.

$options : array<string, mixed> = []

List of extra options to pass to the rule callable as second argument.

Return values
$this

addCreate()

Adds a rule that will be applied to the entity on create operations.

public addCreate(callable $rule[, array<string|int, mixed>|string|null $name = null ][, array<string, mixed> $options = [] ]) : $this

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.
Parameters
$rule : callable

A callable function or object that will return whether the entity is valid or not.

$name : array<string|int, mixed>|string|null = null

The alias for a rule or an array of options.

$options : array<string, mixed> = []

List of extra options to pass to the rule callable as second argument.

Return values
$this

addDelete()

Adds a rule that will be applied to the entity on delete operations.

public addDelete(callable $rule[, array<string|int, mixed>|string|null $name = null ][, array<string, mixed> $options = [] ]) : $this

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.
Parameters
$rule : callable

A callable function or object that will return whether the entity is valid or not.

$name : array<string|int, mixed>|string|null = null

The alias for a rule, or an array of options.

$options : array<string, mixed> = []

List of extra options to pass to the rule callable as second argument.

Return values
$this

addUpdate()

Adds a rule that will be applied to the entity on update operations.

public addUpdate(callable $rule[, array<string|int, mixed>|string|null $name = null ][, array<string, mixed> $options = [] ]) : $this

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.
Parameters
$rule : callable

A callable function or object that will return whether the entity is valid or not.

$name : array<string|int, mixed>|string|null = null

The alias for a rule, or an array of options.

$options : array<string, mixed> = []

List of extra options to pass to the rule callable as second argument.

Return values
$this

check()

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules to be applied are depended on the $mode parameter which can only be RulesChecker::CREATE, RulesChecker::UPDATE or RulesChecker::DELETE

public check(EntityInterface $entity, string $mode[, array<string, mixed> $options = [] ]) : bool
Parameters
$entity : EntityInterface

The entity to check for validity.

$mode : string

Either 'create, 'update' or 'delete'.

$options : array<string, mixed> = []

Extra options to pass to checker functions.

Tags
throws
InvalidArgumentException

if an invalid mode is passed.

Return values
bool

checkCreate()

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'create'

public checkCreate(EntityInterface $entity[, array<string, mixed> $options = [] ]) : bool
Parameters
$entity : EntityInterface

The entity to check for validity.

$options : array<string, mixed> = []

Extra options to pass to checker functions.

Return values
bool

checkDelete()

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'delete'

public checkDelete(EntityInterface $entity[, array<string, mixed> $options = [] ]) : bool
Parameters
$entity : EntityInterface

The entity to check for validity.

$options : array<string, mixed> = []

Extra options to pass to checker functions.

Return values
bool

checkUpdate()

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'update'

public checkUpdate(EntityInterface $entity[, array<string, mixed> $options = [] ]) : bool
Parameters
$entity : EntityInterface

The entity to check for validity.

$options : array<string, mixed> = []

Extra options to pass to checker functions.

Return values
bool

existsIn()

Returns a callable that can be used as a rule for checking that the values extracted from the entity to check exist as the primary key in another table.

public existsIn(array<string|int, string>|string $field, Table|Association|string $table[, array<string, mixed>|string|null $message = null ]) : RuleInvoker

This is useful for enforcing foreign key integrity checks.

Example:

$rules->add($rules->existsIn('author_id', 'Authors', 'Invalid Author'));

$rules->add($rules->existsIn('site_id', new SitesTable(), 'Invalid Site'));

Available $options are error 'message' and 'allowNullableNulls' flag. 'message' sets a custom error message. Set 'allowNullableNulls' to true to accept composite foreign keys where one or more nullable columns are null.

Parameters
$field : array<string|int, string>|string

The field or list of fields to check for existence by primary key lookup in the other table.

$table : Table|Association|string

The table name where the fields existence will be checked.

$message : array<string, mixed>|string|null = null

The error message to show in case the rule does not pass. Can also be an array of options. When an array, the 'message' key can be used to provide a message.

Return values
RuleInvoker

isLinkedTo()

Validates whether links to the given association exist.

public isLinkedTo(Association|string $association[, string|null $field = null ][, string|null $message = null ]) : RuleInvoker

Example:

$rules->addUpdate($rules->isLinkedTo('Articles', 'article'));

On a Comments table that has a belongsTo Articles association, this check would ensure that comments can only be edited as long as they are associated to an existing article.

Parameters
$association : Association|string

The association to check for links.

$field : string|null = null

The name of the association property. When supplied, this is the name used to set possible errors. When absent, the name is inferred from $association.

$message : string|null = null

The error message to show in case the rule does not pass.

Tags
since
4.0.0
Return values
RuleInvoker

isNotLinkedTo()

Validates whether links to the given association do not exist.

public isNotLinkedTo(Association|string $association[, string|null $field = null ][, string|null $message = null ]) : RuleInvoker

Example:

$rules->addDelete($rules->isNotLinkedTo('Comments', 'comments'));

On a Articles table that has a hasMany Comments association, this check would ensure that articles can only be deleted when no associated comments exist.

Parameters
$association : Association|string

The association to check for links.

$field : string|null = null

The name of the association property. When supplied, this is the name used to set possible errors. When absent, the name is inferred from $association.

$message : string|null = null

The error message to show in case the rule does not pass.

Tags
since
4.0.0
Return values
RuleInvoker

isUnique()

Returns a callable that can be used as a rule for checking the uniqueness of a value in the table.

public isUnique(array<string|int, string> $fields[, array<string, mixed>|string|null $message = null ]) : RuleInvoker

Example

$rules->add($rules->isUnique(['email'], 'The email should be unique'));

Options

  • allowMultipleNulls Allows any field to have multiple null values. Defaults to false.
Parameters
$fields : array<string|int, string>

The list of fields to check for uniqueness.

$message : array<string, mixed>|string|null = null

The error message to show in case the rule does not pass. Can also be an array of options. When an array, the 'message' key can be used to provide a message.

Return values
RuleInvoker

validCount()

Validates the count of associated records.

public validCount(string $field[, int $count = 0 ][, string $operator = '>' ][, string|null $message = null ]) : RuleInvoker
Parameters
$field : string

The field to check the count on.

$count : int = 0

The expected count.

$operator : string = '>'

The operator for the count comparison.

$message : string|null = null

The error message to show in case the rule does not pass.

Return values
RuleInvoker

_addError()

Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.

protected _addError(callable|RuleInvoker $rule[, array<string|int, mixed>|string|null $name = null ][, array<string, mixed> $options = [] ]) : RuleInvoker
Parameters
$rule : callable|RuleInvoker

The rule to decorate

$name : array<string|int, mixed>|string|null = null

The alias for a rule or an array of options

$options : array<string, mixed> = []

The options containing the error message and field.

Return values
RuleInvoker

_addLinkConstraintRule()

Adds a link constraint rule.

protected _addLinkConstraintRule(Association|string $association, string|null $errorField, string|null $message, string $linkStatus, string $ruleName) : RuleInvoker
Parameters
$association : Association|string

The association to check for links.

$errorField : string|null

The name of the property to use for setting possible errors. When absent, the name is inferred from $association.

$message : string|null

The error message to show in case the rule does not pass.

$linkStatus : string

The ink status required for the check to pass.

$ruleName : string

The alias/name of the rule.

Tags
throws
InvalidArgumentException

In case the $association argument is of an invalid type.

since
4.0.0
see
RulesChecker::isLinkedTo()
see
RulesChecker::isNotLinkedTo()
see
LinkConstraint::STATUS_LINKED
see
LinkConstraint::STATUS_NOT_LINKED
Return values
RuleInvoker

_checkRules()

Used by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all.

protected _checkRules(EntityInterface $entity[, array<string, mixed> $options = [] ][, array<string|int, RuleInvoker$rules = [] ]) : bool
Parameters
$entity : EntityInterface

The entity to check for validity.

$options : array<string, mixed> = []

Extra options to pass to checker functions.

$rules : array<string|int, RuleInvoker> = []

The list of rules that must be checked.

Return values
bool

        
On this page

Search results