edmondscommerce /
doctrine-static-meta
| 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 | public static function metaForLocaleIdentifier(ClassMetadataBuilder $builder): void |
||
| 29 | { |
||
| 30 | $fieldBuilder = new FieldBuilder( |
||
| 31 | $builder, |
||
| 32 | [ |
||
| 33 | 'fieldName' => LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
||
| 34 | 'type' => Type::STRING, |
||
| 35 | 'default' => LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER, |
||
| 36 | ] |
||
| 37 | ); |
||
| 38 | $fieldBuilder |
||
| 39 | ->columnName( |
||
| 40 | MappingHelper::getColumnNameForField(LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER) |
||
| 41 | ) |
||
| 42 | ->nullable(true) |
||
| 43 | ->unique(false) |
||
| 44 | ->length(50) |
||
| 45 | ->build(); |
||
| 46 | } |
||
| 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 | protected static function validatorMetaForPropertyLocaleIdentifier(ValidatorClassMetaData $metadata): void |
||
| 60 | { |
||
| 61 | $metadata->addPropertyConstraint( |
||
| 62 | LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
||
| 63 | new Locale(['canonicalize' => true]) |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return string|null |
||
| 69 | */ |
||
| 70 | public function getLocaleIdentifier(): ?string |
||
| 71 | { |
||
| 72 | if (null === $this->localeIdentifier) { |
||
| 73 | return LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER; |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->localeIdentifier; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string|null $localeIdentifier |
||
| 81 | * |
||
| 82 | * @return self |
||
| 83 | */ |
||
| 84 | private function setLocaleIdentifier(?string $localeIdentifier): self |
||
| 85 | { |
||
| 86 | $this->updatePropertyValue( |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 87 | LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
||
| 88 | $localeIdentifier |
||
| 89 | ); |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | } |
||
| 94 |