|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
|
4
|
|
|
|
|
5
|
|
|
// phpcs:disable |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
|
8
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
9
|
|
|
use Doctrine\ORM\Mapping\Builder\FieldBuilder; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\UniqueStringFieldInterface; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
12
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
|
13
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
14
|
|
|
|
|
15
|
|
|
// phpcs:enable |
|
16
|
|
|
trait UniqueStringFieldTrait |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $uniqueString; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ClassMetadataBuilder $builder |
|
26
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function metaForUniqueString(ClassMetadataBuilder $builder): void |
|
29
|
|
|
{ |
|
30
|
|
|
$fieldBuilder = new FieldBuilder( |
|
31
|
|
|
$builder, |
|
32
|
|
|
[ |
|
33
|
|
|
'fieldName' => UniqueStringFieldInterface::PROP_UNIQUE_STRING, |
|
34
|
|
|
'type' => Type::STRING, |
|
35
|
|
|
'default' => UniqueStringFieldInterface::DEFAULT_UNIQUE_STRING, |
|
36
|
|
|
] |
|
37
|
|
|
); |
|
38
|
|
|
$fieldBuilder |
|
39
|
|
|
->columnName(MappingHelper::getColumnNameForField(UniqueStringFieldInterface::PROP_UNIQUE_STRING)) |
|
40
|
|
|
->nullable(false) |
|
41
|
|
|
->unique(true) |
|
42
|
|
|
->length(UniqueStringFieldInterface::LENGTH_UNIQUE_STRING) |
|
43
|
|
|
->build(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string|null |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getUniqueString(): ?string |
|
50
|
|
|
{ |
|
51
|
|
|
if (null === $this->uniqueString) { |
|
52
|
|
|
return UniqueStringFieldInterface::DEFAULT_UNIQUE_STRING; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->uniqueString; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static function validatorMetaForPropertyUniqueString(ValidatorClassMetaData $metadata): void |
|
62
|
|
|
{ |
|
63
|
|
|
$metadata->addPropertyConstraint( |
|
64
|
|
|
UniqueStringFieldInterface::PROP_UNIQUE_STRING, |
|
65
|
|
|
new Length(['min' => 0, 'max' => UniqueStringFieldInterface::LENGTH_UNIQUE_STRING]) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string|null $uniqueString |
|
71
|
|
|
* |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
|
|
private function setUniqueString(?string $uniqueString): self |
|
75
|
|
|
{ |
|
76
|
|
|
$this->updatePropertyValue( |
|
|
|
|
|
|
77
|
|
|
UniqueStringFieldInterface::PROP_UNIQUE_STRING, |
|
78
|
|
|
$uniqueString |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|