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