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\Entity\Interfaces\ValidatedEntityInterface; |
||
| 12 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 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 | 1 | public static function metaForLocaleIdentifier(ClassMetadataBuilder $builder): void |
|
| 29 | { |
||
| 30 | 1 | $fieldBuilder = new FieldBuilder( |
|
| 31 | 1 | $builder, |
|
| 32 | [ |
||
| 33 | 1 | 'fieldName' => LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
|
| 34 | 'type' => Type::STRING, |
||
| 35 | 'default' => LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER, |
||
| 36 | ] |
||
| 37 | ); |
||
| 38 | $fieldBuilder |
||
| 39 | 1 | ->columnName( |
|
| 40 | 1 | MappingHelper::getColumnNameForField(LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER) |
|
| 41 | ) |
||
| 42 | 1 | ->nullable(LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER === null) |
|
| 43 | 1 | ->unique(false) |
|
| 44 | 1 | ->length(50) |
|
| 45 | 1 | ->build(); |
|
| 46 | 1 | } |
|
| 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 | * Remove the PHPMD suppressed warning once you start setting constraints |
||
| 54 | * |
||
| 55 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 56 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 57 | * |
||
| 58 | * @param ValidatorClassMetaData $metadata |
||
| 59 | * |
||
| 60 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 61 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 62 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 63 | */ |
||
| 64 | 2 | protected static function validatorMetaForLocaleIdentifier(ValidatorClassMetaData $metadata) |
|
| 65 | { |
||
| 66 | // $metadata->addPropertyConstraint( |
||
| 67 | // LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
||
| 68 | // new NotBlank() |
||
| 69 | // ); |
||
| 70 | 2 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return string|null |
||
| 74 | */ |
||
| 75 | 2 | public function getLocaleIdentifier(): ?string |
|
| 76 | { |
||
| 77 | 2 | if (null === $this->localeIdentifier) { |
|
| 78 | 1 | return LocaleIdentifierFieldInterface::DEFAULT_LOCALE_IDENTIFIER; |
|
| 79 | } |
||
| 80 | |||
| 81 | 2 | return $this->localeIdentifier; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string|null $localeIdentifier |
||
| 86 | * |
||
| 87 | * @return self |
||
| 88 | */ |
||
| 89 | 2 | public function setLocaleIdentifier(?string $localeIdentifier): self |
|
| 90 | { |
||
| 91 | 2 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 92 | 2 | LocaleIdentifierFieldInterface::PROP_LOCALE_IDENTIFIER, |
|
| 93 | 2 | $localeIdentifier |
|
| 94 | ); |
||
| 95 | |||
| 96 | 2 | return $this; |
|
| 97 | } |
||
| 98 | } |
||
| 99 |