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