-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Address.php
140 lines (116 loc) · 4.44 KB
/
Address.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime;
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\MessageIDValidation;
use Egulias\EmailValidator\Validation\RFCValidation;
use Symfony\Component\Mime\Encoder\IdnAddressEncoder;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Exception\RfcComplianceException;
/**
* @author Fabien Potencier <[email protected]>
*/
final class Address
{
/**
* A regex that matches a structure like 'Name <[email protected]>'.
* It matches anything between the first < and last > as email address.
* This allows to use a single string to construct an Address, which can be convenient to use in
* config, and allows to have more readable config.
* This does not try to cover all edge cases for address.
*/
private const FROM_STRING_PATTERN = '~(?<displayName>[^<]*)<(?<addrSpec>.*)>[^>]*~';
private static EmailValidator $validator;
private static IdnAddressEncoder $encoder;
private string $address;
private string $name;
public function __construct(string $address, string $name = '')
{
if (!class_exists(EmailValidator::class)) {
throw new LogicException(\sprintf('The "%s" class cannot be used as it needs "%s". Try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
}
self::$validator ??= new EmailValidator();
$this->address = trim($address);
$this->name = trim(str_replace(["\n", "\r"], '', $name));
if (!self::$validator->isValid($this->address, class_exists(MessageIDValidation::class) ? new MessageIDValidation() : new RFCValidation())) {
throw new RfcComplianceException(\sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address));
}
}
public function getAddress(): string
{
return $this->address;
}
public function getName(): string
{
return $this->name;
}
public function getEncodedAddress(): string
{
self::$encoder ??= new IdnAddressEncoder();
return self::$encoder->encodeString($this->address);
}
public function toString(): string
{
return ($n = $this->getEncodedName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
}
public function getEncodedName(): string
{
if ('' === $this->getName()) {
return '';
}
return \sprintf('"%s"', preg_replace('/"/u', '\"', $this->getName()));
}
public static function create(self|string $address): self
{
if ($address instanceof self) {
return $address;
}
if (!str_contains($address, '<')) {
return new self($address);
}
if (!preg_match(self::FROM_STRING_PATTERN, $address, $matches)) {
throw new InvalidArgumentException(\sprintf('Could not parse "%s" to a "%s" instance.', $address, self::class));
}
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
}
/**
* @param array<Address|string> $addresses
*
* @return Address[]
*/
public static function createArray(array $addresses): array
{
$addrs = [];
foreach ($addresses as $address) {
$addrs[] = self::create($address);
}
return $addrs;
}
/**
* Returns true if this address' localpart contains at least one
* non-ASCII character, and false if it is only ASCII (or empty).
*
* This is a helper for Envelope, which has to decide whether to
* the SMTPUTF8 extensions (RFC 6530 and following) for any given
* message.
*
* The SMTPUTF8 extension is strictly required if any address
* contains a non-ASCII character in its localpart. If non-ASCII
* is only used in domains (e.g. horst@freiherr-von-mühlhausen.de)
* then it is possible to send the message using IDN encoding
* instead of SMTPUTF8. The most common software will display the
* message as intended.
*/
public function hasUnicodeLocalpart(): bool
{
return (bool) preg_match('/[\x80-\xFF].*@/', $this->address);
}
}