Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
2.3 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\DoctrineBehaviors;
  3. use Zitec\RuleEngineBundle\Entity\Rule;
  4. use Zitec\RuleEngineBundle\Service\RuleContextInterface;
  5. /**
  6. * Class RuleTrait
  7. * A trait that can be used in an entity class to add rule behavior to it.
  8. * It is recommended that you use it in conjecture with RuleInterface.
  9. */
  10. trait RuleTrait
  11. {
  12. /**
  13. * @var Rule
  14. */
  15. private $rule;
  16. /**
  17. * @var Rule
  18. */
  19. private $precondition;
  20. /**
  21. * @var Rule
  22. */
  23. private $postcondition;
  24. /**
  25. * @var RuleContextInterface
  26. */
  27. private $contextObject;
  28. /**
  29. * Set rule
  30. *
  31. * @param null|Rule $rule
  32. *
  33. * @return self
  34. */
  35. public function setRule(?Rule $rule = null)
  36. {
  37. $this->rule = $rule;
  38. return $this;
  39. }
  40. /**
  41. * Get rule
  42. *
  43. * @return Rule
  44. */
  45. public function getRule(): ?Rule
  46. {
  47. return $this->rule;
  48. }
  49. /**
  50. * Get rule
  51. *
  52. * @return Rule
  53. */
  54. public function getPrecondition(): ?Rule
  55. {
  56. return $this->rule;
  57. }
  58. /**
  59. * Set rule
  60. *
  61. * @param null|Rule $rule
  62. *
  63. * @return self
  64. */
  65. public function setPrecondition(?Rule $rule = null)
  66. {
  67. $this->rule = $rule;
  68. return $this;
  69. }
  70. /**
  71. * Get rule
  72. *
  73. * @return Rule
  74. */
  75. public function getPostcondition(): ?Rule
  76. {
  77. return $this->rule;
  78. }
  79. /**
  80. * Set rule
  81. *
  82. * @param null|Rule $rule
  83. *
  84. * @return self
  85. */
  86. public function setPostcondition(?Rule $rule = null)
  87. {
  88. $this->rule = $rule;
  89. return $this;
  90. }
  91. /**
  92. * @return RuleContextInterface
  93. */
  94. public function getContextObject(): RuleContextInterface
  95. {
  96. return $this->contextObject;
  97. }
  98. /**
  99. * @param RuleContextInterface $contextObject
  100. *
  101. * @return self
  102. */
  103. public function setContextObject(RuleContextInterface $contextObject)
  104. {
  105. $this->contextObject = $contextObject;
  106. return $this;
  107. }
  108. /**
  109. * @return string
  110. */
  111. public function __toString()
  112. {
  113. $rule = $this->getRule();
  114. if ($rule instanceof Rule) {
  115. $name = $rule->getName();
  116. if (isset($name)){
  117. return $name;
  118. }
  119. }
  120. return "New rule";
  121. }
  122. }