friendica_2021.01_tupambae_.../src/Object/Email.php

188 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2020-02-09 15:45:36 +01:00
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object;
use Friendica\Object\EMail\IEmail;
/**
* The default implementation of the IEmail interface
*
* Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient())
*/
class Email implements IEmail
{
/** @var string */
private $fromName;
/** @var string */
private $fromAddress;
/** @var string */
private $replyTo;
/** @var string */
private $toAddress;
/** @var string */
private $subject;
2020-01-31 19:32:17 +01:00
/** @var string|null */
private $msgHtml;
/** @var string */
private $msgText;
/** @var string */
private $additionalMailHeader = '';
/** @var int|null */
private $toUid = null;
2020-01-27 00:01:17 +01:00
public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress,
string $subject, string $msgHtml, string $msgText,
string $additionalMailHeader = '', int $toUid = null)
{
$this->fromName = $fromName;
2020-01-27 00:01:17 +01:00
$this->fromAddress = $fromAddress;
$this->replyTo = $replyTo;
2020-01-27 00:01:17 +01:00
$this->toAddress = $toAddress;
$this->subject = $subject;
$this->msgHtml = $msgHtml;
$this->msgText = $msgText;
$this->additionalMailHeader = $additionalMailHeader;
$this->toUid = $toUid;
}
/**
* {@inheritDoc}
*/
public function getFromName()
{
return $this->fromName;
}
/**
* {@inheritDoc}
*/
public function getFromAddress()
{
return $this->fromAddress;
}
/**
* {@inheritDoc}
*/
public function getReplyTo()
{
return $this->replyTo;
}
/**
* {@inheritDoc}
*/
public function getToAddress()
{
return $this->toAddress;
}
/**
* {@inheritDoc}
*/
public function getSubject()
{
return $this->subject;
}
/**
* {@inheritDoc}
*/
2020-01-26 20:41:53 +01:00
public function getMessage(bool $plain = false)
{
2020-01-26 20:41:53 +01:00
if ($plain) {
return $this->msgText;
} else {
return $this->msgHtml;
}
}
/**
* {@inheritDoc}
*/
public function getAdditionalMailHeader()
{
return $this->additionalMailHeader;
}
/**
* {@inheritDoc}
*/
public function getRecipientUid()
{
return $this->toUid;
}
/**
2020-01-31 19:32:17 +01:00
* {@inheritDoc}
*/
2020-01-31 19:32:17 +01:00
public function withRecipient(string $address, int $uid = null)
{
$newEmail = clone $this;
2020-01-31 19:32:17 +01:00
$newEmail->toAddress = $address;
$newEmail->toUid = $uid;
return $newEmail;
}
/**
2020-01-31 19:32:17 +01:00
* {@inheritDoc}
*/
public function withMessage(string $plaintext, string $html = null)
{
$newMail = clone $this;
$newMail->msgText = $plaintext;
$newMail->msgHtml = $html;
return $newMail;
}
/**
* Returns the properties of the email as an array
*
2020-01-31 19:32:17 +01:00
* @return array
*/
2020-01-31 19:32:17 +01:00
private function toArray()
{
2020-01-31 19:32:17 +01:00
return get_object_vars($this);
}
2020-01-31 19:32:17 +01:00
/**
* @inheritDoc
*/
public function __toString()
{
return json_encode($this->toArray());
}
2020-01-31 19:32:17 +01:00
/**
* @inheritDoc
*/
public function jsonSerialize()
{
return $this->toArray();
}
}