PreUpdateEventArgs.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Common\Persistence\Event;
  20. use Doctrine\Common\EventArgs;
  21. use Doctrine\Common\Persistence\ObjectManager;
  22. /**
  23. * Class that holds event arguments for a preUpdate event.
  24. *
  25. * @author Guilherme Blanco <guilehrmeblanco@hotmail.com>
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. * @since 2.2
  29. */
  30. class PreUpdateEventArgs extends LifecycleEventArgs
  31. {
  32. /**
  33. * @var array
  34. */
  35. private $entityChangeSet;
  36. /**
  37. * Constructor.
  38. *
  39. * @param object $entity
  40. * @param ObjectManager $objectManager
  41. * @param array $changeSet
  42. */
  43. public function __construct($entity, ObjectManager $objectManager, array &$changeSet)
  44. {
  45. parent::__construct($entity, $objectManager);
  46. $this->entityChangeSet = &$changeSet;
  47. }
  48. /**
  49. * Retrieves the entity changeset.
  50. *
  51. * @return array
  52. */
  53. public function getEntityChangeSet()
  54. {
  55. return $this->entityChangeSet;
  56. }
  57. /**
  58. * Checks if field has a changeset.
  59. *
  60. * @param string $field
  61. *
  62. * @return boolean
  63. */
  64. public function hasChangedField($field)
  65. {
  66. return isset($this->entityChangeSet[$field]);
  67. }
  68. /**
  69. * Gets the old value of the changeset of the changed field.
  70. *
  71. * @param string $field
  72. *
  73. * @return mixed
  74. */
  75. public function getOldValue($field)
  76. {
  77. $this->assertValidField($field);
  78. return $this->entityChangeSet[$field][0];
  79. }
  80. /**
  81. * Gets the new value of the changeset of the changed field.
  82. *
  83. * @param string $field
  84. *
  85. * @return mixed
  86. */
  87. public function getNewValue($field)
  88. {
  89. $this->assertValidField($field);
  90. return $this->entityChangeSet[$field][1];
  91. }
  92. /**
  93. * Sets the new value of this field.
  94. *
  95. * @param string $field
  96. * @param mixed $value
  97. *
  98. * @return void
  99. */
  100. public function setNewValue($field, $value)
  101. {
  102. $this->assertValidField($field);
  103. $this->entityChangeSet[$field][1] = $value;
  104. }
  105. /**
  106. * Asserts the field exists in changeset.
  107. *
  108. * @param string $field
  109. *
  110. * @return void
  111. *
  112. * @throws \InvalidArgumentException
  113. */
  114. private function assertValidField($field)
  115. {
  116. if ( ! isset($this->entityChangeSet[$field])) {
  117. throw new \InvalidArgumentException(sprintf(
  118. 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
  119. $field,
  120. get_class($this->getEntity())
  121. ));
  122. }
  123. }
  124. }