RulesChecker
in package
Contains logic for storing and checking rules on entities
RulesCheckers are used by Table classes to ensure that the current entity state satisfies the application logic and business rules.
RulesCheckers afford different rules to be applied in the create and update scenario.
Adding rules
Rules must be callable objects that return true/false depending on whether the rule has been satisfied. You can use RulesChecker::add(), RulesChecker::addCreate(), RulesChecker::addUpdate() and RulesChecker::addDelete to add rules to a checker.
Running checks
Generally a Table object will invoke the rules objects, but you can manually invoke the checks by calling RulesChecker::checkCreate(), RulesChecker::checkUpdate() or RulesChecker::checkDelete().
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'
- _addError() : RuleInvoker
- Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
- _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 toerrorField
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
$thisaddCreate()
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 toerrorField
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
$thisaddDelete()
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 toerrorField
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
$thisaddUpdate()
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 toerrorField
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
$thischeck()
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
Return values
boolcheckCreate()
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
boolcheckDelete()
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
boolcheckUpdate()
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_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_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.