PreUpdateEventArgs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Event;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use InvalidArgumentException;
  22. use function get_class;
  23. use function sprintf;
  24. /**
  25. * Class that holds event arguments for a preInsert/preUpdate event.
  26. */
  27. class PreUpdateEventArgs extends LifecycleEventArgs
  28. {
  29. /** @var array<string,array<int,mixed>> */
  30. private $entityChangeSet;
  31. /**
  32. * @param object $entity
  33. * @param array<string,array<int,mixed>> $changeSet
  34. */
  35. public function __construct($entity, EntityManagerInterface $em, array &$changeSet)
  36. {
  37. parent::__construct($entity, $em);
  38. $this->entityChangeSet = &$changeSet;
  39. }
  40. /**
  41. * Retrieves entity changeset.
  42. *
  43. * @return array<string,array<int,mixed>>
  44. */
  45. public function getEntityChangeSet()
  46. {
  47. return $this->entityChangeSet;
  48. }
  49. /**
  50. * Checks if field has a changeset.
  51. *
  52. * @param string $field
  53. *
  54. * @return bool
  55. */
  56. public function hasChangedField($field)
  57. {
  58. return isset($this->entityChangeSet[$field]);
  59. }
  60. /**
  61. * Gets the old value of the changeset of the changed field.
  62. *
  63. * @param string $field
  64. *
  65. * @return mixed
  66. */
  67. public function getOldValue($field)
  68. {
  69. $this->assertValidField($field);
  70. return $this->entityChangeSet[$field][0];
  71. }
  72. /**
  73. * Gets the new value of the changeset of the changed field.
  74. *
  75. * @param string $field
  76. *
  77. * @return mixed
  78. */
  79. public function getNewValue($field)
  80. {
  81. $this->assertValidField($field);
  82. return $this->entityChangeSet[$field][1];
  83. }
  84. /**
  85. * Sets the new value of this field.
  86. *
  87. * @param string $field
  88. * @param mixed $value
  89. *
  90. * @return void
  91. */
  92. public function setNewValue($field, $value)
  93. {
  94. $this->assertValidField($field);
  95. $this->entityChangeSet[$field][1] = $value;
  96. }
  97. /**
  98. * Asserts the field exists in changeset.
  99. *
  100. * @param string $field
  101. *
  102. * @return void
  103. *
  104. * @throws InvalidArgumentException
  105. */
  106. private function assertValidField($field)
  107. {
  108. if (! isset($this->entityChangeSet[$field])) {
  109. throw new InvalidArgumentException(sprintf(
  110. 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
  111. $field,
  112. get_class($this->getEntity())
  113. ));
  114. }
  115. }
  116. }