PreUpdateEventArgs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Persistence\Event;
  20. use Doctrine\Common\EventArgs,
  21. 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. * Retrieve entity changeset.
  50. *
  51. * @return array
  52. */
  53. public function getEntityChangeSet()
  54. {
  55. return $this->entityChangeSet;
  56. }
  57. /**
  58. * Check if field has a changeset.
  59. *
  60. * @return boolean
  61. */
  62. public function hasChangedField($field)
  63. {
  64. return isset($this->entityChangeSet[$field]);
  65. }
  66. /**
  67. * Get the old value of the changeset of the changed field.
  68. *
  69. * @param string $field
  70. * @return mixed
  71. */
  72. public function getOldValue($field)
  73. {
  74. $this->assertValidField($field);
  75. return $this->entityChangeSet[$field][0];
  76. }
  77. /**
  78. * Get the new value of the changeset of the changed field.
  79. *
  80. * @param string $field
  81. * @return mixed
  82. */
  83. public function getNewValue($field)
  84. {
  85. $this->assertValidField($field);
  86. return $this->entityChangeSet[$field][1];
  87. }
  88. /**
  89. * Set the new value of this field.
  90. *
  91. * @param string $field
  92. * @param mixed $value
  93. */
  94. public function setNewValue($field, $value)
  95. {
  96. $this->assertValidField($field);
  97. $this->entityChangeSet[$field][1] = $value;
  98. }
  99. /**
  100. * Assert the field exists in changeset.
  101. *
  102. * @param string $field
  103. */
  104. private function assertValidField($field)
  105. {
  106. if ( ! isset($this->entityChangeSet[$field])) {
  107. throw new \InvalidArgumentException(sprintf(
  108. 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
  109. $field,
  110. get_class($this->getEntity())
  111. ));
  112. }
  113. }
  114. }