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
|
|
|
public function prePersist() |
76
|
|
|
{ |
77
|
|
|
$this->getRepository() |
78
|
|
|
->preSave($this) |
79
|
|
|
->validate($this); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @ODM\PreUpdate |
84
|
|
|
*/ |
85
|
|
|
public function preUpdate() |
86
|
|
|
{ |
87
|
|
|
$this->getRepository() |
88
|
|
|
->preSave($this) |
89
|
|
|
->validate($this); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getRepository() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return app(EntityManagerInterface::class)->getRepository(get_class($this)); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function save($flush = false) |
98
|
|
|
{ |
99
|
|
|
$this->getRepository()->save($this); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function remove() |
105
|
|
|
{ |
106
|
|
|
$this->getRepository()->remove($this); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function flush($all = true) |
112
|
|
|
{ |
113
|
|
|
$this->getRepository()->flush($all ? null : $this); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getId() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return $this->id; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getFillable() |
124
|
|
|
{ |
125
|
|
|
return ['id', 'createdAt', 'updatedAt', 'deletedAt']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retona um array com o nome das propriedade que o cliente pode setar para realizar o store |
|
|
|
|
130
|
|
|
* É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
131
|
|
|
* Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
|
|
|
|
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
abstract public function getOnlyStore(); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Retona um array com o nome das propriedade que o cliente pode setar para realizar o update. |
|
|
|
|
139
|
|
|
* Por padrão retorna os mesmos valores de $this->getOnlyStore(). |
140
|
|
|
* Este método pode ser sobrescrito nas classes filhas. |
141
|
|
|
* É usado principalmente em $this->setPropertiesEntity e nos Controllers. |
142
|
|
|
* Este método não evita que uma propriedade seja alterada caso tenha seu método set(). |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getOnlyUpdate() |
147
|
|
|
{ |
148
|
|
|
return $this->getOnlyStore(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function setPropertiesEntity(array $data) |
152
|
|
|
{ |
153
|
|
|
foreach ($data as $key => $value) { |
154
|
|
|
$set = true; |
155
|
|
|
|
156
|
|
View Code Duplication |
if ( |
|
|
|
|
157
|
|
|
((!isset($data['id']) || !is_numeric($data['id'])) && !in_array($key, $this->getOnlyStore())) |
|
|
|
|
158
|
|
|
|| |
159
|
|
|
(isset($data['id']) && is_numeric($data['id']) && !in_array($key, $this->getOnlyUpdate())) |
|
|
|
|
160
|
|
|
) { |
161
|
|
|
$set = false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$method = 'set'.ucfirst($key); |
165
|
|
|
|
166
|
|
|
if (method_exists($this, $method) && $set) { |
167
|
|
|
$this->$method($value); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
final protected function checkOnyExceptInArray($key, array $options = null) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
if ( |
177
|
|
|
$options |
178
|
|
|
&& |
179
|
|
|
( |
180
|
|
|
(isset($options['only']) && is_array($options['only']) && !in_array($key, $options['only'])) |
|
|
|
|
181
|
|
|
|| |
182
|
|
|
(isset($options['except']) && is_array($options['except']) && in_array($key, $options['except'])) |
|
|
|
|
183
|
|
|
) |
184
|
|
|
) { |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function toArray(array $options = null) |
192
|
|
|
{ |
193
|
|
|
$classMetadata = $this->getRepository()->getClassMetadata(); |
194
|
|
|
$array = []; |
|
|
|
|
195
|
|
|
|
196
|
|
|
foreach ($this->getFillable() as $key) { |
197
|
|
|
if ($this->checkOnyExceptInArray($key, $options)) { |
198
|
|
|
if (is_object($this->$key)) { |
199
|
|
View Code Duplication |
if ($this->$key instanceof \DateTime) { |
|
|
|
|
200
|
|
|
$metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null; |
|
|
|
|
201
|
|
|
|
202
|
|
|
if ($this->$key) { |
203
|
|
|
$dateFormat = 'Y-m-d H:i:s'; |
204
|
|
|
|
205
|
|
|
if ($metaDataKey) { |
206
|
|
|
switch ($metaDataKey['type']) { |
207
|
|
|
case 'date': |
208
|
|
|
$dateFormat = 'Y-m-d'; |
209
|
|
|
break; |
210
|
|
|
|
211
|
|
|
case 'time': |
212
|
|
|
$dateFormat = 'H:i:s'; |
213
|
|
|
break; |
214
|
|
|
|
215
|
|
|
default: |
216
|
|
|
break; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
$array[$key] = $this->$key->format($dateFormat); |
220
|
|
|
} |
221
|
|
|
} elseif ($this->$key instanceof \MongoTimestamp) { |
222
|
|
|
$array[$key] = $this->$key; |
223
|
|
|
} elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) { |
|
|
|
|
224
|
|
|
$ids = []; |
225
|
|
|
foreach ($this->$key->getValues() as $item) { |
226
|
|
|
$ids[] = $item->getId(); |
227
|
|
|
} |
228
|
|
|
$array[$key] = $ids; |
229
|
|
|
} else { |
230
|
|
|
$array[$key] = $this->$key->getId(); |
231
|
|
|
} |
232
|
|
|
} else { |
233
|
|
|
$array[$key] = $this->$key; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $array; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.