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