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 MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\Common\Persistence\Event;
21
22use Doctrine\Common\EventArgs,
23    Doctrine\Common\Persistence\ObjectManager;
24
25/**
26 * Class that holds event arguments for a preUpdate event.
27 *
28 * @author Guilherme Blanco <guilehrmeblanco@hotmail.com>
29 * @author Roman Borschel <roman@code-factory.org>
30 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 * @since  2.2
32 */
33class PreUpdateEventArgs extends LifecycleEventArgs
34{
35    /**
36     * @var array
37     */
38    private $entityChangeSet;
39
40    /**
41     * Constructor.
42     *
43     * @param object $entity
44     * @param ObjectManager $objectManager
45     * @param array $changeSet
46     */
47    public function __construct($entity, ObjectManager $objectManager, array &$changeSet)
48    {
49        parent::__construct($entity, $objectManager);
50
51        $this->entityChangeSet = &$changeSet;
52    }
53
54    /**
55     * Retrieve entity changeset.
56     *
57     * @return array
58     */
59    public function getEntityChangeSet()
60    {
61        return $this->entityChangeSet;
62    }
63
64    /**
65     * Check if field has a changeset.
66     *
67     * @param string $field
68     *
69     * @return boolean
70     */
71    public function hasChangedField($field)
72    {
73        return isset($this->entityChangeSet[$field]);
74    }
75
76    /**
77     * Get the old value of the changeset of the changed field.
78     *
79     * @param  string $field
80     * @return mixed
81     */
82    public function getOldValue($field)
83    {
84        $this->assertValidField($field);
85
86        return $this->entityChangeSet[$field][0];
87    }
88
89    /**
90     * Get the new value of the changeset of the changed field.
91     *
92     * @param  string $field
93     * @return mixed
94     */
95    public function getNewValue($field)
96    {
97        $this->assertValidField($field);
98
99        return $this->entityChangeSet[$field][1];
100    }
101
102    /**
103     * Set the new value of this field.
104     *
105     * @param string $field
106     * @param mixed $value
107     */
108    public function setNewValue($field, $value)
109    {
110        $this->assertValidField($field);
111
112        $this->entityChangeSet[$field][1] = $value;
113    }
114
115    /**
116     * Assert the field exists in changeset.
117     *
118     * @param string $field
119     *
120     * @throws \InvalidArgumentException
121     */
122    private function assertValidField($field)
123    {
124        if ( ! isset($this->entityChangeSet[$field])) {
125            throw new \InvalidArgumentException(sprintf(
126                'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
127                $field,
128                get_class($this->getEntity())
129            ));
130        }
131    }
132}
133
134