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 LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\Common\Persistence\Event; |
---|
21 | |
---|
22 | use 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 | */ |
---|
33 | class 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 | * @return boolean |
---|
68 | */ |
---|
69 | public function hasChangedField($field) |
---|
70 | { |
---|
71 | return isset($this->entityChangeSet[$field]); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Get the old value of the changeset of the changed field. |
---|
76 | * |
---|
77 | * @param string $field |
---|
78 | * @return mixed |
---|
79 | */ |
---|
80 | public function getOldValue($field) |
---|
81 | { |
---|
82 | $this->assertValidField($field); |
---|
83 | |
---|
84 | return $this->entityChangeSet[$field][0]; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Get the new value of the changeset of the changed field. |
---|
89 | * |
---|
90 | * @param string $field |
---|
91 | * @return mixed |
---|
92 | */ |
---|
93 | public function getNewValue($field) |
---|
94 | { |
---|
95 | $this->assertValidField($field); |
---|
96 | |
---|
97 | return $this->entityChangeSet[$field][1]; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Set the new value of this field. |
---|
102 | * |
---|
103 | * @param string $field |
---|
104 | * @param mixed $value |
---|
105 | */ |
---|
106 | public function setNewValue($field, $value) |
---|
107 | { |
---|
108 | $this->assertValidField($field); |
---|
109 | |
---|
110 | $this->entityChangeSet[$field][1] = $value; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Assert the field exists in changeset. |
---|
115 | * |
---|
116 | * @param string $field |
---|
117 | */ |
---|
118 | private function assertValidField($field) |
---|
119 | { |
---|
120 | if ( ! isset($this->entityChangeSet[$field])) { |
---|
121 | throw new \InvalidArgumentException(sprintf( |
---|
122 | 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.', |
---|
123 | $field, |
---|
124 | get_class($this->getEntity()) |
---|
125 | )); |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|