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