Skip to content

Commit

Permalink
Passing save options to the rules checker.
Browse files Browse the repository at this point in the history
This will help passing some extra data or disabling temporarily some rules
  • Loading branch information
lorenzo committed Dec 26, 2014
1 parent 363f8d0 commit b001256
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
27 changes: 17 additions & 10 deletions src/ORM/RulesChecker.php
Expand Up @@ -203,20 +203,21 @@ public function addDelete(callable $rule, array $options = []) {
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param string $mode Either 'create, 'update' or 'delete'.
* @param array $options Extra options to pass to checker functions.
* @return bool
* @throws \InvalidArgumentException if an invalid mode is passed.
*/
public function check(EntityInterface $entity, $mode) {
public function check(EntityInterface $entity, $mode, array $options = []) {
if ($mode === self::CREATE) {
return $this->checkCreate($entity);
return $this->checkCreate($entity, $options);
}

if ($mode === self::UPDATE) {
return $this->checkUpdate($entity);
return $this->checkUpdate($entity, $options);
}

if ($mode === self::DELETE) {
return $this->checkDelete($entity);
return $this->checkDelete($entity, $options);
}

throw new InvalidArgumentException('Wrong checking mode: ' . $mode);
Expand All @@ -227,12 +228,14 @@ public function check(EntityInterface $entity, $mode) {
* of them pass. The rules selected will be only those specified to be run on 'create'
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @return bool
*/
public function checkCreate(EntityInterface $entity) {
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, $this->_options) && $success;
$success = $rule($entity, $options) && $success;
}
return $success;
}
Expand All @@ -242,12 +245,14 @@ public function checkCreate(EntityInterface $entity) {
* of them pass. The rules selected will be only those specified to be run on 'update'
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @return bool
*/
public function checkUpdate(EntityInterface $entity) {
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, $this->_options) && $success;
$success = $rule($entity, $options) && $success;
}
return $success;
}
Expand All @@ -257,12 +262,14 @@ public function checkUpdate(EntityInterface $entity) {
* of them pass. The rules selected will be only those specified to be run on 'delete'
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @return bool
*/
public function checkDelete(EntityInterface $entity) {
public function checkDelete(EntityInterface $entity, array $options = []) {
$success = true;
$options = $options + $this->_options;
foreach ($this->_deleteRules as $rule) {
$success = $rule($entity, $this->_options) && $success;
$success = $rule($entity, $options) && $success;
}
return $success;
}
Expand Down
19 changes: 12 additions & 7 deletions src/ORM/Table.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ORM;

use ArrayObject;
use BadMethodCallException;
use Cake\Core\App;
use Cake\Database\Schema\Table as Schema;
Expand Down Expand Up @@ -1222,7 +1223,7 @@ public function exists($conditions) {
*
*/
public function save(EntityInterface $entity, $options = []) {
$options = new \ArrayObject($options + [
$options = new ArrayObject($options + [
'atomic' => true,
'associated' => true,
'checkRules' => true
Expand Down Expand Up @@ -1269,7 +1270,7 @@ protected function _processSave($entity, $options) {
}

$mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
if ($options['checkRules'] && !$this->checkRules($entity, $mode)) {
if ($options['checkRules'] && !$this->checkRules($entity, $mode, $options)) {
return false;
}

Expand Down Expand Up @@ -1469,7 +1470,7 @@ protected function _update($entity, $data) {
*
*/
public function delete(EntityInterface $entity, $options = []) {
$options = new \ArrayObject($options + ['atomic' => true, 'checkRules' => true]);
$options = new ArrayObject($options + ['atomic' => true, 'checkRules' => true]);

$process = function () use ($entity, $options) {
return $this->_processDelete($entity, $options);
Expand Down Expand Up @@ -1905,24 +1906,28 @@ public function validateUnique($value, array $options, array $context = []) {
* the rules checker.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param \ArrayObject|array $options The options To be passed to the rules.
* @param string $operation Either 'create, 'update' or 'delete'.
* @return bool
*/
public function checkRules(EntityInterface $entity, $operation = RulesChecker::CREATE) {
public function checkRules(EntityInterface $entity, $operation = RulesChecker::CREATE, $options = null) {
$rules = $this->rulesChecker();
$options = $options ?: new ArrayObject;
$options = is_array($options) ? new ArrayObject($options) : $options;

$event = $this->dispatchEvent(
'Model.beforeRules',
compact('entity', 'rules', 'operation')
compact('entity', 'options', 'operation', 'rules')
);

if ($event->isStopped()) {
return $event->result;
}

$result = $rules->check($entity, $operation);
$result = $rules->check($entity, $operation, $options->getArrayCopy());
$event = $this->dispatchEvent(
'Model.afterRules',
compact('entity', 'rules', 'result', 'operation')
compact('entity', 'options', 'result', 'operation', 'rules')
);

if ($event->isStopped()) {
Expand Down

0 comments on commit b001256

Please sign in to comment.