edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
||
| 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\String\SettableUuidFieldInterface; |
||
| 9 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 10 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 11 | use Symfony\Component\Validator\Constraints\Uuid; |
||
| 12 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Trait SettableUuidFieldTrait |
||
| 16 | * |
||
| 17 | * This field allows you to set a UUID that is generated elsewhere than the database. |
||
| 18 | * This is as opposed to using a UUID primary key which is generated by the database |
||
| 19 | * - eg |
||
| 20 | * \EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\UuidFieldTrait |
||
| 21 | * |
||
| 22 | * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String |
||
| 23 | */ |
||
| 24 | trait SettableUuidFieldTrait |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string|null |
||
| 29 | */ |
||
| 30 | private $settableUuid; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 34 | * @param ClassMetadataBuilder $builder |
||
| 35 | */ |
||
| 36 | 1 | public static function metaForSettableUuid(ClassMetadataBuilder $builder): void |
|
| 37 | { |
||
| 38 | 1 | $fieldBuilder = new FieldBuilder( |
|
| 39 | 1 | $builder, |
|
| 40 | [ |
||
| 41 | 1 | 'fieldName' => SettableUuidFieldInterface::PROP_SETTABLE_UUID, |
|
| 42 | 'type' => Type::STRING, |
||
| 43 | 'default' => SettableUuidFieldInterface::DEFAULT_SETTABLE_UUID, |
||
| 44 | ] |
||
| 45 | ); |
||
| 46 | $fieldBuilder |
||
| 47 | 1 | ->columnName(MappingHelper::getColumnNameForField(SettableUuidFieldInterface::PROP_SETTABLE_UUID)) |
|
| 48 | 1 | ->nullable(null === SettableUuidFieldInterface::DEFAULT_SETTABLE_UUID) |
|
| 49 | 1 | ->unique(true) |
|
| 50 | 1 | ->length(100) |
|
| 51 | 1 | ->build(); |
|
| 52 | 1 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * This method sets the validation for this field. |
||
| 56 | * |
||
| 57 | * You should add in as many relevant property constraints as you see fit. |
||
| 58 | * |
||
| 59 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 60 | * |
||
| 61 | * @param ValidatorClassMetaData $metadata |
||
| 62 | * |
||
| 63 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 64 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 65 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 66 | */ |
||
| 67 | 2 | protected static function validatorMetaForSettableUuid(ValidatorClassMetaData $metadata) |
|
| 68 | { |
||
| 69 | 2 | $metadata->addPropertyConstraint( |
|
| 70 | 2 | SettableUuidFieldInterface::PROP_SETTABLE_UUID, |
|
| 71 | 2 | new Uuid() |
|
| 72 | ); |
||
| 73 | 2 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @return string|null |
||
| 77 | */ |
||
| 78 | 2 | public function getSettableUuid(): ?string |
|
| 79 | { |
||
| 80 | 2 | if (null === $this->settableUuid) { |
|
| 81 | 1 | return SettableUuidFieldInterface::DEFAULT_SETTABLE_UUID; |
|
| 82 | } |
||
| 83 | |||
| 84 | 2 | return $this->settableUuid; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string|null $settableUuid |
||
| 89 | * |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | 2 | public function setSettableUuid(?string $settableUuid): self |
|
| 93 | { |
||
| 94 | 2 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 95 | 2 | SettableUuidFieldInterface::PROP_SETTABLE_UUID, |
|
| 96 | 2 | $settableUuid |
|
| 97 | ); |
||
| 98 | |||
| 99 | 2 | return $this; |
|
| 100 | } |
||
| 101 | } |
||
| 102 |