1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Event; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class that holds event arguments for a preInsert/preUpdate event. |
11
|
|
|
*/ |
12
|
|
|
class PreUpdateEventArgs extends LifecycleEventArgs |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var mixed[] |
16
|
|
|
*/ |
17
|
|
|
private $entityChangeSet; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
* |
22
|
|
|
* @param object $entity |
23
|
|
|
* @param mixed[] $changeSet |
24
|
|
|
*/ |
25
|
11 |
|
public function __construct($entity, EntityManagerInterface $em, array &$changeSet) |
26
|
|
|
{ |
27
|
11 |
|
parent::__construct($entity, $em); |
28
|
|
|
|
29
|
11 |
|
$this->entityChangeSet = &$changeSet; |
30
|
11 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Retrieves entity changeset. |
34
|
|
|
* |
35
|
|
|
* @return mixed[] |
36
|
|
|
*/ |
37
|
|
|
public function getEntityChangeSet() |
38
|
|
|
{ |
39
|
|
|
return $this->entityChangeSet; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Checks if field has a changeset. |
44
|
|
|
* |
45
|
|
|
* @param string $field |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function hasChangedField($field) |
50
|
|
|
{ |
51
|
|
|
return isset($this->entityChangeSet[$field]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets the old value of the changeset of the changed field. |
56
|
|
|
* |
57
|
|
|
* @param string $field |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getOldValue($field) |
62
|
|
|
{ |
63
|
|
|
$this->assertValidField($field); |
64
|
|
|
|
65
|
|
|
return $this->entityChangeSet[$field][0]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the new value of the changeset of the changed field. |
70
|
|
|
* |
71
|
|
|
* @param string $field |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function getNewValue($field) |
76
|
|
|
{ |
77
|
|
|
$this->assertValidField($field); |
78
|
|
|
|
79
|
|
|
return $this->entityChangeSet[$field][1]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the new value of this field. |
84
|
|
|
* |
85
|
|
|
* @param string $field |
86
|
|
|
* @param mixed $value |
87
|
|
|
*/ |
88
|
|
|
public function setNewValue($field, $value) |
89
|
|
|
{ |
90
|
|
|
$this->assertValidField($field); |
91
|
|
|
|
92
|
|
|
$this->entityChangeSet[$field][1] = $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Asserts the field exists in changeset. |
97
|
|
|
* |
98
|
|
|
* @param string $field |
99
|
|
|
* |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
|
private function assertValidField($field) |
103
|
|
|
{ |
104
|
|
|
if (! isset($this->entityChangeSet[$field])) { |
105
|
|
|
throw new \InvalidArgumentException(sprintf( |
106
|
|
|
'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.', |
107
|
|
|
$field, |
108
|
|
|
get_class($this->getEntity()) |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|