44
55namespace Light \App \Entity ;
66
7- use DateTimeImmutable ;
87use Doctrine \ORM \Mapping as ORM ;
98use Laminas \Stdlib \ArraySerializableInterface ;
10- use Ramsey \Uuid \Uuid ;
11- use Ramsey \Uuid \UuidInterface ;
129
1310use function is_array ;
1411use function method_exists ;
1512use function ucfirst ;
1613
1714#[ORM \MappedSuperclass]
18- abstract class AbstractEntity implements ArraySerializableInterface
15+ abstract class AbstractEntity implements ArraySerializableInterface, EntityInterface
1916{
20- #[ORM \Id]
21- #[ORM \Column(name: 'id ' , type: 'uuid ' , unique: true , nullable: false )]
22- protected UuidInterface $ id ;
23-
24- #[ORM \Column(name: 'created ' , type: 'datetime_immutable ' , nullable: false )]
25- protected DateTimeImmutable $ created ;
26-
27- #[ORM \Column(name: 'updated ' , type: 'datetime_immutable ' , nullable: true )]
28- protected ?DateTimeImmutable $ updated = null ;
29-
3017 public function __construct ()
3118 {
32- $ this ->id = Uuid::uuid7 ();
33- }
34-
35- public function getId (): UuidInterface
36- {
37- return $ this ->id ;
38- }
39-
40- public function setId (UuidInterface $ id ): static
41- {
42- $ this ->id = $ id ;
43-
44- return $ this ;
45- }
46-
47- public function getCreated (): ?DateTimeImmutable
48- {
49- return $ this ->created ;
19+ $ this ->initId ();
5020 }
5121
52- public function getCreatedFormatted ( string $ dateFormat = ' Y-m-d H:i:s ' ): string
22+ protected function initId ( ): void
5323 {
54- return $ this ->created ->format ($ dateFormat );
5524 }
5625
57- public function getUpdated (): ?DateTimeImmutable
58- {
59- return $ this ->updated ;
60- }
61-
62- public function getUpdatedFormatted (string $ dateFormat = 'Y-m-d H:i:s ' ): ?string
63- {
64- if ($ this ->updated instanceof DateTimeImmutable) {
65- return $ this ->updated ->format ($ dateFormat );
66- }
67-
68- return null ;
69- }
70-
71- #[ORM \PrePersist]
72- public function created (): void
73- {
74- $ this ->created = new DateTimeImmutable ();
75- }
76-
77- #[ORM \PreUpdate]
78- public function touch (): void
26+ /**
27+ * Override this method in soft-deletable entities
28+ */
29+ public function isDeleted (): bool
7930 {
80- $ this -> updated = new DateTimeImmutable () ;
31+ return false ;
8132 }
8233
8334 /**
@@ -103,4 +54,4 @@ public function exchangeArray(array $array): void
10354 }
10455 }
10556 }
106- }
57+ }
0 commit comments