Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Turning the code within Datasource\RulesChecker cleaner avoiding unec…
…essary code repetition
  • Loading branch information
mylux committed Dec 28, 2015
1 parent 01211a6 commit 72eee41
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/Datasource/RulesChecker.php
Expand Up @@ -248,12 +248,7 @@ public function check(EntityInterface $entity, $mode, array $options = [])
*/
public function checkCreate(EntityInterface $entity, array $options = [])
{
$success = true;
$options = $options + $this->_options;
foreach (array_merge($this->_rules, $this->_createRules) as $rule) {
$success = $rule($entity, $options) && $success;
}
return $success;
return $this->_checkRules($entity, $options, array_merge($this->_rules, $this->_createRules));
}

/**
Expand All @@ -266,12 +261,7 @@ public function checkCreate(EntityInterface $entity, array $options = [])
*/
public function checkUpdate(EntityInterface $entity, array $options = [])
{
$success = true;
$options = $options + $this->_options;
foreach (array_merge($this->_rules, $this->_updateRules) as $rule) {
$success = $rule($entity, $options) && $success;
}
return $success;
return $this->_checkRules($entity, $options, array_merge($this->_rules, $this->_updateRules));
}

/**
Expand All @@ -283,10 +273,24 @@ public function checkUpdate(EntityInterface $entity, array $options = [])
* @return bool
*/
public function checkDelete(EntityInterface $entity, array $options = [])
{
return $this->_checkRules($entity, $options, $this->_deleteRules);
}

/**
* Used by top level functions checkDelete, checkCreate and checkUpdate, this function
* iterates an array containing the rules to be checked and check them all.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @param array $rules the list of rules that must be checked
* @return bool
*/
protected function _checkRules(EntityInterface $entity, array $options = [], array $rules = [])
{
$success = true;
$options = $options + $this->_options;
foreach ($this->_deleteRules as $rule) {
foreach ($rules as $rule) {
$success = $rule($entity, $options) && $success;
}
return $success;
Expand Down

0 comments on commit 72eee41

Please sign in to comment.