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\ShortIndexedRequiredStringFieldInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
10
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
11
|
|
|
use Symfony\Component\Validator\Constraints\NotEqualTo; |
12
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This trait provides a short (50 characters) string which is non uniquely indexed and is required |
16
|
|
|
* |
17
|
|
|
* Trait ShortIndexedRequiredStringFieldTrait |
18
|
|
|
* |
19
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String |
20
|
|
|
*/ |
21
|
|
|
trait ShortIndexedRequiredStringFieldTrait |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $shortIndexedRequiredString; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
31
|
|
|
* @param ClassMetadataBuilder $builder |
32
|
|
|
*/ |
33
|
4 |
|
public static function metaForShortIndexedRequiredString(ClassMetadataBuilder $builder): void |
34
|
|
|
{ |
35
|
4 |
|
$columnName = MappingHelper::getColumnNameForField( |
36
|
4 |
|
ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING |
37
|
|
|
); |
38
|
4 |
|
$fieldBuilder = new FieldBuilder( |
39
|
4 |
|
$builder, |
40
|
|
|
[ |
41
|
4 |
|
'fieldName' => ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
42
|
|
|
'type' => Type::STRING, |
43
|
|
|
'default' => ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING, |
44
|
|
|
] |
45
|
|
|
); |
46
|
|
|
$fieldBuilder |
47
|
4 |
|
->columnName($columnName) |
48
|
4 |
|
->nullable(false) |
49
|
4 |
|
->unique(false) |
50
|
4 |
|
->length(50) |
51
|
4 |
|
->build(); |
52
|
|
|
|
53
|
4 |
|
$builder->addIndex([$columnName], $columnName . '_idx'); |
54
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ValidatorClassMetaData $metadata |
58
|
|
|
*/ |
59
|
4 |
|
protected static function validatorMetaForPropertyShortIndexedRequiredString(ValidatorClassMetaData $metadata): void |
60
|
|
|
{ |
61
|
4 |
|
$metadata->addPropertyConstraints( |
62
|
4 |
|
ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
63
|
|
|
[ |
64
|
4 |
|
new NotEqualTo(ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING), |
65
|
4 |
|
new NotBlank(), |
66
|
|
|
] |
67
|
|
|
); |
68
|
4 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
4 |
|
public function getShortIndexedRequiredString(): string |
74
|
|
|
{ |
75
|
4 |
|
if (null === $this->shortIndexedRequiredString) { |
76
|
|
|
return ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
return $this->shortIndexedRequiredString; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string|null $shortIndexedRequiredString |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
4 |
|
private function setShortIndexedRequiredString(string $shortIndexedRequiredString): self |
88
|
|
|
{ |
89
|
4 |
|
$this->updatePropertyValue( |
|
|
|
|
90
|
4 |
|
ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
91
|
4 |
|
$shortIndexedRequiredString |
92
|
|
|
); |
93
|
|
|
|
94
|
4 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
private function initShortIndexedRequiredString(): void |
98
|
|
|
{ |
99
|
4 |
|
$this->shortIndexedRequiredString = |
100
|
|
|
ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING; |
101
|
4 |
|
} |
102
|
|
|
} |
103
|
|
|
|