]> git.mxchange.org Git - friendica.git/blob - src/Object/Email.php
4f9c4e7bd94164678e2ad3f63d147a1a5a1743a2
[friendica.git] / src / Object / Email.php
1 <?php
2
3 namespace Friendica\Object;
4
5 use Friendica\Object\EMail\IEmail;
6
7 /**
8  * The default implementation of the IEmail interface
9  *
10  * Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient())
11  */
12 class Email implements IEmail
13 {
14         /** @var string */
15         private $fromName;
16         /** @var string */
17         private $fromAddress;
18         /** @var string */
19         private $replyTo;
20
21         /** @var string */
22         private $toAddress;
23
24         /** @var string */
25         private $subject;
26         /** @var string */
27         private $msgHtml;
28         /** @var string */
29         private $msgText;
30
31         /** @var string */
32         private $additionalMailHeader = '';
33         /** @var int|null */
34         private $toUid = null;
35
36         public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress,
37                                     string $subject, string $msgHtml, string $msgText,
38                                     string $additionalMailHeader = '', int $toUid = null)
39         {
40                 $this->fromName             = $fromName;
41                 $this->fromAddress          = $fromAddress;
42                 $this->replyTo              = $replyTo;
43                 $this->toAddress            = $toAddress;
44                 $this->subject              = $subject;
45                 $this->msgHtml              = $msgHtml;
46                 $this->msgText              = $msgText;
47                 $this->additionalMailHeader = $additionalMailHeader;
48                 $this->toUid                = $toUid;
49         }
50
51         /**
52          * {@inheritDoc}
53          */
54         public function getFromName()
55         {
56                 return $this->fromName;
57         }
58
59         /**
60          * {@inheritDoc}
61          */
62         public function getFromAddress()
63         {
64                 return $this->fromAddress;
65         }
66
67         /**
68          * {@inheritDoc}
69          */
70         public function getReplyTo()
71         {
72                 return $this->replyTo;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         public function getToAddress()
79         {
80                 return $this->toAddress;
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         public function getSubject()
87         {
88                 return $this->subject;
89         }
90
91         /**
92          * {@inheritDoc}
93          */
94         public function getMessage(bool $plain = false)
95         {
96                 if ($plain) {
97                         return $this->msgText;
98                 } else {
99                         return $this->msgHtml;
100                 }
101         }
102
103         /**
104          * {@inheritDoc}
105          */
106         public function getAdditionalMailHeader()
107         {
108                 return $this->additionalMailHeader;
109         }
110
111         /**
112          * {@inheritDoc}
113          */
114         public function getRecipientUid()
115         {
116                 return $this->toUid;
117         }
118
119         /**
120          * Returns the current email with a new recipient
121          *
122          * @param string $email The email of the recipient
123          * @param int    $uid   The (optional) UID of the recipient for further infos
124          *
125          * @return static
126          */
127         public function withRecipient(string $email, int $uid = null)
128         {
129                 $newEmail            = clone $this;
130                 $newEmail->toAddress = $email;
131                 $newEmail->toUid     = $uid;
132
133                 return $newEmail;
134         }
135
136         /**
137          * Creates a new Email instance based on a given prototype
138          *
139          * @param static $prototype The base prototype
140          * @param array  $data      The delta-data (key must be an existing property)
141          *
142          * @return static The new email instance
143          */
144         public static function createFromPrototype(Email $prototype, array $data = [])
145         {
146                 $newMail = clone $prototype;
147
148                 foreach ($data as $key => $value) {
149                         if (property_exists($newMail, $key)) {
150                                 $newMail->{$key} = $value;
151                         }
152                 }
153
154                 return $newMail;
155         }
156 }