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