1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\TimeStamp; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\ORM\Events; |
8
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
9
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\TimeStamp\UpdatedAtTimestampFieldInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait TimestampFieldTrait |
16
|
|
|
* |
17
|
|
|
* An Immutable creation timestamp. It is null until it is saved (and reloaded) |
18
|
|
|
* |
19
|
|
|
* Notice the use of a lifecyle event to handle setting the pre persist creation timestamp |
20
|
|
|
* |
21
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\DateTime |
22
|
|
|
*/ |
23
|
|
|
trait UpdatedAtTimestampFieldTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var DateTimeImmutable|null |
27
|
|
|
*/ |
28
|
|
|
private $updatedAtTimestamp; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
32
|
|
|
* @param ClassMetadataBuilder $builder |
33
|
|
|
*/ |
34
|
6 |
|
public static function metaForUpdatedAtTimestamp(ClassMetadataBuilder $builder): void |
35
|
|
|
{ |
36
|
6 |
|
$builder->addLifecycleEvent('prePersistUpdatedAtTimestamp', Events::preUpdate); |
37
|
6 |
|
$builder->addLifecycleEvent('prePersistUpdatedAtTimestamp', Events::prePersist); |
38
|
6 |
|
$fieldBuilder = new FieldBuilder( |
39
|
6 |
|
$builder, |
40
|
|
|
[ |
41
|
6 |
|
'fieldName' => UpdatedAtTimestampFieldInterface::PROP_UPDATED_AT_TIMESTAMP, |
42
|
|
|
'type' => Type::DATETIME_IMMUTABLE, |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
$fieldBuilder |
46
|
6 |
|
->columnName(MappingHelper::getColumnNameForField( |
47
|
6 |
|
UpdatedAtTimestampFieldInterface::PROP_UPDATED_AT_TIMESTAMP |
48
|
|
|
)) |
49
|
6 |
|
->nullable(false) |
50
|
6 |
|
->build(); |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws Exception |
55
|
|
|
*/ |
56
|
6 |
|
public function prePersistUpdatedAtTimestamp(): void |
57
|
|
|
{ |
58
|
6 |
|
$this->updatePropertyValue( |
|
|
|
|
59
|
6 |
|
UpdatedAtTimestampFieldInterface::PROP_UPDATED_AT_TIMESTAMP, |
60
|
6 |
|
new DateTimeImmutable() |
61
|
|
|
); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return DateTimeImmutable|null |
66
|
|
|
*/ |
67
|
6 |
|
public function getUpdatedAtTimestamp(): ?DateTimeImmutable |
68
|
|
|
{ |
69
|
6 |
|
return $this->updatedAtTimestamp; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|