|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bludata\Doctrine\ODM\MongoDB\Entities; |
|
4
|
|
|
|
|
5
|
|
|
use Bludata\Doctrine\Common\Interfaces\BaseEntityInterface; |
|
6
|
|
|
use Bludata\Doctrine\Common\Interfaces\EntityManagerInterface; |
|
7
|
|
|
use Bludata\Doctrine\Common\Interfaces\EntityTimestampInterface; |
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
9
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
|
10
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @ODM\MappedSuperclass |
|
14
|
|
|
* @ODM\HasLifecycleCallbacks |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class BaseEntity implements BaseEntityInterface, EntityTimestampInterface |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @ODM\Id |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $id; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \DateTime |
|
25
|
|
|
* |
|
26
|
|
|
* @ODM\Field(type="timestamp", name="createdAt") |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $createdAt; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \DateTime |
|
32
|
|
|
* |
|
33
|
|
|
* @ODM\Field(type="timestamp", name="updatedAt") |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $updatedAt; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @ODM\Field(type="timestamp", nullable=true, name="deletedAt") |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $deletedAt; |
|
41
|
|
|
|
|
42
|
|
|
public function getCreatedAt() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->createdAt; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function getUpdatedAt() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->updatedAt; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getDeletedAt() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return $this->deletedAt; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function setDeletedAt($deletedAt) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->deletedAt = $deletedAt; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Altera o campo updatedAt para forçar o persist da entity. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function forcePersist() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->updatedAt = new \DateTime(); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @ODM\PrePersist |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function prePersist() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$repository = $this->getRepository(); |
|
78
|
|
|
|
|
79
|
|
|
if (method_exists($repository, 'preSave')) { |
|
80
|
|
|
$repository->preSave($this); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (method_exists($repository, 'preSave')) { |
|
84
|
|
|
$repository->validate($this); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @ODM\PostPersist |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function postPersist() |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$repository = $this->getRepository(); |
|
94
|
|
|
|
|
95
|
|
|
if (method_exists($repository, 'postSave')) { |
|
96
|
|
|
$repository->postSave($this); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (method_exists($repository, 'postSave')) { |
|
100
|
|
|
$repository->validate($this); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @ODM\PreUpdate |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
public function preUpdate() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$repository = $this->getRepository(); |
|
110
|
|
|
|
|
111
|
|
|
if (method_exists($repository, 'preSave')) { |
|
112
|
|
|
$repository->preSave($this); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (method_exists($repository, 'preSave')) { |
|
116
|
|
|
$repository->validate($this); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @ODM\PostUpdate |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function postUpdate() |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$repository = $this->getRepository(); |
|
126
|
|
|
|
|
127
|
|
|
if (method_exists($repository, 'postSave')) { |
|
128
|
|
|
$repository->postSave($this); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (method_exists($repository, 'postSave')) { |
|
132
|
|
|
$repository->validate($this); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getRepository() |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
return app(EntityManagerInterface::class)->getRepository(get_class($this)); |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function save($flush = false) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->getRepository()->save($this); |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function remove() |
|
149
|
|
|
{ |
|
150
|
|
|
$this->getRepository()->remove($this); |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function flush($all = true) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->getRepository()->flush($all ? null : $this); |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function getId() |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
return $this->id; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
protected function getFillable() |
|
168
|
|
|
{ |
|
169
|
|
|
return ['id', 'createdAt', 'updatedAt', 'deletedAt']; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
|
|
|
|
|
|
174
|
|
|
* É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
|
175
|
|
|
* Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
|
|
|
|
|
|
176
|
|
|
* |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
abstract public function getOnlyStore(); |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
|
|
|
|
|
|
183
|
|
|
* Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
|
184
|
|
|
* Este método pode ser sobrescrito nas classes filhas. |
|
185
|
|
|
* É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
|
186
|
|
|
* Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
|
|
|
|
|
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getOnlyUpdate() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->getOnlyStore(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function setPropertiesEntity(array $data) |
|
196
|
|
|
{ |
|
197
|
|
|
foreach ($data as $key => $value) { |
|
198
|
|
|
$set = true; |
|
199
|
|
|
|
|
200
|
|
View Code Duplication |
if ( |
|
|
|
|
|
|
201
|
|
|
((!isset($data['id']) || !is_numeric($data['id'])) && !in_array($key, $this->getOnlyStore())) |
|
|
|
|
|
|
202
|
|
|
|| |
|
203
|
|
|
(isset($data['id']) && is_numeric($data['id']) && !in_array($key, $this->getOnlyUpdate())) |
|
|
|
|
|
|
204
|
|
|
) { |
|
205
|
|
|
$set = false; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$method = 'set'.ucfirst($key); |
|
209
|
|
|
|
|
210
|
|
|
if (method_exists($this, $method) && $set) { |
|
211
|
|
|
$this->$method($value); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
View Code Duplication |
final protected function checkOnyExceptInArray($key, array $options = null) |
|
|
|
|
|
|
219
|
|
|
{ |
|
220
|
|
|
if ( |
|
221
|
|
|
$options |
|
222
|
|
|
&& |
|
223
|
|
|
( |
|
224
|
|
|
(isset($options['only']) && is_array($options['only']) && !in_array($key, $options['only'])) |
|
|
|
|
|
|
225
|
|
|
|| |
|
226
|
|
|
(isset($options['except']) && is_array($options['except']) && in_array($key, $options['except'])) |
|
|
|
|
|
|
227
|
|
|
) |
|
228
|
|
|
) { |
|
229
|
|
|
return false; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return true; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function toArray(array $options = null) |
|
236
|
|
|
{ |
|
237
|
|
|
$classMetadata = $this->getRepository()->getClassMetadata(); |
|
238
|
|
|
$array = []; |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
foreach ($this->getFillable() as $key) { |
|
241
|
|
|
if ($this->checkOnyExceptInArray($key, $options)) { |
|
242
|
|
|
if (is_object($this->$key) && $this->$key instanceof \DateTime || $this->$key instanceof \MongoTimestamp) { |
|
|
|
|
|
|
243
|
|
|
$metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null; |
|
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
if ($this->$key) { |
|
246
|
|
|
$dateFormat = 'Y-m-d H:i:s'; |
|
247
|
|
|
|
|
248
|
|
|
if ($this->$key instanceof \MongoTimestamp) { |
|
249
|
|
|
$timestamp = new \DateTime(); |
|
250
|
|
|
$timestamp->setTimestamp($this->$key->sec); |
|
251
|
|
|
$this->$key = $timestamp; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
$array[$key] = $this->$key->format($dateFormat); |
|
255
|
|
|
} |
|
256
|
|
View Code Duplication |
} elseif (is_object($this->$key) && $this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) { |
|
|
|
|
|
|
257
|
|
|
$ids = []; |
|
258
|
|
|
foreach ($this->$key->getValues() as $item) { |
|
259
|
|
|
$ids[] = $item->getId(); |
|
260
|
|
|
} |
|
261
|
|
|
$array[$key] = $ids; |
|
262
|
|
|
} elseif (method_exists($this->$key, 'getId')) { |
|
263
|
|
|
$array[$key] = $this->$key->getId(); |
|
264
|
|
|
} else { |
|
265
|
|
|
$array[$key] = $this->$key; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $array; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.