1 | <?php |
---|
2 | /* |
---|
3 | * $Id$ |
---|
4 | * |
---|
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
16 | * |
---|
17 | * This software consists of voluntary contributions made by many individuals |
---|
18 | * and is licensed under the LGPL. For more information, see |
---|
19 | * <http://www.doctrine-project.org>. |
---|
20 | */ |
---|
21 | |
---|
22 | namespace Doctrine\ORM\Event; |
---|
23 | |
---|
24 | use Doctrine\Common\EventArgs, |
---|
25 | Doctrine\ORM\EntityManager; |
---|
26 | |
---|
27 | /** |
---|
28 | * Class that holds event arguments for a preInsert/preUpdate event. |
---|
29 | * |
---|
30 | * @author Guilherme Blanco <guilehrmeblanco@hotmail.com> |
---|
31 | * @author Roman Borschel <roman@code-factory.org> |
---|
32 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
33 | * @since 2.0 |
---|
34 | */ |
---|
35 | class PreUpdateEventArgs extends LifecycleEventArgs |
---|
36 | { |
---|
37 | /** |
---|
38 | * @var array |
---|
39 | */ |
---|
40 | private $entityChangeSet; |
---|
41 | |
---|
42 | /** |
---|
43 | * Constructor. |
---|
44 | * |
---|
45 | * @param object $entity |
---|
46 | * @param \Doctrine\ORM\EntityManager $em |
---|
47 | * @param array $changeSet |
---|
48 | */ |
---|
49 | public function __construct($entity, EntityManager $em, array &$changeSet) |
---|
50 | { |
---|
51 | parent::__construct($entity, $em); |
---|
52 | |
---|
53 | $this->entityChangeSet = &$changeSet; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Retrieve entity changeset. |
---|
58 | * |
---|
59 | * @return array |
---|
60 | */ |
---|
61 | public function getEntityChangeSet() |
---|
62 | { |
---|
63 | return $this->entityChangeSet; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Check if field has a changeset. |
---|
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 | private function assertValidField($field) |
---|
121 | { |
---|
122 | if ( ! isset($this->entityChangeSet[$field])) { |
---|
123 | throw new \InvalidArgumentException(sprintf( |
---|
124 | 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.', |
---|
125 | $field, |
---|
126 | get_class($this->getEntity()) |
---|
127 | )); |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|