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\LocaleIdentifierFieldInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
12
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Locale; |
14
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
15
|
|
|
|
16
|
|
|
// phpcs:enable |
17
|
|
|
trait LocaleIdentifierFieldTrait |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
private $localeIdentifier; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
27
|
|
|
* @param ClassMetadataBuilder $builder |
28
|
|
|
*/ |
29
|
|
|
public static function metaForLocaleIdentifier(ClassMetadataBuilder $builder): void |
30
|
|
|
{ |
31
|
|
|
$fieldBuilder = new FieldBuilder( |
32
|
|
|
$builder, |
33
|
|
|
[ |
34
|
|
|
'fieldName' => LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
35
|
|
|
'type' => Type::STRING, |
36
|
|
|
'default' => LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER, |
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
$fieldBuilder |
40
|
|
|
->columnName( |
41
|
|
|
MappingHelper::getColumnNameForField(LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER) |
42
|
|
|
) |
43
|
|
|
->nullable(true) |
44
|
|
|
->unique(false) |
45
|
|
|
->length(50) |
46
|
|
|
->build(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* This method sets the validation for this field. |
51
|
|
|
* |
52
|
|
|
* You should add in as many relevant property constraints as you see fit. |
53
|
|
|
* |
54
|
|
|
* @param ValidatorClassMetaData $metadata |
55
|
|
|
* |
56
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
57
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
58
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
59
|
|
|
*/ |
60
|
|
|
protected static function validatorMetaForPropertyLocaleIdentifier(ValidatorClassMetaData $metadata): void |
61
|
|
|
{ |
62
|
|
|
$metadata->addPropertyConstraints( |
63
|
|
|
LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
64
|
|
|
[ |
65
|
|
|
new Locale(['canonicalize' => true]), |
66
|
|
|
new Length( |
67
|
|
|
[ |
68
|
|
|
'min' => 0, |
69
|
|
|
'max' => 50, |
70
|
|
|
] |
71
|
|
|
) |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string|null |
78
|
|
|
*/ |
79
|
|
|
public function getLocaleIdentifier(): ?string |
80
|
|
|
{ |
81
|
|
|
if (null === $this->localeIdentifier) { |
82
|
|
|
return LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->localeIdentifier; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string|null $localeIdentifier |
90
|
|
|
* |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
private function setLocaleIdentifier(?string $localeIdentifier): self |
94
|
|
|
{ |
95
|
|
|
$this->updatePropertyValue( |
|
|
|
|
96
|
|
|
LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
97
|
|
|
$localeIdentifier |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|