Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common\Persistence\Event; Chris@0: Chris@0: use Doctrine\Common\Persistence\ObjectManager; Chris@0: Chris@0: /** Chris@0: * Class that holds event arguments for a preUpdate event. Chris@0: * Chris@0: * @author Guilherme Blanco Chris@0: * @author Roman Borschel Chris@0: * @author Benjamin Eberlei Chris@0: * @since 2.2 Chris@0: */ Chris@0: class PreUpdateEventArgs extends LifecycleEventArgs Chris@0: { Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $entityChangeSet; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param object $entity Chris@0: * @param ObjectManager $objectManager Chris@0: * @param array $changeSet Chris@0: */ Chris@0: public function __construct($entity, ObjectManager $objectManager, array &$changeSet) Chris@0: { Chris@0: parent::__construct($entity, $objectManager); Chris@0: Chris@0: $this->entityChangeSet = &$changeSet; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves the entity changeset. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getEntityChangeSet() Chris@0: { Chris@0: return $this->entityChangeSet; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if field has a changeset. Chris@0: * Chris@0: * @param string $field Chris@0: * Chris@0: * @return boolean Chris@0: */ Chris@0: public function hasChangedField($field) Chris@0: { Chris@0: return isset($this->entityChangeSet[$field]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the old value of the changeset of the changed field. Chris@0: * Chris@0: * @param string $field Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: public function getOldValue($field) Chris@0: { Chris@0: $this->assertValidField($field); Chris@0: Chris@0: return $this->entityChangeSet[$field][0]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the new value of the changeset of the changed field. Chris@0: * Chris@0: * @param string $field Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: public function getNewValue($field) Chris@0: { Chris@0: $this->assertValidField($field); Chris@0: Chris@0: return $this->entityChangeSet[$field][1]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the new value of this field. Chris@0: * Chris@0: * @param string $field Chris@0: * @param mixed $value Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function setNewValue($field, $value) Chris@0: { Chris@0: $this->assertValidField($field); Chris@0: Chris@0: $this->entityChangeSet[$field][1] = $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts the field exists in changeset. Chris@0: * Chris@0: * @param string $field Chris@0: * Chris@0: * @return void Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: private function assertValidField($field) Chris@0: { Chris@0: if ( ! isset($this->entityChangeSet[$field])) { Chris@0: throw new \InvalidArgumentException(sprintf( Chris@0: 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.', Chris@0: $field, Chris@12: get_class($this->getObject()) Chris@0: )); Chris@0: } Chris@0: } Chris@0: }