PreUpdateEventArgs.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Doctrine\Persistence\Event;
  3. use Doctrine\Persistence\ObjectManager;
  4. use InvalidArgumentException;
  5. use function get_class;
  6. use function sprintf;
  7. /**
  8. * Class that holds event arguments for a preUpdate event.
  9. */
  10. class PreUpdateEventArgs extends LifecycleEventArgs
  11. {
  12. /** @var mixed[][] */
  13. private $entityChangeSet;
  14. /**
  15. * @param object $entity
  16. * @param mixed[][] $changeSet
  17. */
  18. public function __construct($entity, ObjectManager $objectManager, array &$changeSet)
  19. {
  20. parent::__construct($entity, $objectManager);
  21. $this->entityChangeSet = &$changeSet;
  22. }
  23. /**
  24. * Retrieves the entity changeset.
  25. *
  26. * @return mixed[][]
  27. */
  28. public function getEntityChangeSet()
  29. {
  30. return $this->entityChangeSet;
  31. }
  32. /**
  33. * Checks if field has a changeset.
  34. *
  35. * @param string $field
  36. *
  37. * @return bool
  38. */
  39. public function hasChangedField($field)
  40. {
  41. return isset($this->entityChangeSet[$field]);
  42. }
  43. /**
  44. * Gets the old value of the changeset of the changed field.
  45. *
  46. * @param string $field
  47. *
  48. * @return mixed
  49. */
  50. public function getOldValue($field)
  51. {
  52. $this->assertValidField($field);
  53. return $this->entityChangeSet[$field][0];
  54. }
  55. /**
  56. * Gets the new value of the changeset of the changed field.
  57. *
  58. * @param string $field
  59. *
  60. * @return mixed
  61. */
  62. public function getNewValue($field)
  63. {
  64. $this->assertValidField($field);
  65. return $this->entityChangeSet[$field][1];
  66. }
  67. /**
  68. * Sets the new value of this field.
  69. *
  70. * @param string $field
  71. * @param mixed $value
  72. *
  73. * @return void
  74. */
  75. public function setNewValue($field, $value)
  76. {
  77. $this->assertValidField($field);
  78. $this->entityChangeSet[$field][1] = $value;
  79. }
  80. /**
  81. * Asserts the field exists in changeset.
  82. *
  83. * @param string $field
  84. *
  85. * @return void
  86. *
  87. * @throws InvalidArgumentException
  88. */
  89. private function assertValidField($field)
  90. {
  91. if (! isset($this->entityChangeSet[$field])) {
  92. throw new InvalidArgumentException(sprintf(
  93. 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
  94. $field,
  95. get_class($this->getObject())
  96. ));
  97. }
  98. }
  99. }