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\MappingHelper; |
10
|
|
|
use Symfony\Component\Validator\Constraints\Uuid; |
11
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait SettableUuidFieldTrait |
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
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String |
22
|
|
|
*/ |
23
|
|
|
trait SettableUuidFieldTrait |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string|null |
28
|
|
|
*/ |
29
|
|
|
private $settableUuid; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
33
|
|
|
* @param ClassMetadataBuilder $builder |
34
|
|
|
*/ |
35
|
|
|
public static function metaForSettableUuid(ClassMetadataBuilder $builder): void |
36
|
|
|
{ |
37
|
|
|
$fieldBuilder = new FieldBuilder( |
38
|
|
|
$builder, |
39
|
|
|
[ |
40
|
|
|
'fieldName' => SettableUuidFieldInterface::PROP_SETTABLE_UUID, |
41
|
|
|
'type' => Type::STRING, |
42
|
|
|
'default' => SettableUuidFieldInterface::DEFAULT_SETTABLE_UUID, |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
$fieldBuilder |
46
|
|
|
->columnName(MappingHelper::getColumnNameForField(SettableUuidFieldInterface::PROP_SETTABLE_UUID)) |
47
|
|
|
->nullable(true) |
48
|
|
|
->unique(true) |
49
|
|
|
->length(100) |
50
|
|
|
->build(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This method sets the validation for this field. |
55
|
|
|
* |
56
|
|
|
* You should add in as many relevant property constraints as you see fit. |
57
|
|
|
* |
58
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
59
|
|
|
* |
60
|
|
|
* @param ValidatorClassMetaData $metadata |
61
|
|
|
* |
62
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
63
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
64
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
65
|
|
|
*/ |
66
|
|
|
protected static function validatorMetaForPropertySettableUuid(ValidatorClassMetaData $metadata): void |
67
|
|
|
{ |
68
|
|
|
$metadata->addPropertyConstraint( |
69
|
|
|
SettableUuidFieldInterface::PROP_SETTABLE_UUID, |
70
|
|
|
new Uuid() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
|
|
public function getSettableUuid(): ?string |
78
|
|
|
{ |
79
|
|
|
if (null === $this->settableUuid) { |
80
|
|
|
return SettableUuidFieldInterface::DEFAULT_SETTABLE_UUID; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->settableUuid; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|null $settableUuid |
88
|
|
|
* |
89
|
|
|
* @return self |
90
|
|
|
*/ |
91
|
|
|
private function setSettableUuid(?string $settableUuid): self |
92
|
|
|
{ |
93
|
|
|
$this->updatePropertyValue( |
|
|
|
|
94
|
|
|
SettableUuidFieldInterface::PROP_SETTABLE_UUID, |
95
|
|
|
$settableUuid |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|