|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under The MIT License |
|
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
11
|
|
|
* @link https://github.com/Phauthentic |
|
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace Phauthentic\Email; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Email Address |
|
20
|
|
|
*/ |
|
21
|
|
|
class EmailAddress implements EmailAddressInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Receiver Name |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $name = ''; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Email |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $email = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritDoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(string $email, string $name = '') |
|
41
|
|
|
{ |
|
42
|
|
|
$this->setEmail($email); |
|
43
|
|
|
$this->setName($name); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritDoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setEmail(string $email, bool $validate = true): EmailAddressInterface |
|
50
|
|
|
{ |
|
51
|
|
|
if ($validate === true && !$this->validateEmailAddress($email)) { |
|
52
|
|
|
throw new InvalidArgumentException(sprintf( |
|
53
|
|
|
'The given email address `%s` is invalid', |
|
54
|
|
|
$email |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->email = $email; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setName(string $name): EmailAddressInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$this->name = $name; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritDoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getEmail(): string { |
|
77
|
|
|
return $this->email; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritDoc |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getName(): string |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->name; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritDoc |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __toString(): string |
|
92
|
|
|
{ |
|
93
|
|
|
if (empty($this->name)) { |
|
94
|
|
|
return $this->email; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->email . ' <' . $this->name . '>'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function toArray(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return [$this->getEmail() => $this->getName()]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Validates the email address by regex |
|
110
|
|
|
* |
|
111
|
|
|
* @link http://emailregex.com/ |
|
112
|
|
|
* @param string $email Email address |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function validateEmailAddress(string $email): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return (bool)preg_match('/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD', $email); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|