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