1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\Common\PropertyChangedListener; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait ImplementNotifyChangeTrackingPolicy |
16
|
|
|
* |
17
|
|
|
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/change-tracking-policies.html#notify |
18
|
|
|
* |
19
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Traits |
20
|
|
|
*/ |
21
|
|
|
trait ImplementNotifyChangeTrackingPolicy |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array PropertyChangedListener[] |
25
|
|
|
*/ |
26
|
|
|
private $notifyChangeTrackingListeners = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param PropertyChangedListener $listener |
30
|
|
|
*/ |
31
|
55 |
|
public function addPropertyChangedListener(PropertyChangedListener $listener): void |
32
|
|
|
{ |
33
|
55 |
|
$this->notifyChangeTrackingListeners[] = $listener; |
34
|
55 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The meta data is set to the entity when the meta data is loaded |
38
|
|
|
* |
39
|
|
|
* This call will load the meta data if it has not already been set, which will in turn set it against the Entity |
40
|
|
|
* |
41
|
|
|
* @param EntityManagerInterface $entityManager |
42
|
|
|
*/ |
43
|
18 |
|
public function ensureMetaDataIsSet(EntityManagerInterface $entityManager): void |
44
|
|
|
{ |
45
|
18 |
|
if (self::$metaData instanceof ClassMetadata) { |
46
|
9 |
|
return; |
47
|
|
|
} |
48
|
9 |
|
$entityManager->getClassMetadata(self::class); |
49
|
9 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This notifies the embeddable properties on the owning Entity |
53
|
|
|
* |
54
|
|
|
* @param string $embeddablePropertyName |
55
|
|
|
* @param null|string $propName |
56
|
|
|
* @param null $oldValue |
|
|
|
|
57
|
|
|
* @param null $newValue |
|
|
|
|
58
|
|
|
*/ |
59
|
4 |
|
public function notifyEmbeddablePrefixedProperties( |
60
|
|
|
string $embeddablePropertyName, |
61
|
|
|
?string $propName = null, |
62
|
|
|
$oldValue = null, |
63
|
|
|
$newValue = null |
64
|
|
|
): void { |
65
|
4 |
|
if ($oldValue !== null && $oldValue === $newValue) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
/** |
69
|
|
|
* @var ClassMetadata $metaData |
70
|
|
|
*/ |
71
|
4 |
|
$metaData = static::$metaData; |
72
|
4 |
|
foreach ($metaData->getFieldNames() as $fieldName) { |
73
|
4 |
|
if (true === \ts\stringStartsWith($fieldName, $embeddablePropertyName) |
74
|
4 |
|
&& false !== \ts\stringContains($fieldName, '.') |
75
|
|
|
) { |
76
|
4 |
|
if ($fieldName !== null && $fieldName !== "$embeddablePropertyName.$propName") { |
77
|
4 |
|
continue; |
78
|
|
|
} |
79
|
4 |
|
foreach ($this->notifyChangeTrackingListeners as $listener) { |
80
|
|
|
//wondering if we can get away with not passing in the values? |
81
|
4 |
|
$listener->propertyChanged($this, $fieldName, $oldValue, $newValue); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
4 |
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* To be called from all set methods |
90
|
|
|
* |
91
|
|
|
* This method updates the property value, then it runs this through validation |
92
|
|
|
* If validation fails, it sets the old value back and throws the caught exception |
93
|
|
|
* If validation passes, it then performs the Doctrine notification for property change |
94
|
|
|
* |
95
|
|
|
* @param string $propName |
96
|
|
|
* @param mixed $newValue |
97
|
|
|
* |
98
|
|
|
* @throws ValidationException |
99
|
|
|
*/ |
100
|
44 |
|
private function updatePropertyValueThenValidateAndNotify(string $propName, $newValue): void |
101
|
|
|
{ |
102
|
44 |
|
if ($this->$propName === $newValue) { |
103
|
2 |
|
return; |
104
|
|
|
} |
105
|
42 |
|
$oldValue = $this->$propName; |
106
|
42 |
|
$this->$propName = $newValue; |
107
|
42 |
|
if ($this instanceof ValidatedEntityInterface) { |
108
|
|
|
try { |
109
|
42 |
|
$this->validateProperty($propName); |
110
|
2 |
|
} catch (ValidationException $e) { |
111
|
2 |
|
$this->$propName = $oldValue; |
112
|
2 |
|
throw $e; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
40 |
|
foreach ($this->notifyChangeTrackingListeners as $listener) { |
117
|
6 |
|
$listener->propertyChanged($this, $propName, $oldValue, $newValue); |
118
|
|
|
} |
119
|
40 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Called from the Has___Entities Traits |
123
|
|
|
* |
124
|
|
|
* @param string $propName |
125
|
|
|
* @param Collection|EntityInterface[] $entities |
126
|
|
|
*/ |
127
|
|
|
private function setEntityCollectionAndNotify(string $propName, Collection $entities): void |
128
|
|
|
{ |
129
|
|
|
if ($this->$propName === $entities) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
$oldValue = $this->$propName; |
133
|
|
|
$this->$propName = $entities; |
134
|
|
|
foreach ($this->notifyChangeTrackingListeners as $listener) { |
135
|
|
|
$listener->propertyChanged($this, $propName, $oldValue, $entities); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Called from the Has___Entities Traits |
141
|
|
|
* |
142
|
|
|
* @param string $propName |
143
|
|
|
* @param EntityInterface $entity |
144
|
|
|
*/ |
145
|
1 |
|
private function addToEntityCollectionAndNotify(string $propName, EntityInterface $entity): void |
146
|
|
|
{ |
147
|
1 |
|
if ($this->$propName === null) { |
148
|
|
|
$this->$propName = new ArrayCollection(); |
149
|
|
|
} |
150
|
1 |
|
if ($this->$propName->contains($entity)) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
1 |
|
$oldValue = $this->$propName; |
154
|
1 |
|
$this->$propName->add($entity); |
155
|
1 |
|
$newValue = $this->$propName; |
156
|
1 |
|
foreach ($this->notifyChangeTrackingListeners as $listener) { |
157
|
1 |
|
$listener->propertyChanged($this, $propName, $oldValue, $newValue); |
158
|
|
|
} |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Called from the Has___Entities Traits |
163
|
|
|
* |
164
|
|
|
* @param string $propName |
165
|
|
|
* @param EntityInterface $entity |
166
|
|
|
*/ |
167
|
|
|
private function removeFromEntityCollectionAndNotify(string $propName, EntityInterface $entity): void |
168
|
|
|
{ |
169
|
|
|
if ($this->$propName === null) { |
170
|
|
|
$this->$propName = new ArrayCollection(); |
171
|
|
|
} |
172
|
|
|
if (!$this->$propName->contains($entity)) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
$oldValue = $this->$propName; |
176
|
|
|
$this->$propName->removeElement($entity); |
177
|
|
|
$newValue = $this->$propName; |
178
|
|
|
foreach ($this->notifyChangeTrackingListeners as $listener) { |
179
|
|
|
$listener->propertyChanged($this, $propName, $oldValue, $newValue); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Called from the Has___Entity Traits |
185
|
|
|
* |
186
|
|
|
* @param string $propName |
187
|
|
|
* @param EntityInterface|null $entity |
188
|
|
|
*/ |
189
|
1 |
|
private function setEntityAndNotify(string $propName, ?EntityInterface $entity): void |
190
|
|
|
{ |
191
|
1 |
|
if ($this->$propName === $entity) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
1 |
|
$oldValue = $this->$propName; |
195
|
1 |
|
$this->$propName = $entity; |
196
|
1 |
|
foreach ($this->notifyChangeTrackingListeners as $listener) { |
197
|
1 |
|
$listener->propertyChanged($this, $propName, $oldValue, $entity); |
198
|
|
|
} |
199
|
1 |
|
} |
200
|
|
|
} |
201
|
|
|
|