1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\Binary; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
6
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\Binary\BinaryUuidFieldInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
9
|
|
|
use Ramsey\Uuid\UuidInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait BinaryUuidFieldTrait |
13
|
|
|
* |
14
|
|
|
* This field allows you to set a UUID that is generated elsewhere than the database. |
15
|
|
|
* This is as opposed to using a UUID primary key which is generated by the database |
16
|
|
|
* - eg |
17
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\UuidFieldTrait |
18
|
|
|
*/ |
19
|
|
|
trait BinaryUuidFieldTrait |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var UuidInterface|null |
24
|
|
|
*/ |
25
|
|
|
private $binaryUuid; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
29
|
|
|
* @param ClassMetadataBuilder $builder |
30
|
|
|
*/ |
31
|
|
|
public static function metaForBinaryUuid(ClassMetadataBuilder $builder): void |
32
|
|
|
{ |
33
|
|
|
$columnName = MappingHelper::getColumnNameForField( |
34
|
|
|
BinaryUuidFieldInterface::PROP_BINARY_UUID |
35
|
|
|
); |
36
|
|
|
$fieldBuilder = new FieldBuilder( |
37
|
|
|
$builder, |
38
|
|
|
[ |
39
|
|
|
'fieldName' => BinaryUuidFieldInterface::PROP_BINARY_UUID, |
40
|
|
|
'type' => MappingHelper::TYPE_UUID, |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
$fieldBuilder->columnName($columnName) |
44
|
|
|
->nullable(true) |
45
|
|
|
->unique(false) |
46
|
|
|
->build(); |
47
|
|
|
$builder->addIndex([$columnName], $columnName . '_idx'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return UuidInterface|null |
52
|
|
|
*/ |
53
|
|
|
public function getBinaryUuid(): ?UuidInterface |
54
|
|
|
{ |
55
|
|
|
return $this->binaryUuid; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param UuidInterface $binaryUuid |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
private function setBinaryUuid(?UuidInterface $binaryUuid): self |
64
|
|
|
{ |
65
|
|
|
$this->updatePropertyValue( |
|
|
|
|
66
|
|
|
BinaryUuidFieldInterface::PROP_BINARY_UUID, |
67
|
|
|
$binaryUuid |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|