Skip to content

Commit

Permalink
Adding more doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 3, 2014
1 parent 9acd4ba commit fbf8875
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/ORM/RulesChecker.php
Expand Up @@ -164,6 +164,12 @@ public function checkUpdate(EntityInterface $entity) {
* Returns 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'));
* }}}
*
* @param array $fields The list of fields to check for uniqueness.
* @param string $message The error message to show in case the rule does not pass.
* @return callable
Expand All @@ -173,12 +179,38 @@ public function isUnique(array $fields, $message = 'This value is already in use
return $this->_addError(new IsUnique($fields), compact('errorField', 'message'));
}


/**
* 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.
*
* 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'));
* }}}
*
* @param string|array $fields The field or list of fields to check for existance by
* primary key lookup in the other table.
* @param string $message The error message to show in case the rule does not pass.
* @return callable
*/
public function existsIn($field, $table, $message = 'This value does not exist') {
$errorField = $field;
return $this->_addError(new ExistsIn($field, $table), compact('errorField', 'message'));
}

/**
* Utility method for decorating any callable so that if it returns false, the correct
* property in the entity is marked as invalid.
*
* @param callable $rule The rule to decorate
* @param array $options The options containing the error message and field
* @return callable
*/
protected function _addError($rule, $options) {
return function ($entity, $scope) use ($rule, $options) {
$pass = $rule($entity, $options + $scope);
Expand Down

0 comments on commit fbf8875

Please sign in to comment.