PreUpdateEventArgs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Event;
  22. use Doctrine\Common\EventArgs,
  23. Doctrine\ORM\EntityManager;
  24. /**
  25. * Class that holds event arguments for a preInsert/preUpdate event.
  26. *
  27. * @author Guilherme Blanco <guilehrmeblanco@hotmail.com>
  28. * @author Roman Borschel <roman@code-factory.org>
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. * @since 2.0
  31. */
  32. class PreUpdateEventArgs extends LifecycleEventArgs
  33. {
  34. /**
  35. * @var array
  36. */
  37. private $entityChangeSet;
  38. /**
  39. * Constructor.
  40. *
  41. * @param object $entity
  42. * @param \Doctrine\ORM\EntityManager $em
  43. * @param array $changeSet
  44. */
  45. public function __construct($entity, EntityManager $em, array &$changeSet)
  46. {
  47. parent::__construct($entity, $em);
  48. $this->entityChangeSet = &$changeSet;
  49. }
  50. /**
  51. * Retrieve entity changeset.
  52. *
  53. * @return array
  54. */
  55. public function getEntityChangeSet()
  56. {
  57. return $this->entityChangeSet;
  58. }
  59. /**
  60. * Check if field has a changeset.
  61. *
  62. * @return boolean
  63. */
  64. public function hasChangedField($field)
  65. {
  66. return isset($this->entityChangeSet[$field]);
  67. }
  68. /**
  69. * Get the old value of the changeset of the changed field.
  70. *
  71. * @param string $field
  72. * @return mixed
  73. */
  74. public function getOldValue($field)
  75. {
  76. $this->assertValidField($field);
  77. return $this->entityChangeSet[$field][0];
  78. }
  79. /**
  80. * Get the new value of the changeset of the changed field.
  81. *
  82. * @param string $field
  83. * @return mixed
  84. */
  85. public function getNewValue($field)
  86. {
  87. $this->assertValidField($field);
  88. return $this->entityChangeSet[$field][1];
  89. }
  90. /**
  91. * Set the new value of this field.
  92. *
  93. * @param string $field
  94. * @param mixed $value
  95. */
  96. public function setNewValue($field, $value)
  97. {
  98. $this->assertValidField($field);
  99. $this->entityChangeSet[$field][1] = $value;
  100. }
  101. /**
  102. * Assert the field exists in changeset.
  103. *
  104. * @param string $field
  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. }