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