|
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
|
4 |
|
public static function metaForShortIndexedRequiredString(ClassMetadataBuilder $builder): void |
|
35
|
|
|
{ |
|
36
|
4 |
|
$columnName = MappingHelper::getColumnNameForField( |
|
37
|
4 |
|
ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING |
|
38
|
|
|
); |
|
39
|
4 |
|
$fieldBuilder = new FieldBuilder( |
|
40
|
4 |
|
$builder, |
|
41
|
|
|
[ |
|
42
|
4 |
|
'fieldName' => ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
|
43
|
|
|
'type' => Type::STRING, |
|
44
|
|
|
'default' => ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING, |
|
45
|
|
|
] |
|
46
|
|
|
); |
|
47
|
|
|
$fieldBuilder |
|
48
|
4 |
|
->columnName($columnName) |
|
49
|
4 |
|
->nullable(false) |
|
50
|
4 |
|
->unique(false) |
|
51
|
4 |
|
->length(ShortIndexedRequiredStringFieldInterface::LENGTH_SHORT_INDEXED_REQUIRED_STRING) |
|
52
|
4 |
|
->build(); |
|
53
|
|
|
|
|
54
|
4 |
|
$builder->addIndex([$columnName], $columnName . '_idx'); |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function getShortIndexedRequiredString(): string |
|
61
|
|
|
{ |
|
62
|
4 |
|
if (null === $this->shortIndexedRequiredString) { |
|
63
|
|
|
return ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
return $this->shortIndexedRequiredString; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ValidatorClassMetaData $metadata |
|
71
|
|
|
*/ |
|
72
|
4 |
|
protected static function validatorMetaForPropertyShortIndexedRequiredString(ValidatorClassMetaData $metadata): void |
|
73
|
|
|
{ |
|
74
|
4 |
|
$metadata->addPropertyConstraints( |
|
75
|
4 |
|
ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
|
76
|
|
|
[ |
|
77
|
4 |
|
new NotEqualTo(ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING), |
|
78
|
4 |
|
new NotBlank(), |
|
79
|
4 |
|
new Length( |
|
80
|
|
|
[ |
|
81
|
4 |
|
'min' => 1, |
|
82
|
|
|
'max' => ShortIndexedRequiredStringFieldInterface::LENGTH_SHORT_INDEXED_REQUIRED_STRING, |
|
83
|
|
|
] |
|
84
|
|
|
), |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
4 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string|null $shortIndexedRequiredString |
|
91
|
|
|
* |
|
92
|
|
|
* @return self |
|
93
|
|
|
*/ |
|
94
|
4 |
|
private function setShortIndexedRequiredString(string $shortIndexedRequiredString): self |
|
95
|
|
|
{ |
|
96
|
4 |
|
$this->updatePropertyValue( |
|
|
|
|
|
|
97
|
4 |
|
ShortIndexedRequiredStringFieldInterface::PROP_SHORT_INDEXED_REQUIRED_STRING, |
|
98
|
4 |
|
$shortIndexedRequiredString |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
4 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
private function initShortIndexedRequiredString(): void |
|
105
|
|
|
{ |
|
106
|
4 |
|
$this->shortIndexedRequiredString = |
|
107
|
|
|
ShortIndexedRequiredStringFieldInterface::DEFAULT_SHORT_INDEXED_REQUIRED_STRING; |
|
108
|
4 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|