blob: 4833487dd3e9c47798e4c864f2293464b41a448a [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;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010022 $this->address = null;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010023
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}