1 | <?php |
||
8 | final class Address |
||
9 | { |
||
10 | private const PARSE_POSITION_START = 1; |
||
11 | |||
12 | private const PARSE_POSITION_QUOTE = 2; |
||
13 | |||
14 | private const PARSE_STATE_EMAIL = 1; |
||
15 | |||
16 | private const PARSE_STATE_TAGGED_EMAIL = 2; |
||
17 | |||
18 | /** |
||
19 | * @var EmailAddress |
||
20 | */ |
||
21 | private $address; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $name; |
||
27 | |||
28 | /** |
||
29 | * @param EmailAddress $address |
||
30 | * @param string $name |
||
31 | */ |
||
32 | 94 | public function __construct(EmailAddress $address, string $name = '') |
|
41 | |||
42 | /** |
||
43 | * @return EmailAddress |
||
44 | */ |
||
45 | 28 | public function getAddress(): EmailAddress |
|
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | 36 | public function getName(): string |
|
57 | |||
58 | /** |
||
59 | * @param Address $address |
||
60 | * @return bool |
||
61 | */ |
||
62 | 3 | public function equals(Address $address): bool |
|
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | 54 | public function __toString(): string |
|
71 | { |
||
72 | 54 | if ($this->name === '') { |
|
73 | 15 | return (string)$this->address; |
|
74 | } |
||
75 | |||
76 | 41 | $encodedPhrase = OptimalEncodedHeaderValue::forPhrase($this->name); |
|
77 | 41 | if ($encodedPhrase->getEncoding() === '7bit' || $encodedPhrase->getEncoding() === '8bit') { |
|
78 | 38 | $encodedName = \addslashes((string)$encodedPhrase); |
|
79 | |||
80 | 38 | if (\preg_match('/[^A-Za-z0-9\s!#$%&\'*+\/=?^_`{|}~\-]/', $this->name) === 1) { |
|
81 | 38 | $encodedName = \sprintf('"%s"', $encodedName); |
|
82 | } |
||
83 | } else { |
||
84 | 4 | $encodedName = (string)$encodedPhrase; |
|
85 | } |
||
86 | |||
87 | 41 | return \sprintf('%s <%s>', $encodedName, $this->address->getPunyCode()); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | 1 | public function toReadableString(): string |
|
106 | |||
107 | /** |
||
108 | * @param string $addressAsString |
||
109 | * @return Address |
||
110 | */ |
||
111 | 52 | public static function fromString(string $addressAsString) |
|
213 | } |
||
214 |