|
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\MappingHelper; |
|
8
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
|
9
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
10
|
|
|
|
|
11
|
|
|
trait EmailAddressFieldTrait |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string|null |
|
16
|
|
|
*/ |
|
17
|
|
|
private $emailAddress; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
21
|
|
|
* @param ClassMetadataBuilder $builder |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function metaForEmailAddress(ClassMetadataBuilder $builder): void |
|
24
|
|
|
{ |
|
25
|
|
|
MappingHelper::setSimpleStringFields( |
|
26
|
|
|
[EmailAddressFieldInterface::PROP_EMAIL_ADDRESS], |
|
27
|
|
|
$builder, |
|
28
|
|
|
EmailAddressFieldInterface::DEFAULT_EMAIL_ADDRESS, |
|
29
|
|
|
true |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* This method sets the validation for this field. |
|
35
|
|
|
* |
|
36
|
|
|
* You should add in as many relevant property constraints as you see fit. |
|
37
|
|
|
* |
|
38
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
|
39
|
|
|
* |
|
40
|
|
|
* @param ValidatorClassMetaData $metadata |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
|
43
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
|
44
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
|
45
|
|
|
*/ |
|
46
|
|
|
protected static function validatorMetaForPropertyEmailAddress(ValidatorClassMetaData $metadata): void |
|
47
|
|
|
{ |
|
48
|
|
|
$metadata->addPropertyConstraint( |
|
49
|
|
|
EmailAddressFieldInterface::PROP_EMAIL_ADDRESS, |
|
50
|
|
|
new Email() |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getEmailAddress(): ?string |
|
58
|
|
|
{ |
|
59
|
|
|
if (null === $this->emailAddress) { |
|
60
|
|
|
return EmailAddressFieldInterface::DEFAULT_EMAIL_ADDRESS; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->emailAddress; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string|null $emailAddress |
|
68
|
|
|
* |
|
69
|
|
|
* @return self |
|
70
|
|
|
*/ |
|
71
|
|
|
private function setEmailAddress(?string $emailAddress): self |
|
72
|
|
|
{ |
|
73
|
|
|
$this->updatePropertyValue( |
|
|
|
|
|
|
74
|
|
|
EmailAddressFieldInterface::PROP_EMAIL_ADDRESS, |
|
75
|
|
|
$emailAddress |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|