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