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 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | private $mailbox; |
| 16 | |
| 17 | /** |
| 18 | * @var null|string |
| 19 | */ |
| 20 | private $hostname; |
| 21 | |
| 22 | /** |
| 23 | * @var null|string |
| 24 | */ |
| 25 | private $name; |
| 26 | |
| 27 | /** |
| 28 | * @var null|string |
| 29 | */ |
| 30 | private $address; |
| 31 | |
| 32 | public function __construct(string $mailbox, string $hostname = null, string $name = null) |
| 33 | { |
| 34 | $this->mailbox = $mailbox; |
| 35 | $this->hostname = $hostname; |
| 36 | $this->name = $name; |
| 37 | |
| 38 | if (null !== $hostname) { |
| 39 | $this->address = $mailbox . '@' . $hostname; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return null|string |
| 45 | */ |
| 46 | public function getAddress() |
| 47 | { |
| 48 | return $this->address; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns address with person name. |
| 53 | */ |
| 54 | public function getFullAddress(): string |
| 55 | { |
| 56 | $address = \sprintf('%s@%s', $this->mailbox, $this->hostname); |
| 57 | if (null !== $this->name) { |
| 58 | $address = \sprintf('"%s" <%s>', \addcslashes($this->name, '"'), $address); |
| 59 | } |
| 60 | |
| 61 | return $address; |
| 62 | } |
| 63 | |
| 64 | public function getMailbox(): string |
| 65 | { |
| 66 | return $this->mailbox; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return null|string |
| 71 | */ |
| 72 | public function getHostname() |
| 73 | { |
| 74 | return $this->hostname; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return null|string |
| 79 | */ |
| 80 | public function getName() |
| 81 | { |
| 82 | return $this->name; |
| 83 | } |
| 84 | } |