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