]> git.mxchange.org Git - friendica.git/blob - src/Object/Email.php
32d1382831702985e41f82c527a847daf6b8ed7c
[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|null */
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          * {@inheritDoc}
121          */
122         public function withRecipient(string $address, int $uid = null)
123         {
124                 $newEmail            = clone $this;
125                 $newEmail->toAddress = $address;
126                 $newEmail->toUid     = $uid;
127
128                 return $newEmail;
129         }
130
131         /**
132          * {@inheritDoc}
133          */
134         public function withMessage(string $plaintext, string $html = null)
135         {
136                 $newMail          = clone $this;
137                 $newMail->msgText = $plaintext;
138                 $newMail->msgHtml = $html;
139
140                 return $newMail;
141         }
142
143         /**
144          * Returns the properties of the email as an array
145          *
146          * @return array
147          */
148         private function toArray()
149         {
150                 return get_object_vars($this);
151         }
152
153         /**
154          * @inheritDoc
155          */
156         public function __toString()
157         {
158                 return json_encode($this->toArray());
159         }
160
161         /**
162          * @inheritDoc
163          */
164         public function jsonSerialize()
165         {
166                 return $this->toArray();
167         }
168 }