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