|
|
@@ -17,6 +17,7 @@ namespace Cake\ORM;
|
|
|
use Cake\Datasource\EntityInterface;
|
|
|
use Cake\ORM\Rule\ExistsIn;
|
|
|
use Cake\ORM\Rule\IsUnique;
|
|
|
+use InvalidArgumentException;
|
|
|
|
|
|
/**
|
|
|
* Contains logic for storing and checking rules on entities
|
|
|
@@ -30,18 +31,40 @@ use Cake\ORM\Rule\IsUnique;
|
|
|
* ### Adding rules
|
|
|
*
|
|
|
* Rules must be callable objects that return true/false depending on whether or
|
|
|
- * not the rule has been satisified. You can use RulesChecker::add(), RulesChecker::addCreate()
|
|
|
- * and RulesChecker::addUpdate() to add rules to a checker.
|
|
|
+ * not the rule has been satisified. 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() or RulesChecker::checkUpdate().
|
|
|
+ * invoke the checks by calling RulesChecker::checkCreate(), RulesChecker::checkUpdate() or
|
|
|
+ * RulesChecker::checkDelete().
|
|
|
*/
|
|
|
class RulesChecker {
|
|
|
|
|
|
/**
|
|
|
- * The list of rules to be checked on every case
|
|
|
+ * Indicates that the checking rules to apply are those used for creating entities
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ const CREATE = 'create';
|
|
|
+
|
|
|
+/**
|
|
|
+ * Indicates that the checking rules to apply are those used for updating entities
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ const UPDATE = 'update';
|
|
|
+
|
|
|
+/**
|
|
|
+ * Indicates that the checking rules to apply are those used for deleting entities
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ const DELETE = 'delete';
|
|
|
+
|
|
|
+/**
|
|
|
+ * The list of rules to be checked on both create and update operations
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
@@ -62,6 +85,13 @@ class RulesChecker {
|
|
|
protected $_updateRules = [];
|
|
|
|
|
|
/**
|
|
|
+ * The list of rules to check during delete operations
|
|
|
+ *
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected $_deleteRules = [];
|
|
|
+
|
|
|
+/**
|
|
|
* List of options to pass to every callable rule
|
|
|
*
|
|
|
* @var array
|
|
|
@@ -145,6 +175,53 @@ class RulesChecker {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Adds 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 `errorField` if the rule does not pass.
|
|
|
+ *
|
|
|
+ * @param callable $rule A callable function or object that will return whether
|
|
|
+ * the entity is valid or not.
|
|
|
+ * @param array $options List of extra options to pass to the rule callable as
|
|
|
+ * second argument.
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ public function addDelete(callable $rule, array $options = []) {
|
|
|
+ $this->_deleteRules[] = $this->_addError($rule, $options);
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * 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
|
|
|
+ *
|
|
|
+ * @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
|
|
|
+ * @return bool
|
|
|
+ * @throws \InvalidArgumentException if an invalid mode is passed.
|
|
|
+ */
|
|
|
+ public function check(EntityInterface $entity, $mode) {
|
|
|
+ if ($mode === self::CREATE) {
|
|
|
+ return $this->checkCreate($entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($mode === self::UPDATE) {
|
|
|
+ return $this->checkUpdate($entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($mode === self::DELETE) {
|
|
|
+ return $this->checkDelete($entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new InvalidArgumentException('Wrong checking mode: ' . $mode);
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
* 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'
|
|
|
*
|
|
|
@@ -175,6 +252,21 @@ class RulesChecker {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 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'
|
|
|
+ *
|
|
|
+ * @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function checkDelete(EntityInterface $entity) {
|
|
|
+ $success = true;
|
|
|
+ foreach ($this->_deleteRules as $rule) {
|
|
|
+ $success = $rule($entity, $this->_options) && $success;
|
|
|
+ }
|
|
|
+ return $success;
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
* Returns a callable that can be used as a rule for checking the uniqueness of a value
|
|
|
* in the table.
|
|
|
*
|