Class RulesChecker
ORM flavoured rules checker.
Adds ORM related features to the RulesChecker class.
Constants
- 
          
          stringCREATE ¶'create'Indicates that the checking rules to apply are those used for creating entities 
- 
          
          stringDELETE ¶'delete'Indicates that the checking rules to apply are those used for deleting entities 
- 
          
          stringUPDATE ¶'update'Indicates that the checking rules to apply are those used for updating entities 
Property Summary
- 
        $_createRules protectedarrayThe list of rules to check during create operations 
- 
        $_deleteRules protectedarrayThe list of rules to check during delete operations 
- 
        $_options protectedarrayList of options to pass to every callable rule 
- 
        $_rules protectedarrayThe list of rules to be checked on both create and update operations 
- 
        $_updateRules protectedarrayThe list of rules to check during update operations 
- 
        $_useI18n protectedboolWhether or not to use I18n functions for translating default error messages 
Method Summary
- 
          __construct() publicConstructor. Takes the options to be passed to all rules. 
- 
          _addError() protectedUtility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid. 
- 
          _checkRules() protectedUsed by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all. 
- 
          add() publicAdds a rule that will be applied to the entity both on create and update operations. 
- 
          addCreate() publicAdds a rule that will be applied to the entity on create operations. 
- 
          addDelete() publicAdds a rule that will be applied to the entity on delete operations. 
- 
          addUpdate() publicAdds a rule that will be applied to the entity on update operations. 
- 
          check() publicRuns 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() publicRuns 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() publicRuns 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() publicRuns 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() publicReturns 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. 
- 
          isUnique() publicReturns a callable that can be used as a rule for checking the uniqueness of a value in the table. 
Method Detail
__construct() ¶ public
__construct(array $options = [])Constructor. Takes the options to be passed to all rules.
Parameters
- 
                array$options optional
- The options to pass to every rule 
_addError() ¶ protected
_addError(callable $rule, string $name, array $options): callableUtility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
Parameters
- 
                callable$rule
- The rule to decorate 
- 
                string$name
- The alias for a rule. 
- 
                array$options
- The options containing the error message and field. 
Returns
callable_checkRules() ¶ protected
_checkRules(Cake\Datasource\EntityInterface $entity, array $options = [], array $rules = []): boolUsed by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all.
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- The entity to check for validity. 
- 
                array$options optional
- Extra options to pass to checker functions. 
- 
                array$rules optional
- The list of rules that must be checked. 
Returns
booladd() ¶ public
add(callable $rule, string $name = null, array $options = []): $thisAdds a rule that will be applied to the entity both on create and update operations.
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- errorFieldif the rule does not pass.
Parameters
- 
                callable$rule
- A callable function or object that will return whether the entity is valid or not. 
- 
                string$name optional
- The alias for a rule. 
- 
                array$options optional
- List of extra options to pass to the rule callable as second argument. 
Returns
$thisaddCreate() ¶ public
addCreate(callable $rule, string $name = null, array $options = []): $thisAdds a rule that will be applied to the entity on create operations.
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- errorFieldif the rule does not pass.
Parameters
- 
                callable$rule
- A callable function or object that will return whether the entity is valid or not. 
- 
                string$name optional
- The alias for a rule. 
- 
                array$options optional
- List of extra options to pass to the rule callable as second argument. 
Returns
$thisaddDelete() ¶ public
addDelete(callable $rule, string $name = null, array $options = []): $thisAdds a rule that will be applied to the entity on delete operations.
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- errorFieldif the rule does not pass.
Parameters
- 
                callable$rule
- A callable function or object that will return whether the entity is valid or not. 
- 
                string$name optional
- The alias for a rule. 
- 
                array$options optional
- List of extra options to pass to the rule callable as second argument. 
Returns
$thisaddUpdate() ¶ public
addUpdate(callable $rule, string $name = null, array $options = []): $thisAdds a rule that will be applied to the entity on update operations.
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- errorFieldif the rule does not pass.
Parameters
- 
                callable$rule
- A callable function or object that will return whether the entity is valid or not. 
- 
                string$name optional
- The alias for a rule. 
- 
                array$options optional
- List of extra options to pass to the rule callable as second argument. 
Returns
$thischeck() ¶ public
check(Cake\Datasource\EntityInterface $entity, string $mode, array $options = []): boolRuns 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
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- The entity to check for validity. 
- 
                string$mode
- Either 'create, 'update' or 'delete'. 
- 
                array$options optional
- Extra options to pass to checker functions. 
Returns
boolThrows
InvalidArgumentExceptionif an invalid mode is passed.
checkCreate() ¶ public
checkCreate(Cake\Datasource\EntityInterface $entity, array $options = []): boolRuns 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'
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- The entity to check for validity. 
- 
                array$options optional
- Extra options to pass to checker functions. 
Returns
boolcheckDelete() ¶ public
checkDelete(Cake\Datasource\EntityInterface $entity, array $options = []): boolRuns 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'
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- The entity to check for validity. 
- 
                array$options optional
- Extra options to pass to checker functions. 
Returns
boolcheckUpdate() ¶ public
checkUpdate(Cake\Datasource\EntityInterface $entity, array $options = []): boolRuns 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'
Parameters
- 
                Cake\Datasource\EntityInterface$entity
- The entity to check for validity. 
- 
                array$options optional
- Extra options to pass to checker functions. 
Returns
boolexistsIn() ¶ public
existsIn(string|array $field, object|string $table, string $message = null): callableReturns 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.
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'));Parameters
- 
                string|array$field
- The field or list of fields to check for existence by primary key lookup in the other table. 
- 
                object|string$table
- The table name where the fields existence will be checked. 
- 
                string$message optional
- The error message to show in case the rule does not pass. 
Returns
callableisUnique() ¶ public
isUnique(array $fields, string $message = null): callableReturns a callable that can be used as a rule for checking the uniqueness of a value in the table.
Example:
$rules->add($rules->isUnique(['email'], 'The email should be unique'));Parameters
- 
                array$fields
- The list of fields to check for uniqueness. 
- 
                string$message optional
- The error message to show in case the rule does not pass. 
Returns
callableProperty Detail
$_useI18n ¶ protected
Whether or not to use I18n functions for translating default error messages
Type
bool