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