PreUpdateEventArgs.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Doctrine\ORM\Event;
  3. use Doctrine\Common\EventArgs,
  4. Doctrine\ORM\EntityManager;
  5. /**
  6. * Class that holds event arguments for a preInsert/preUpdate event.
  7. *
  8. * @author Roman Borschel <roman@code-factory.org>
  9. * @author Benjamin Eberlei <kontakt@beberlei.de>
  10. * @since 2.0
  11. */
  12. class PreUpdateEventArgs extends LifecycleEventArgs
  13. {
  14. /**
  15. * @var array
  16. */
  17. private $_entityChangeSet;
  18. /**
  19. *
  20. * @param object $entity
  21. * @param EntityManager $em
  22. * @param array $changeSet
  23. */
  24. public function __construct($entity, $em, array &$changeSet)
  25. {
  26. parent::__construct($entity, $em);
  27. $this->_entityChangeSet = &$changeSet;
  28. }
  29. public function getEntityChangeSet()
  30. {
  31. return $this->_entityChangeSet;
  32. }
  33. /**
  34. * Field has a changeset?
  35. *
  36. * @return bool
  37. */
  38. public function hasChangedField($field)
  39. {
  40. return isset($this->_entityChangeSet[$field]);
  41. }
  42. /**
  43. * Get the old value of the changeset of the changed field.
  44. *
  45. * @param string $field
  46. * @return mixed
  47. */
  48. public function getOldValue($field)
  49. {
  50. $this->_assertValidField($field);
  51. return $this->_entityChangeSet[$field][0];
  52. }
  53. /**
  54. * Get the new value of the changeset of the changed field.
  55. *
  56. * @param string $field
  57. * @return mixed
  58. */
  59. public function getNewValue($field)
  60. {
  61. $this->_assertValidField($field);
  62. return $this->_entityChangeSet[$field][1];
  63. }
  64. /**
  65. * Set the new value of this field.
  66. *
  67. * @param string $field
  68. * @param mixed $value
  69. */
  70. public function setNewValue($field, $value)
  71. {
  72. $this->_assertValidField($field);
  73. $this->_entityChangeSet[$field][1] = $value;
  74. }
  75. private function _assertValidField($field)
  76. {
  77. if (!isset($this->_entityChangeSet[$field])) {
  78. throw new \InvalidArgumentException(
  79. "Field '".$field."' is not a valid field of the entity ".
  80. "'".get_class($this->getEntity())."' in PreInsertUpdateEventArgs."
  81. );
  82. }
  83. }
  84. }