-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractEntity.php
More file actions
106 lines (87 loc) · 2.55 KB
/
Copy pathAbstractEntity.php
File metadata and controls
106 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace Light\App\Entity;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Laminas\Stdlib\ArraySerializableInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use function is_array;
use function method_exists;
use function ucfirst;
#[ORM\MappedSuperclass]
abstract class AbstractEntity implements ArraySerializableInterface
{
#[ORM\Column(name: 'created', type: 'datetime_immutable', nullable: false)]
protected DateTimeImmutable $created;
#[ORM\Column(name: 'updated', type: 'datetime_immutable', nullable: true)]
protected ?DateTimeImmutable $updated = null;
#[ORM\Id]
#[ORM\Column(name: 'id', type: 'uuid', unique: true, nullable: false)]
protected UuidInterface $uuid;
#[ORM\PrePersist]
public function created(): void
{
$this->created = new DateTimeImmutable();
}
#[ORM\PreUpdate]
public function touch(): void
{
$this->updated = new DateTimeImmutable();
}
public function __construct()
{
$this->uuid = Uuid::uuid7();
}
public function getId(): UuidInterface
{
return $this->uuid;
}
public function setId(UuidInterface $id): static
{
$this->uuid = $id;
return $this;
}
public function getCreated(): ?DateTimeImmutable
{
return $this->created;
}
public function getCreatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): string
{
return $this->created->format($dateFormat);
}
public function getUpdated(): ?DateTimeImmutable
{
return $this->updated;
}
public function getUpdatedFormatted(string $dateFormat = 'Y-m-d H:i:s'): ?string
{
if ($this->updated instanceof DateTimeImmutable) {
return $this->updated->format($dateFormat);
}
return null;
}
/**
* @param array<non-empty-string, mixed> $array
*/
public function exchangeArray(array $array): void
{
foreach ($array as $property => $values) {
if (is_array($values)) {
$method = 'add' . ucfirst($property);
if (! method_exists($this, $method)) {
continue;
}
foreach ($values as $value) {
$this->$method($value);
}
} else {
$method = 'set' . ucfirst($property);
if (! method_exists($this, $method)) {
continue;
}
$this->$method($values);
}
}
}
}