RulesAwareTrait.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.7
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Datasource;
  16. use ArrayObject;
  17. use Cake\Event\EventDispatcherInterface;
  18. /**
  19. * A trait that allows a class to build and apply application.
  20. * rules.
  21. *
  22. * If the implementing class also implements EventAwareTrait, then
  23. * events will be emitted when rules are checked.
  24. *
  25. * The implementing class is expected to define the `RULES_CLASS` constant
  26. * if they need to customize which class is used for rules objects.
  27. */
  28. trait RulesAwareTrait
  29. {
  30. /**
  31. * The domain rules to be applied to entities saved by this table
  32. *
  33. * @var \Cake\Datasource\RulesChecker
  34. */
  35. protected $_rulesChecker;
  36. /**
  37. * Returns whether or not the passed entity complies with all the rules stored in
  38. * the rules checker.
  39. *
  40. * @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
  41. * @param string $operation The operation being run. Either 'create', 'update' or 'delete'.
  42. * @param \ArrayObject|array|null $options The options To be passed to the rules.
  43. * @return bool
  44. */
  45. public function checkRules(EntityInterface $entity, $operation = RulesChecker::CREATE, $options = null)
  46. {
  47. $rules = $this->rulesChecker();
  48. $options = $options ?: new ArrayObject();
  49. $options = is_array($options) ? new ArrayObject($options) : $options;
  50. $hasEvents = ($this instanceof EventDispatcherInterface);
  51. if ($hasEvents) {
  52. $event = $this->dispatchEvent(
  53. 'Model.beforeRules',
  54. compact('entity', 'options', 'operation')
  55. );
  56. if ($event->isStopped()) {
  57. return $event->getResult();
  58. }
  59. }
  60. $result = $rules->check($entity, $operation, $options->getArrayCopy());
  61. if ($hasEvents) {
  62. $event = $this->dispatchEvent(
  63. 'Model.afterRules',
  64. compact('entity', 'options', 'result', 'operation')
  65. );
  66. if ($event->isStopped()) {
  67. return $event->getResult();
  68. }
  69. }
  70. return $result;
  71. }
  72. /**
  73. * Returns the RulesChecker for this instance.
  74. *
  75. * A RulesChecker object is used to test an entity for validity
  76. * on rules that may involve complex logic or data that
  77. * needs to be fetched from relevant datasources.
  78. *
  79. * @see \Cake\Datasource\RulesChecker
  80. * @return \Cake\Datasource\RulesChecker
  81. */
  82. public function rulesChecker()
  83. {
  84. if ($this->_rulesChecker !== null) {
  85. return $this->_rulesChecker;
  86. }
  87. $class = defined('static::RULES_CLASS') ? static::RULES_CLASS : 'Cake\Datasource\RulesChecker';
  88. $this->_rulesChecker = $this->buildRules(new $class(['repository' => $this]));
  89. $this->dispatchEvent('Model.buildRules', ['rules' => $this->_rulesChecker]);
  90. return $this->_rulesChecker;
  91. }
  92. /**
  93. * Returns a RulesChecker object after modifying the one that was supplied.
  94. *
  95. * Subclasses should override this method in order to initialize the rules to be applied to
  96. * entities saved by this instance.
  97. *
  98. * @param \Cake\Datasource\RulesChecker $rules The rules object to be modified.
  99. * @return \Cake\Datasource\RulesChecker
  100. */
  101. public function buildRules(RulesChecker $rules)
  102. {
  103. return $rules;
  104. }
  105. }