PreUpdateEventArgs.php 3.5 KB

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