edmondscommerce /
doctrine-static-meta
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
||
| 4 | |||
| 5 | use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
||
| 6 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\EmailAddressFieldInterface; |
||
| 7 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\ValidatedEntityInterface; |
||
| 8 | use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
||
| 9 | use Symfony\Component\Validator\Constraints\Email; |
||
| 10 | use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
||
| 11 | |||
| 12 | trait EmailAddressFieldTrait |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string|null |
||
| 17 | */ |
||
| 18 | private $emailAddress; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 22 | * @param ClassMetadataBuilder $builder |
||
| 23 | */ |
||
| 24 | 1 | public static function metaForEmailAddress(ClassMetadataBuilder $builder): void |
|
| 25 | { |
||
| 26 | 1 | MappingHelper::setSimpleStringFields( |
|
| 27 | 1 | [EmailAddressFieldInterface::PROP_EMAIL_ADDRESS], |
|
| 28 | 1 | $builder, |
|
| 29 | 1 | EmailAddressFieldInterface::DEFAULT_EMAIL_ADDRESS, |
|
| 30 | 1 | false |
|
| 31 | ); |
||
| 32 | 1 | } |
|
| 33 | |||
| 34 | /** |
||
| 35 | * This method sets the validation for this field. |
||
| 36 | * |
||
| 37 | * You should add in as many relevant property constraints as you see fit. |
||
| 38 | * |
||
| 39 | * @see https://symfony.com/doc/current/validation.html#supported-constraints |
||
| 40 | * |
||
| 41 | * @param ValidatorClassMetaData $metadata |
||
| 42 | * |
||
| 43 | * @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
||
| 44 | * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
||
| 45 | * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
||
| 46 | */ |
||
| 47 | 3 | protected static function validatorMetaForEmailAddress(ValidatorClassMetaData $metadata) |
|
| 48 | { |
||
| 49 | 3 | $metadata->addPropertyConstraint( |
|
| 50 | 3 | EmailAddressFieldInterface::PROP_EMAIL_ADDRESS, |
|
| 51 | 3 | new Email() |
|
| 52 | ); |
||
| 53 | 3 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @return string|null |
||
| 57 | */ |
||
| 58 | 3 | public function getEmailAddress(): ?string |
|
| 59 | { |
||
| 60 | 3 | if (null === $this->emailAddress) { |
|
| 61 | 1 | return EmailAddressFieldInterface::DEFAULT_EMAIL_ADDRESS; |
|
| 62 | } |
||
| 63 | |||
| 64 | 3 | return $this->emailAddress; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string|null $emailAddress |
||
| 69 | * |
||
| 70 | * @return self |
||
| 71 | */ |
||
| 72 | 3 | public function setEmailAddress(?string $emailAddress): self |
|
| 73 | { |
||
| 74 | 3 | $this->updatePropertyValueThenValidateAndNotify( |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 75 | 3 | EmailAddressFieldInterface::PROP_EMAIL_ADDRESS, |
|
| 76 | 3 | $emailAddress |
|
| 77 | ); |
||
| 78 | |||
| 79 | 3 | return $this; |
|
| 80 | } |
||
| 81 | } |
||
| 82 |