edmondscommerce /
doctrine-static-meta
| 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 | public static function metaForUnicodeLanguageIdentifier(ClassMetadataBuilder $builder): void |
||
| 26 | { |
||
| 27 | $fieldBuilder = new FieldBuilder( |
||
| 28 | $builder, |
||
| 29 | [ |
||
| 30 | 'fieldName' => UnicodeLanguageIdentifierFieldInterface::PROP_UNICODE_LANGUAGE_IDENTIFIER, |
||
| 31 | 'type' => Type::STRING, |
||
| 32 | 'default' => null, |
||
| 33 | ] |
||
| 34 | ); |
||
| 35 | $fieldBuilder |
||
| 36 | ->columnName(MappingHelper::getColumnNameForField( |
||
| 37 | UnicodeLanguageIdentifierFieldInterface::PROP_UNICODE_LANGUAGE_IDENTIFIER |
||
| 38 | )) |
||
| 39 | ->nullable(true) |
||
| 40 | ->unique(false) |
||
| 41 | ->length(50) |
||
| 42 | ->build(); |
||
| 43 | } |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Validates the property is a Unicode Language Identifier |
||
| 48 | * |
||
| 49 | * @param ValidatorClassMetaData $metadata |
||
| 50 | */ |
||
| 51 | protected static function validatorMetaForPropertyUnicodeLanguageIdentifier(ValidatorClassMetaData $metadata): void |
||
| 52 | { |
||
| 53 | $metadata->addPropertyConstraint( |
||
| 54 | UnicodeLanguageIdentifierFieldInterface::PROP_UNICODE_LANGUAGE_IDENTIFIER, |
||
| 55 | new Language() |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return string|null |
||
| 61 | */ |
||
| 62 | public function getUnicodeLanguageIdentifier(): ?string |
||
| 63 | { |
||
| 64 | if (null === $this->unicodeLanguageIdentifier) { |
||
| 65 | return UnicodeLanguageIdentifierFieldInterface::DEFAULT_UNICODE_LANGUAGE_IDENTIFIER; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $this->unicodeLanguageIdentifier; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string|null $unicodeLanguageIdentifier |
||
| 73 | * |
||
| 74 | * @return self |
||
| 75 | */ |
||
| 76 | private function setUnicodeLanguageIdentifier(?string $unicodeLanguageIdentifier): self |
||
| 77 | { |
||
| 78 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 79 | UnicodeLanguageIdentifierFieldInterface::PROP_UNICODE_LANGUAGE_IDENTIFIER, |
||
| 80 | $unicodeLanguageIdentifier |
||
| 81 | ); |
||
| 82 | |||
| 83 | return $this; |
||
| 84 | } |
||
| 85 | } |
||
| 86 |