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