|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Date; |
|
4
|
|
|
|
|
5
|
|
|
// phpcs:disable |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Date\TimestampFieldInterface; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
12
|
|
|
use Symfony\Component\Validator\Constraints\DateTime; |
|
13
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
14
|
|
|
|
|
15
|
|
|
// phpcs:enable |
|
16
|
|
|
trait TimestampFieldTrait |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \DateTime|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $timestamp; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
26
|
|
|
* @param ClassMetadataBuilder $builder |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function metaForTimestamp(ClassMetadataBuilder $builder): void |
|
29
|
|
|
{ |
|
30
|
|
|
MappingHelper::setSimpleDatetimeFields( |
|
31
|
|
|
[TimestampFieldInterface::PROP_TIMESTAMP], |
|
32
|
|
|
$builder |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param ValidatorClassMetaData $metadata |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
|
40
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
|
41
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
|
42
|
|
|
*/ |
|
43
|
|
|
protected static function validatorMetaForTimestamp(ValidatorClassMetaData $metadata): void |
|
44
|
|
|
{ |
|
45
|
|
|
$metadata->addPropertyConstraint( |
|
46
|
|
|
TimestampFieldInterface::PROP_TIMESTAMP, |
|
47
|
|
|
new DateTime() |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \DateTime|null |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function getTimestamp(): ?\DateTime |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->timestamp; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param \DateTime|null $timestamp |
|
61
|
|
|
* |
|
62
|
|
|
* @return $this|TimestampFieldInterface |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function setTimestamp(?\DateTime $timestamp): self |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->timestamp = $timestamp; |
|
67
|
1 |
|
if ($this instanceof ValidatedEntityInterface) { |
|
68
|
1 |
|
$this->validateProperty(TimestampFieldInterface::PROP_TIMESTAMP); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|