|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpEmail; |
|
6
|
|
|
|
|
7
|
|
|
class Address |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $email; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string|null |
|
16
|
|
|
*/ |
|
17
|
|
|
private $name; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Recipient constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $email |
|
23
|
|
|
* @param string|null $name |
|
24
|
|
|
* |
|
25
|
|
|
* @throws ValidationException |
|
26
|
|
|
*/ |
|
27
|
13 |
|
public function __construct(string $email, ?string $name = null) |
|
28
|
|
|
{ |
|
29
|
13 |
|
Validate::that() |
|
30
|
13 |
|
->isEmail('email', $email) |
|
31
|
13 |
|
->now(); |
|
32
|
|
|
|
|
33
|
8 |
|
$this->email = $email; |
|
34
|
8 |
|
$this->name = $name; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function getEmail(): string |
|
41
|
|
|
{ |
|
42
|
2 |
|
return $this->email; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string|null |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function getName(): ?string |
|
49
|
|
|
{ |
|
50
|
2 |
|
return $this->name; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a RFC 2822 compliant string version of the address. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function toRfc2822(): string |
|
59
|
|
|
{ |
|
60
|
2 |
|
if ($this->getName() !== null) { |
|
61
|
1 |
|
return sprintf('"%s" <%s>', $this->getName(), $this->getEmail()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $this->getEmail(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create an Address from a valid RFC 2822 compliant string. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $str |
|
71
|
|
|
* |
|
72
|
|
|
* @return Address |
|
73
|
|
|
*/ |
|
74
|
9 |
|
public static function fromRfc2822(string $str) |
|
75
|
|
|
{ |
|
76
|
9 |
|
$parts = explode(' ', $str); |
|
77
|
|
|
|
|
78
|
|
|
// Pop the email address off of the parts of the string |
|
79
|
9 |
|
$email = trim(array_pop($parts), '<>'); |
|
80
|
|
|
|
|
81
|
9 |
|
$name = null; |
|
82
|
|
|
|
|
83
|
|
|
// If there are more parts to the address string, combine them to use as the name |
|
84
|
9 |
|
if (!empty($parts)) { |
|
85
|
|
|
// Trim any extraneous quotes from the name |
|
86
|
5 |
|
$name = trim(implode(' ', $parts), '"'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
9 |
|
return new static($email, $name); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Create an Address from a string. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $str |
|
96
|
|
|
* |
|
97
|
|
|
* @return Address |
|
98
|
|
|
*/ |
|
99
|
3 |
|
public static function fromString(string $str) |
|
100
|
|
|
{ |
|
101
|
3 |
|
return self::fromRfc2822($str); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function __toString(): string |
|
108
|
|
|
{ |
|
109
|
2 |
|
return $this->toRfc2822(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|