Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap\Message; |
| 6 | |
| 7 | /** |
| 8 | * An e-mail address. |
| 9 | */ |
| 10 | final class EmailAddress |
| 11 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 12 | private string $mailbox; |
| 13 | private ?string $hostname; |
| 14 | private ?string $name; |
| 15 | private ?string $address; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 16 | |
| 17 | public function __construct(string $mailbox, string $hostname = null, string $name = null) |
| 18 | { |
| 19 | $this->mailbox = $mailbox; |
| 20 | $this->hostname = $hostname; |
| 21 | $this->name = $name; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 22 | $this->address = null; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 23 | |
| 24 | if (null !== $hostname) { |
| 25 | $this->address = $mailbox . '@' . $hostname; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @return null|string |
| 31 | */ |
| 32 | public function getAddress() |
| 33 | { |
| 34 | return $this->address; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns address with person name. |
| 39 | */ |
| 40 | public function getFullAddress(): string |
| 41 | { |
| 42 | $address = \sprintf('%s@%s', $this->mailbox, $this->hostname); |
| 43 | if (null !== $this->name) { |
| 44 | $address = \sprintf('"%s" <%s>', \addcslashes($this->name, '"'), $address); |
| 45 | } |
| 46 | |
| 47 | return $address; |
| 48 | } |
| 49 | |
| 50 | public function getMailbox(): string |
| 51 | { |
| 52 | return $this->mailbox; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return null|string |
| 57 | */ |
| 58 | public function getHostname() |
| 59 | { |
| 60 | return $this->hostname; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return null|string |
| 65 | */ |
| 66 | public function getName() |
| 67 | { |
| 68 | return $this->name; |
| 69 | } |
| 70 | } |