Completed
Pull Request — master (#6365)
by Daniel Tome
10:44
created

PreUpdateEventArgs::getOldValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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
20
namespace Doctrine\ORM\Event;
21
22
use Doctrine\ORM\EntityManagerInterface;
23
24
/**
25
 * Class that holds event arguments for a preInsert/preUpdate event.
26
 *
27
 * @author Guilherme Blanco <[email protected]>
28
 * @author Roman Borschel <[email protected]>
29
 * @author Benjamin Eberlei <[email protected]>
30
 * @since  2.0
31
 */
32
class PreUpdateEventArgs extends LifecycleEventArgs
33
{
34
    /**
35
     * @var array
36
     */
37
    private $entityChangeSet;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param object                 $entity
43
     * @param EntityManagerInterface $em
44
     * @param array                  $changeSet
45
     */
46
    public function __construct($entity, EntityManagerInterface $em, array &$changeSet)
47
    {
48
        parent::__construct($entity, $em);
49
50
        $this->entityChangeSet = &$changeSet;
51
    }
52
53
    /**
54
     * Retrieves entity changeset.
55
     *
56
     * @return array
57
     */
58
    public function getEntityChangeSet()
59
    {
60
        return $this->entityChangeSet;
61
    }
62
63
    /**
64
     * Checks if field has a changeset.
65
     *
66
     * @param string $field
67
     *
68
     * @return boolean
69
     */
70
    public function hasChangedField($field)
71
    {
72
        return isset($this->entityChangeSet[$field]);
73
    }
74
75
    /**
76
     * Gets the old value of the changeset of the changed field.
77
     *
78
     * @param string $field
79
     *
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
     * Gets the new value of the changeset of the changed field.
91
     *
92
     * @param string $field
93
     *
94
     * @return mixed
95
     */
96
    public function getNewValue($field)
97
    {
98
        $this->assertValidField($field);
99
100
        return $this->entityChangeSet[$field][1];
101
    }
102
103
    /**
104
     * Sets the new value of this field.
105
     *
106
     * @param string $field
107
     * @param mixed  $value
108
     *
109
     * @return void
110
     */
111
    public function setNewValue($field, $value)
112
    {
113
        $this->assertValidField($field);
114
115
        $this->entityChangeSet[$field][1] = $value;
116
    }
117
118
    /**
119
     * Asserts the field exists in changeset.
120
     *
121
     * @param string $field
122
     *
123
     * @return void
124
     *
125
     * @throws \InvalidArgumentException
126
     */
127
    private function assertValidField($field)
128
    {
129
        if ( ! isset($this->entityChangeSet[$field])) {
130
            throw new \InvalidArgumentException(sprintf(
131
                'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.',
132
                $field,
133
                get_class($this->getEntity())
134
            ));
135
        }
136
    }
137
}
138