/src/Datasource/RulesAwareTrait.php

https://github.com/binondord/cakephp · PHP · 116 lines · 51 code · 9 blank · 56 comment · 5 complexity · 417ed0b663d8092476c5d0c614e0456b MD5 · raw file

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