1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\DateTime; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
8
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\DateTime\DateTimeSettableOnceFieldInterface; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait DateTimeSettableOnceFieldTrait |
15
|
|
|
* |
16
|
|
|
* This field is a dateTime that will be null until you set it. Once set (and possibly saved) it can not be updated |
17
|
|
|
* |
18
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\DateTime |
19
|
|
|
*/ |
20
|
|
|
trait DateTimeSettableOnceFieldTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DateTimeImmutable|null |
25
|
|
|
*/ |
26
|
|
|
private $dateTimeSettableOnce; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
30
|
|
|
* @param ClassMetadataBuilder $builder |
31
|
|
|
*/ |
32
|
|
|
public static function metaForDateTimeSettableOnce(ClassMetadataBuilder $builder): void |
33
|
|
|
{ |
34
|
|
|
$fieldBuilder = new FieldBuilder( |
35
|
|
|
$builder, |
36
|
|
|
[ |
37
|
|
|
'fieldName' => DateTimeSettableOnceFieldInterface::PROP_DATE_TIME_SETTABLE_ONCE, |
38
|
|
|
'type' => Type::DATETIME_IMMUTABLE, |
39
|
|
|
] |
40
|
|
|
); |
41
|
|
|
$fieldBuilder |
42
|
|
|
->columnName(MappingHelper::getColumnNameForField( |
43
|
|
|
DateTimeSettableOnceFieldInterface::PROP_DATE_TIME_SETTABLE_ONCE |
44
|
|
|
)) |
45
|
|
|
->nullable(DateTimeSettableOnceFieldInterface::DEFAULT_DATE_TIME_SETTABLE_ONCE === null) |
46
|
|
|
->build(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return DateTimeImmutable|null |
51
|
|
|
*/ |
52
|
|
|
public function getDateTimeSettableOnce(): ?DateTimeImmutable |
53
|
|
|
{ |
54
|
|
|
return $this->dateTimeSettableOnce; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param DateTimeImmutable|null $dateTimeSettableOnce |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
|
|
private function setDateTimeSettableOnce(?DateTimeImmutable $dateTimeSettableOnce): self |
63
|
|
|
{ |
64
|
|
|
if (null === $dateTimeSettableOnce) { |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
if (null !== $this->dateTimeSettableOnce) { |
68
|
|
|
throw new RuntimeException( |
69
|
|
|
DateTimeSettableOnceFieldInterface::PROP_DATE_TIME_SETTABLE_ONCE |
70
|
|
|
. ' is already set, you can not overwrite this with a new dateTime' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
$this->updatePropertyValue( |
|
|
|
|
74
|
|
|
DateTimeSettableOnceFieldInterface::PROP_DATE_TIME_SETTABLE_ONCE, |
75
|
|
|
$dateTimeSettableOnce |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|