]> git.mxchange.org Git - friendica.git/blob - src/Object/Email.php
Merge pull request #8271 from MrPetovan/bug/8229-frio-mobile-back-to-top
[friendica.git] / src / Object / Email.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object;
23
24 use Friendica\Object\EMail\IEmail;
25
26 /**
27  * The default implementation of the IEmail interface
28  *
29  * Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient())
30  */
31 class Email implements IEmail
32 {
33         /** @var string */
34         private $fromName;
35         /** @var string */
36         private $fromAddress;
37         /** @var string */
38         private $replyTo;
39
40         /** @var string */
41         private $toAddress;
42
43         /** @var string */
44         private $subject;
45         /** @var string|null */
46         private $msgHtml;
47         /** @var string */
48         private $msgText;
49
50         /** @var string */
51         private $additionalMailHeader = '';
52         /** @var int|null */
53         private $toUid = null;
54
55         public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress,
56                                     string $subject, string $msgHtml, string $msgText,
57                                     string $additionalMailHeader = '', int $toUid = null)
58         {
59                 $this->fromName             = $fromName;
60                 $this->fromAddress          = $fromAddress;
61                 $this->replyTo              = $replyTo;
62                 $this->toAddress            = $toAddress;
63                 $this->subject              = $subject;
64                 $this->msgHtml              = $msgHtml;
65                 $this->msgText              = $msgText;
66                 $this->additionalMailHeader = $additionalMailHeader;
67                 $this->toUid                = $toUid;
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         public function getFromName()
74         {
75                 return $this->fromName;
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         public function getFromAddress()
82         {
83                 return $this->fromAddress;
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         public function getReplyTo()
90         {
91                 return $this->replyTo;
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         public function getToAddress()
98         {
99                 return $this->toAddress;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public function getSubject()
106         {
107                 return $this->subject;
108         }
109
110         /**
111          * {@inheritDoc}
112          */
113         public function getMessage(bool $plain = false)
114         {
115                 if ($plain) {
116                         return $this->msgText;
117                 } else {
118                         return $this->msgHtml;
119                 }
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         public function getAdditionalMailHeader()
126         {
127                 return $this->additionalMailHeader;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         public function getRecipientUid()
134         {
135                 return $this->toUid;
136         }
137
138         /**
139          * {@inheritDoc}
140          */
141         public function withRecipient(string $address, int $uid = null)
142         {
143                 $newEmail            = clone $this;
144                 $newEmail->toAddress = $address;
145                 $newEmail->toUid     = $uid;
146
147                 return $newEmail;
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         public function withMessage(string $plaintext, string $html = null)
154         {
155                 $newMail          = clone $this;
156                 $newMail->msgText = $plaintext;
157                 $newMail->msgHtml = $html;
158
159                 return $newMail;
160         }
161
162         /**
163          * Returns the properties of the email as an array
164          *
165          * @return array
166          */
167         private function toArray()
168         {
169                 return get_object_vars($this);
170         }
171
172         /**
173          * @inheritDoc
174          */
175         public function __toString()
176         {
177                 return json_encode($this->toArray());
178         }
179
180         /**
181          * @inheritDoc
182          */
183         public function jsonSerialize()
184         {
185                 return $this->toArray();
186         }
187 }