Remove useless NewInstance

Bugfix: BodyStructure->SearchCharset return value
This commit is contained in:
djmaze 2020-05-08 11:49:18 +02:00
parent 82e560bc53
commit 693e0f4c10
61 changed files with 802 additions and 1345 deletions

View file

@ -57,8 +57,11 @@ class Attachment
*/
private $sContentLocation;
private function __construct($rResource, string $sFileName, int $iFileSize, bool $bIsInline, bool $bIsLinked, string $sCID,
array $aCustomContentTypeParams = array(), string $sContentLocation = '')
/**
* @param resource $rResource
*/
function __construct($rResource, string $sFileName, int $iFileSize, bool $bIsInline,
bool $bIsLinked, string $sCID, array $aCustomContentTypeParams = [], string $sContentLocation = '')
{
$this->rResource = $rResource;
$this->sFileName = $sFileName;
@ -70,15 +73,6 @@ class Attachment
$this->sContentLocation = $sContentLocation;
}
/**
* @param resource $rResource
*/
public static function NewInstance($rResource, string $sFileName = '', int $iFileSize = 0, bool $bIsInline = false,
bool $bIsLinked = false, string $sCID = '', array $aCustomContentTypeParams = array(), string $sContentLocation = '') : self
{
return new self($rResource, $sFileName, $iFileSize, $bIsInline, $bIsLinked, $sCID, $aCustomContentTypeParams, $sContentLocation);
}
/**
* @return resource
*/

View file

@ -17,11 +17,6 @@ namespace MailSo\Mime;
*/
class AttachmentCollection extends \MailSo\Base\Collection
{
public static function NewInstance() : self
{
return new self();
}
public function append($oAttachment, bool $bToTop = false) : void
{
assert($oAttachment instanceof Attachment);

View file

@ -40,11 +40,11 @@ class Email
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
private function __construct(string $sEmail, string $sDisplayName = '')
function __construct(string $sEmail, string $sDisplayName = '')
{
if (!strlen(\trim($sEmail)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
throw new \MailSo\Base\Exceptions\InvalidArgumentException;
}
$this->sEmail = \MailSo\Base\Utils::IdnToAscii(
@ -52,18 +52,10 @@ class Email
$this->sDisplayName = \MailSo\Base\Utils::Trim($sDisplayName);
$this->sDkimStatus = \MailSo\Mime\Enumerations\DkimStatus::NONE;
$this->sDkimStatus = Enumerations\DkimStatus::NONE;
$this->sDkimValue = '';
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
public static function NewInstance(string $sEmail, string $sDisplayName = '') : self
{
return new self($sEmail, $sDisplayName);
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
@ -72,7 +64,7 @@ class Email
$sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress);
if (!strlen(\trim($sEmailAddress)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
throw new \MailSo\Base\Exceptions\InvalidArgumentException;
}
$sName = '';
@ -191,7 +183,7 @@ class Email
$sName = \preg_replace('/\\\\(.)/s', '$1', $sName);
$sComment = \preg_replace('/\\\\(.)/s', '$1', $sComment);
return Email::NewInstance($sEmail, $sName);
return new self($sEmail, $sName);
}
public function GetEmail(bool $bIdn = false) : string
@ -226,7 +218,7 @@ class Email
public function SetDkimStatusAndValue(string $sDkimStatus, string $sDkimValue = '')
{
$this->sDkimStatus = \MailSo\Mime\Enumerations\DkimStatus::normalizeValue($sDkimStatus);
$this->sDkimStatus = Enumerations\DkimStatus::normalizeValue($sDkimStatus);
$this->sDkimValue = $sDkimValue;
}

View file

@ -17,33 +17,22 @@ namespace MailSo\Mime;
*/
class EmailCollection extends \MailSo\Base\Collection
{
protected function __construct(string $sEmailAddresses = '')
function __construct($mEmailAddresses = '')
{
parent::__construct();
$sEmailAddresses = \MailSo\Base\Utils::Trim($sEmailAddresses);
if (0 < \strlen($sEmailAddresses))
{
$this->parseEmailAddresses($sEmailAddresses);
if (\is_string($mEmailAddresses)) {
parent::__construct();
$this->parseEmailAddresses($mEmailAddresses);
} else {
parent::__construct($mEmailAddresses);
}
}
public static function NewInstance(string $sEmailAddresses = '') : self
{
return new self($sEmailAddresses);
}
public function append($oEmail, bool $bToTop = false) : void
{
assert($oEmail instanceof Email);
parent::append($oEmail, $bToTop);
}
public static function Parse(string $sEmailAddresses) : self
{
return self::NewInstance($sEmailAddresses);
}
public function ToArray() : array
{
$aReturn = array();
@ -78,9 +67,7 @@ class EmailCollection extends \MailSo\Base\Collection
}
}
$this->exchangeArray(array_values($aReturn));
return $this;
return new static($aReturn);
}
public function ToString(bool $bConvertSpecialsName = false, bool $bIdn = false) : string
@ -94,15 +81,15 @@ class EmailCollection extends \MailSo\Base\Collection
return \implode(', ', $aReturn);
}
private function parseEmailAddresses(string $sRawEmails) : self
private function parseEmailAddresses(string $sRawEmails) : void
{
$this->Clear();
// $sRawEmails = \MailSo\Base\Utils::Trim($sRawEmails);
$sRawEmails = \trim($sRawEmails);
$sWorkingRecipients = \trim($sRawEmails);
if (0 === \strlen($sWorkingRecipients))
$sWorkingRecipientsLen = \strlen($sRawEmails);
if (!$sWorkingRecipientsLen)
{
return $this;
return;
}
$iEmailStartPos = 0;
@ -115,20 +102,18 @@ class EmailCollection extends \MailSo\Base\Collection
$iCurrentPos = 0;
$sWorkingRecipientsLen = \strlen($sWorkingRecipients);
while ($iCurrentPos < $sWorkingRecipientsLen)
{
switch ($sWorkingRecipients[$iCurrentPos])
switch ($sRawEmails[$iCurrentPos])
{
case '\'':
case '"':
if (!$bIsInQuotes)
{
$sChQuote = $sWorkingRecipients[$iCurrentPos];
$sChQuote = $sRawEmails[$iCurrentPos];
$bIsInQuotes = true;
}
else if ($sChQuote == $sWorkingRecipients[$iCurrentPos])
else if ($sChQuote == $sRawEmails[$iCurrentPos])
{
$bIsInQuotes = false;
}
@ -175,7 +160,7 @@ class EmailCollection extends \MailSo\Base\Collection
try
{
$this->append(
Email::Parse(\substr($sWorkingRecipients, $iEmailStartPos, $iEmailEndPos - $iEmailStartPos))
Email::Parse(\substr($sRawEmails, $iEmailStartPos, $iEmailEndPos - $iEmailStartPos))
);
$iEmailStartPos = $iCurrentPos + 1;
@ -187,7 +172,7 @@ class EmailCollection extends \MailSo\Base\Collection
break;
}
$iCurrentPos++;
++$iCurrentPos;
}
if ($iEmailStartPos < $iCurrentPos)
@ -195,12 +180,10 @@ class EmailCollection extends \MailSo\Base\Collection
try
{
$this->append(
Email::Parse(\substr($sWorkingRecipients, $iEmailStartPos, $iCurrentPos - $iEmailStartPos))
Email::Parse(\substr($sRawEmails, $iEmailStartPos, $iCurrentPos - $iEmailStartPos))
);
}
catch (\MailSo\Base\Exceptions\InvalidArgumentException $oException) {}
}
return $this;
}
}

View file

@ -1,283 +0,0 @@
<?php
/*
* This file is part of MailSo.
*
* (c) 2014 Usenko Timur
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MailSo\Mime;
/**
* @deprecated
* @category MailSo
* @package Mime
*/
class EmailDep
{
/**
* @var string
*/
private $sDisplayName;
/**
* @var string
*/
private $sEmail;
/**
* @var string
*/
private $sRemark;
/**
* @var string
*/
private $sDkimStatus;
/**
* @var string
*/
private $sDkimValue;
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
private function __construct(string $sEmail, string $sDisplayName = '', string $sRemark = '')
{
if (!strlen(\trim($sEmail)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}
$this->sEmail = \MailSo\Base\Utils::IdnToAscii(
\MailSo\Base\Utils::Trim($sEmail), true);
$this->sDisplayName = \MailSo\Base\Utils::Trim($sDisplayName);
$this->sRemark = \MailSo\Base\Utils::Trim($sRemark);
$this->sDkimStatus = \MailSo\Mime\Enumerations\DkimStatus::NONE;
$this->sDkimValue = '';
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
public static function NewInstance(string $sEmail, string $sDisplayName = '', string $sRemark = '') : \MailSo\Mime\Email
{
return new self($sEmail, $sDisplayName, $sRemark);
}
/**
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
public static function Parse(string $sEmailAddress) : \MailSo\Mime\Email
{
$sEmailAddress = \MailSo\Base\Utils::Trim($sEmailAddress);
if (!strlen(\trim($sEmailAddress)))
{
throw new \MailSo\Base\Exceptions\InvalidArgumentException();
}
$sName = '';
$sEmail = '';
$sComment = '';
$bInName = false;
$bInAddress = false;
$bInComment = false;
$iStartIndex = 0;
$iEndIndex = 0;
$iCurrentIndex = 0;
while ($iCurrentIndex < \strlen($sEmailAddress))
{
switch ($sEmailAddress[$iCurrentIndex])
{
// case '\'':
case '"':
// $sQuoteChar = $sEmailAddress[$iCurrentIndex];
if ((!$bInName) && (!$bInAddress) && (!$bInComment))
{
$bInName = true;
$iStartIndex = $iCurrentIndex;
}
else if ((!$bInAddress) && (!$bInComment))
{
$iEndIndex = $iCurrentIndex;
$sName = \substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1);
$sEmailAddress = \substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1);
$iEndIndex = 0;
$iCurrentIndex = 0;
$iStartIndex = 0;
$bInName = false;
}
break;
case '<':
if ((!$bInName) && (!$bInAddress) && (!$bInComment))
{
if ($iCurrentIndex > 0 && \strlen($sName) === 0)
{
$sName = \substr($sEmailAddress, 0, $iCurrentIndex);
}
$bInAddress = true;
$iStartIndex = $iCurrentIndex;
}
break;
case '>':
if ($bInAddress)
{
$iEndIndex = $iCurrentIndex;
$sEmail = \substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1);
$sEmailAddress = \substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1);
$iEndIndex = 0;
$iCurrentIndex = 0;
$iStartIndex = 0;
$bInAddress = false;
}
break;
case '(':
if ((!$bInName) && (!$bInAddress) && (!$bInComment))
{
$bInComment = true;
$iStartIndex = $iCurrentIndex;
}
break;
case ')':
if ($bInComment)
{
$iEndIndex = $iCurrentIndex;
$sComment = \substr($sEmailAddress, $iStartIndex + 1, $iEndIndex - $iStartIndex - 1);
$sEmailAddress = \substr_replace($sEmailAddress, '', $iStartIndex, $iEndIndex - $iStartIndex + 1);
$iEndIndex = 0;
$iCurrentIndex = 0;
$iStartIndex = 0;
$bInComment = false;
}
break;
case '\\':
$iCurrentIndex++;
break;
}
$iCurrentIndex++;
}
if (\strlen($sEmail) === 0)
{
$aRegs = array('');
if (\preg_match('/[^@\s]+@\S+/i', $sEmailAddress, $aRegs) && isset($aRegs[0]))
{
$sEmail = $aRegs[0];
}
else
{
$sName = $sEmailAddress;
}
}
if ((\strlen($sEmail) > 0) && (\strlen($sName) == 0) && (\strlen($sComment) == 0))
{
$sName = \str_replace($sEmail, '', $sEmailAddress);
}
$sEmail = \trim(\trim($sEmail), '<>');
$sEmail = \rtrim(\trim($sEmail), '.');
$sEmail = \trim($sEmail);
$sName = \trim(\trim($sName), '"');
$sName = \trim($sName, '\'');
$sComment = \trim(\trim($sComment), '()');
// Remove backslash
$sName = \preg_replace('/\\\\(.)/s', '$1', $sName);
$sComment = \preg_replace('/\\\\(.)/s', '$1', $sComment);
return Email::NewInstance($sEmail, $sName, $sComment);
}
public function GetEmail(bool $bIdn = false) : string
{
return $bIdn ? \MailSo\Base\Utils::IdnToUtf8($this->sEmail) : $this->sEmail;
}
public function GetDisplayName() : string
{
return $this->sDisplayName;
}
public function GetRemark() : string
{
return $this->sRemark;
}
public function GetDkimStatus() : string
{
return $this->sDkimStatus;
}
public function GetDkimValue() : string
{
return $this->sDkimValue;
}
public function GetAccountName() : string
{
return \MailSo\Base\Utils::GetAccountNameFromEmail($this->GetEmail(false));
}
public function GetDomain(bool $bIdn = false) : string
{
return \MailSo\Base\Utils::GetDomainFromEmail($this->GetEmail($bIdn));
}
public function SetDkimStatusAndValue(string $sDkimStatus, string $sDkimValue = '') : void
{
$this->sDkimStatus = \MailSo\Mime\Enumerations\DkimStatus::normalizeValue($sDkimStatus);
$this->sDkimValue = $sDkimValue;
}
public function ToArray(bool $bIdn = false, bool $bDkim = true) : array
{
return $bDkim ? array($this->sDisplayName, $this->GetEmail($bIdn), $this->sRemark, $this->sDkimStatus, $this->sDkimValue) :
array($this->sDisplayName, $this->GetEmail($bIdn), $this->sRemark);
}
public function ToString(bool $bConvertSpecialsName = false, bool $bIdn = false) : string
{
$sReturn = '';
$sRemark = \str_replace(')', '\)', $this->sRemark);
$sDisplayName = \str_replace('"', '\"', $this->sDisplayName);
if ($bConvertSpecialsName)
{
$sDisplayName = 0 === \strlen($sDisplayName) ? '' : \MailSo\Base\Utils::EncodeUnencodedValue(
\MailSo\Base\Enumerations\Encoding::BASE64_SHORT,
$sDisplayName);
$sRemark = 0 === \strlen($sRemark) ? '' : \MailSo\Base\Utils::EncodeUnencodedValue(
\MailSo\Base\Enumerations\Encoding::BASE64_SHORT,
$sRemark);
}
$sDisplayName = 0 === \strlen($sDisplayName) ? '' : '"'.$sDisplayName.'"';
$sRemark = 0 === \strlen($sRemark) ? '' : '('.$sRemark.')';
if (0 < \strlen($this->sEmail))
{
$sReturn = $this->GetEmail($bIdn);
if (0 < \strlen($sDisplayName.$sRemark))
{
$sReturn = $sDisplayName.' <'.$sReturn.'> '.$sRemark;
}
}
return \trim($sReturn);
}
}

View file

@ -47,7 +47,7 @@ class Header
*/
private $sParentCharset;
private function __construct(string $sName, string $sValue, string $sEncodedValueForReparse, string $sParentCharset = '')
function __construct(string $sName, string $sValue = '', string $sEncodedValueForReparse = '', string $sParentCharset = '')
{
$this->sParentCharset = $sParentCharset;
@ -72,8 +72,7 @@ class Header
if (2 === \count($aRawExplode))
{
$this->sValue = $aRawExplode[0];
$this->oParameters =
\MailSo\Mime\ParameterCollection::NewInstance($aRawExplode[1]);
$this->oParameters = new ParameterCollection($aRawExplode[1]);
}
else
{
@ -86,12 +85,7 @@ class Header
}
}
public static function NewInstance(string $sName, string $sValue = '', string $sEncodedValueForReparse = '', string $sParentCharset = '') : \MailSo\Mime\Header
{
return new self($sName, $sValue, $sEncodedValueForReparse, $sParentCharset);
}
public static function NewInstanceFromEncodedString(string $sEncodedLines, string $sIncomingCharset = \MailSo\Base\Enumerations\Charset::ISO_8859_1) : \MailSo\Mime\Header
public static function NewInstanceFromEncodedString(string $sEncodedLines, string $sIncomingCharset = \MailSo\Base\Enumerations\Charset::ISO_8859_1) : Header
{
if (empty($sIncomingCharset))
{
@ -101,7 +95,7 @@ class Header
$aParts = \explode(':', \str_replace("\r", '', $sEncodedLines), 2);
if (isset($aParts[0]) && isset($aParts[1]) && 0 < \strlen($aParts[0]) && 0 < \strlen($aParts[1]))
{
return self::NewInstance(
return new self(
\trim($aParts[0]),
\trim(\MailSo\Base\Utils::DecodeHeaderValue(\trim($aParts[1]), $sIncomingCharset)),
\trim($aParts[1]),
@ -132,7 +126,7 @@ class Header
return $this->sFullValue;
}
public function SetParentCharset(string $sParentCharset) : \MailSo\Mime\Header
public function SetParentCharset(string $sParentCharset) : Header
{
if ($this->sParentCharset !== $sParentCharset && $this->IsReparsed() && 0 < \strlen($this->sEncodedValueForReparse))
{
@ -148,7 +142,7 @@ class Header
return $this;
}
public function Parameters() : ?\MailSo\Mime\ParameterCollection
public function Parameters() : ?ParameterCollection
{
return $this->oParameters;
}
@ -156,7 +150,7 @@ class Header
private function wordWrapHelper(string $sValue, string $sGlue = "\r\n ") : string
{
return \trim(substr(wordwrap($this->NameWithDelimitrom().$sValue,
\MailSo\Mime\Enumerations\Constants::LINE_LENGTH, $sGlue
Enumerations\Constants::LINE_LENGTH, $sGlue
), \strlen($this->NameWithDelimitrom())));
}
@ -174,8 +168,8 @@ class Header
'scheme' => \MailSo\Base\Enumerations\Encoding::BASE64_SHORT,
'input-charset' => \MailSo\Base\Enumerations\Charset::UTF_8,
'output-charset' => \MailSo\Base\Enumerations\Charset::UTF_8,
'line-length' => \MailSo\Mime\Enumerations\Constants::LINE_LENGTH,
'line-break-chars' => \MailSo\Mime\Enumerations\Constants::CRLF
'line-length' => Enumerations\Constants::LINE_LENGTH,
'line-break-chars' => Enumerations\Constants::CRLF
);
return \iconv_mime_encode($this->Name(), $sResult, $aPreferences);
@ -187,7 +181,7 @@ class Header
}
else if ($this->IsEmail())
{
$oEmailCollection = \MailSo\Mime\EmailCollection::NewInstance($this->sFullValue);
$oEmailCollection = new EmailCollection($this->sFullValue);
if ($oEmailCollection && 0 < $oEmailCollection->Count())
{
$sResult = $oEmailCollection->ToString(true, false);
@ -199,27 +193,27 @@ class Header
public function IsSubject() : bool
{
return \strtolower(\MailSo\Mime\Enumerations\Header::SUBJECT) === \strtolower($this->Name());
return \strtolower(Enumerations\Header::SUBJECT) === \strtolower($this->Name());
}
public function IsParameterized() : bool
{
return \in_array(\strtolower($this->sName), array(
\strtolower(\MailSo\Mime\Enumerations\Header::CONTENT_TYPE),
\strtolower(\MailSo\Mime\Enumerations\Header::CONTENT_DISPOSITION)
\strtolower(Enumerations\Header::CONTENT_TYPE),
\strtolower(Enumerations\Header::CONTENT_DISPOSITION)
));
}
public function IsEmail() : bool
{
return \in_array(\strtolower($this->sName), array(
\strtolower(\MailSo\Mime\Enumerations\Header::FROM_),
\strtolower(\MailSo\Mime\Enumerations\Header::TO_),
\strtolower(\MailSo\Mime\Enumerations\Header::CC),
\strtolower(\MailSo\Mime\Enumerations\Header::BCC),
\strtolower(\MailSo\Mime\Enumerations\Header::REPLY_TO),
\strtolower(\MailSo\Mime\Enumerations\Header::RETURN_PATH),
\strtolower(\MailSo\Mime\Enumerations\Header::SENDER)
\strtolower(Enumerations\Header::FROM_),
\strtolower(Enumerations\Header::TO_),
\strtolower(Enumerations\Header::CC),
\strtolower(Enumerations\Header::BCC),
\strtolower(Enumerations\Header::REPLY_TO),
\strtolower(Enumerations\Header::RETURN_PATH),
\strtolower(Enumerations\Header::SENDER)
));
}

View file

@ -25,20 +25,14 @@ class HeaderCollection extends \MailSo\Base\Collection
*/
protected $sParentCharset = '';
protected function __construct(string $sRawHeaders = '', bool $bStoreRawHeaders = true)
function __construct(string $sRawHeaders = '', bool $bStoreRawHeaders = true, string $sParentCharset = '')
{
parent::__construct();
if (0 < \strlen($sRawHeaders)) {
$this->Parse($sRawHeaders, $bStoreRawHeaders);
if (\strlen($sRawHeaders)) {
$this->Parse($sRawHeaders, $bStoreRawHeaders, $sParentCharset);
}
}
public static function NewInstance(string $sRawHeaders = '', bool $bStoreRawHeaders = true) : self
{
return new self($sRawHeaders, $bStoreRawHeaders);
}
public function append($oHeader, bool $bToTop = false) : void
{
assert($oHeader instanceof Header);
@ -47,13 +41,13 @@ class HeaderCollection extends \MailSo\Base\Collection
public function AddByName(string $sName, string $sValue, bool $bToTop = false) : self
{
$this->append(Header::NewInstance($sName, $sValue), $bToTop);
$this->append(new Header($sName, $sValue), $bToTop);
return $this;
}
public function SetByName(string $sName, string $sValue, bool $bToTop = false) : self
{
return $this->RemoveByName($sName)->Add(Header::NewInstance($sName, $sValue), $bToTop);
return $this->RemoveByName($sName)->Add(new Header($sName, $sValue), $bToTop);
}
public function ValueByName(string $sHeaderName, bool $bCharsetAutoDetect = false) : string
@ -88,7 +82,7 @@ class HeaderCollection extends \MailSo\Base\Collection
$oResult = null;
$sValue = $this->ValueByName($sHeaderName, $bCharsetAutoDetect);
if (0 < \strlen($sValue)) {
$oResult = EmailCollection::NewInstance($sValue);
$oResult = new EmailCollection($sValue);
}
return $oResult && 0 < $oResult->Count() ? $oResult : null;
}
@ -118,20 +112,18 @@ class HeaderCollection extends \MailSo\Base\Collection
public function SetParentCharset(string $sParentCharset) : self
{
if (0 < \strlen($sParentCharset)){
if ($this->sParentCharset !== $sParentCharset) {
foreach ($this as $oHeader) {
$oHeader->SetParentCharset($sParentCharset);
}
$this->sParentCharset = $sParentCharset;
if (\strlen($sParentCharset) && $this->sParentCharset !== $sParentCharset) {
foreach ($this as $oHeader) {
$oHeader->SetParentCharset($sParentCharset);
}
$this->sParentCharset = $sParentCharset;
}
return $this;
}
public function Clear() : void
{
$this->exchangeArray(array());
parent::Clear();
$this->sRawHeaders = '';
}
@ -143,7 +135,7 @@ class HeaderCollection extends \MailSo\Base\Collection
$this->sRawHeaders = $sRawHeaders;
}
if (0 === \strlen($this->sParentCharset)) {
if (\strlen($this->sParentCharset)) {
$this->sParentCharset = $sParentCharset;
}

View file

@ -42,20 +42,15 @@ class Message
*/
private $bAddDefaultXMailer;
private function __construct()
function __construct()
{
$this->aHeadersValue = array();
$this->aAlternativeParts = array();
$this->oAttachmentCollection = AttachmentCollection::NewInstance();
$this->oAttachmentCollection = new AttachmentCollection;
$this->bAddEmptyTextPart = true;
$this->bAddDefaultXMailer = true;
}
public static function NewInstance() : self
{
return new self();
}
public function DoesNotCreateEmptyTextPart() : self
{
$this->bAddEmptyTextPart = false;
@ -103,64 +98,66 @@ class Message
public function GetFrom() : ?Email
{
$oResult = null;
if (isset($this->aHeadersValue[Enumerations\Header::FROM_]) &&
$this->aHeadersValue[Enumerations\Header::FROM_] instanceof Email)
{
$oResult = $this->aHeadersValue[Enumerations\Header::FROM_];
return $this->aHeadersValue[Enumerations\Header::FROM_];
}
return $oResult;
return null;
}
public function GetTo() : EmailCollection
{
$oResult = EmailCollection::NewInstance();
if (isset($this->aHeadersValue[Enumerations\Header::TO_]) &&
$this->aHeadersValue[Enumerations\Header::TO_] instanceof EmailCollection)
{
$oResult->MergeWithOtherCollection($this->aHeadersValue[Enumerations\Header::TO_]);
return $this->aHeadersValue[Enumerations\Header::TO_]->Unique();
}
return $oResult->Unique();
return new EmailCollection;
}
public function GetBcc() : ?EmailCollection
{
$oResult = null;
if (isset($this->aHeadersValue[Enumerations\Header::BCC]) &&
$this->aHeadersValue[Enumerations\Header::BCC] instanceof EmailCollection)
{
$oResult = $this->aHeadersValue[Enumerations\Header::BCC];
return $this->aHeadersValue[Enumerations\Header::BCC]->Unique();
}
return $oResult ? $oResult->Unique() : null;
return null;
}
public function GetRcpt() : EmailCollection
{
$oResult = EmailCollection::NewInstance();
$oResult = new EmailCollection;
if (isset($this->aHeadersValue[Enumerations\Header::TO_]) &&
$this->aHeadersValue[Enumerations\Header::TO_] instanceof EmailCollection)
{
$oResult->MergeWithOtherCollection($this->aHeadersValue[Enumerations\Header::TO_]);
$headers = array(Enumerations\Header::TO_, Enumerations\Header::CC, Enumerations\Header::BCC);
foreach ($headers as $header) {
if (isset($this->aHeadersValue[$header]) && $this->aHeadersValue[$header] instanceof EmailCollection) {
foreach ($this->aHeadersValue[$header] as $oEmail) {
$oResult->append($oEmail);
}
}
}
if (isset($this->aHeadersValue[Enumerations\Header::CC]) &&
$this->aHeadersValue[Enumerations\Header::CC] instanceof EmailCollection)
{
$oResult->MergeWithOtherCollection($this->aHeadersValue[Enumerations\Header::CC]);
}
if (isset($this->aHeadersValue[Enumerations\Header::BCC]) &&
$this->aHeadersValue[Enumerations\Header::BCC] instanceof EmailCollection)
{
$oResult->MergeWithOtherCollection($this->aHeadersValue[Enumerations\Header::BCC]);
/*
$aReturn = array();
$headers = array(Enumerations\Header::TO_, Enumerations\Header::CC, Enumerations\Header::BCC);
foreach ($headers as $header) {
if (isset($this->aHeadersValue[$header]) && $this->aHeadersValue[$header] instanceof EmailCollection) {
foreach ($this->aHeadersValue[$header] as $oEmail) {
$oResult->append($oEmail);
$sEmail = $oEmail->GetEmail();
if (!isset($aReturn[$sEmail])) {
$aReturn[$sEmail] = $oEmail;
}
}
}
}
return new EmailCollection($aReturn);
*/
return $oResult->Unique();
}
@ -317,10 +314,10 @@ class Message
public function SetDraftInfo(string $sType, string $sUid, string $sFolder) : self
{
$this->aHeadersValue[Enumerations\Header::X_DRAFT_INFO] = ParameterCollection::NewInstance()
->Add(Parameter::NewInstance('type', $sType))
->Add(Parameter::NewInstance('uid', $sUid))
->Add(Parameter::NewInstance('folder', base64_encode($sFolder)))
$this->aHeadersValue[Enumerations\Header::X_DRAFT_INFO] = (new ParameterCollection)
->Add(new Parameter('type', $sType))
->Add(new Parameter('uid', $sUid))
->Add(new Parameter('folder', base64_encode($sFolder)))
;
return $this;
@ -382,7 +379,7 @@ class Message
private function createNewMessageAttachmentBody(Attachment $oAttachment) : Part
{
$oAttachmentPart = Part::NewInstance();
$oAttachmentPart = new Part;
$sFileName = $oAttachment->FileName();
$sCID = $oAttachment->CID();
@ -394,23 +391,23 @@ class Message
if (0 < strlen(trim($sFileName)))
{
$oContentTypeParameters =
ParameterCollection::NewInstance()->Add(Parameter::NewInstance(
(new ParameterCollection)->Add(new Parameter(
Enumerations\Parameter::NAME, $sFileName));
$oContentDispositionParameters =
ParameterCollection::NewInstance()->Add(Parameter::NewInstance(
(new ParameterCollection)->Add(new Parameter(
Enumerations\Parameter::FILENAME, $sFileName));
}
$oAttachmentPart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_TYPE,
new Header(Enumerations\Header::CONTENT_TYPE,
$oAttachment->ContentType().';'.
(($oContentTypeParameters) ? ' '.$oContentTypeParameters->ToString() : '')
)
);
$oAttachmentPart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_DISPOSITION,
new Header(Enumerations\Header::CONTENT_DISPOSITION,
($oAttachment->IsInline() ? 'inline' : 'attachment').';'.
(($oContentDispositionParameters) ? ' '.$oContentDispositionParameters->ToString() : '')
)
@ -419,14 +416,14 @@ class Message
if (0 < strlen($sCID))
{
$oAttachmentPart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_ID, $sCID)
new Header(Enumerations\Header::CONTENT_ID, $sCID)
);
}
if (0 < strlen($sContentLocation))
{
$oAttachmentPart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_LOCATION, $sContentLocation)
new Header(Enumerations\Header::CONTENT_LOCATION, $sContentLocation)
);
}
@ -435,7 +432,7 @@ class Message
if ('message/rfc822' !== strtolower($oAttachment->ContentType()))
{
$oAttachmentPart->Headers->append(
Header::NewInstance(
new Header(
Enumerations\Header::CONTENT_TRANSFER_ENCODING,
\MailSo\Base\Enumerations\Encoding::BASE64_LOWER
)
@ -464,10 +461,10 @@ class Message
if (isset($aAlternativeData[0]))
{
$oAlternativePart = Part::NewInstance();
$oParameters = ParameterCollection::NewInstance();
$oAlternativePart = new Part;
$oParameters = new ParameterCollection;
$oParameters->append(
Parameter::NewInstance(
new Parameter(
Enumerations\Parameter::CHARSET,
\MailSo\Base\Enumerations\Charset::UTF_8)
);
@ -476,12 +473,12 @@ class Message
{
foreach ($aAlternativeData[3] as $sName => $sValue)
{
$oParameters->append(Parameter::NewInstance($sName, $sValue));
$oParameters->append(new Parameter($sName, $sValue));
}
}
$oAlternativePart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_TYPE,
new Header(Enumerations\Header::CONTENT_TYPE,
$aAlternativeData[0].'; '.$oParameters->ToString())
);
@ -502,7 +499,7 @@ class Message
if (isset($aAlternativeData[2]) && 0 < strlen($aAlternativeData[2]))
{
$oAlternativePart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_TRANSFER_ENCODING,
new Header(Enumerations\Header::CONTENT_TRANSFER_ENCODING,
$aAlternativeData[2]
)
);
@ -536,13 +533,13 @@ class Message
$oResultPart = null;
if (1 < count($this->aAlternativeParts))
{
$oResultPart = Part::NewInstance();
$oResultPart = new Part;
$oResultPart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_TYPE,
new Header(Enumerations\Header::CONTENT_TYPE,
Enumerations\MimeType::MULTIPART_ALTERNATIVE.'; '.
ParameterCollection::NewInstance()->Add(
Parameter::NewInstance(
(new ParameterCollection)->Add(
new Parameter(
Enumerations\Parameter::BOUNDARY,
$this->generateNewBoundary())
)->ToString()
@ -603,13 +600,13 @@ class Message
$aAttachments = $this->oAttachmentCollection->LinkedAttachments();
if (0 < count($aAttachments))
{
$oResultPart = Part::NewInstance();
$oResultPart = new Part;
$oResultPart->Headers->append(
Header::NewInstance(Enumerations\Header::CONTENT_TYPE,
new Header(Enumerations\Header::CONTENT_TYPE,
Enumerations\MimeType::MULTIPART_RELATED.'; '.
ParameterCollection::NewInstance()->Add(
Parameter::NewInstance(
(new ParameterCollection)->Add(
new Parameter(
Enumerations\Parameter::BOUNDARY,
$this->generateNewBoundary())
)->ToString()
@ -638,12 +635,12 @@ class Message
$aAttachments = $this->oAttachmentCollection->UnlinkedAttachments();
if (0 < count($aAttachments))
{
$oResultPart = Part::NewInstance();
$oResultPart = new Part;
$oResultPart->Headers->AddByName(Enumerations\Header::CONTENT_TYPE,
Enumerations\MimeType::MULTIPART_MIXED.'; '.
ParameterCollection::NewInstance()->Add(
Parameter::NewInstance(
(new ParameterCollection)->Add(
new Parameter(
Enumerations\Parameter::BOUNDARY,
$this->generateNewBoundary())
)->ToString()

View file

@ -27,20 +27,15 @@ class Parameter
*/
private $sValue;
private function __construct(string $sName, string $sValue)
function __construct(string $sName, string $sValue)
{
$this->sName = $sName;
$this->sValue = $sValue;
}
public static function NewInstance(string $sName, string $sValue = '') : self
{
return new self($sName, $sValue);
}
public static function CreateFromParameterLine(string $sRawParam) : self
{
$oParameter = self::NewInstance('');
$oParameter = new self('', '');
return $oParameter->Parse($sRawParam);
}
@ -84,8 +79,8 @@ class Parameter
{
$sResult = $this->sName.'=';
if ($bConvertSpecialsName && in_array(strtolower($this->sName), array(
strtolower(\MailSo\Mime\Enumerations\Parameter::NAME),
strtolower(\MailSo\Mime\Enumerations\Parameter::FILENAME)
strtolower(Enumerations\Parameter::NAME),
strtolower(Enumerations\Parameter::FILENAME)
)))
{
$sResult .= '"'.\MailSo\Base\Utils::EncodeUnencodedValue(

View file

@ -17,7 +17,7 @@ namespace MailSo\Mime;
*/
class ParameterCollection extends \MailSo\Base\Collection
{
protected function __construct(string $sRawParams = '')
function __construct(string $sRawParams = '')
{
parent::__construct();
@ -27,11 +27,6 @@ class ParameterCollection extends \MailSo\Base\Collection
}
}
public static function NewInstance(string $sRawParams = '') : self
{
return new self($sRawParams);
}
public function append($oParameter, bool $bToTop = false) : void
{
assert($oParameter instanceof Parameter);
@ -157,7 +152,7 @@ class ParameterCollection extends \MailSo\Base\Collection
$sCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
}
$this->append(Parameter::NewInstance($sName, $sResult));
$this->append(new Parameter($sName, $sResult));
}
}
}

View file

@ -35,7 +35,7 @@ class Part
public static $ForceCharset = '';
/**
* @var \MailSo\Mime\HeaderCollection
* @var HeaderCollection
*/
public $Headers;
@ -45,7 +45,7 @@ class Part
public $Body;
/**
* @var \MailSo\Mime\PartCollection
* @var PartCollection
*/
public $SubParts;
@ -69,24 +69,19 @@ class Part
*/
private $iParseBuffer;
private function __construct()
function __construct()
{
$this->iParseBuffer = \MailSo\Mime\Part::DEFAUL_BUFFER;
$this->iParseBuffer = self::DEFAUL_BUFFER;
$this->Reset();
}
public static function NewInstance() : self
{
return new self();
}
public function Reset() : self
{
\MailSo\Base\ResourceRegistry::CloseMemoryResource($this->Body);
$this->Body = null;
$this->Headers = HeaderCollection::NewInstance();
$this->SubParts = PartCollection::NewInstance();
$this->Headers = new HeaderCollection;
$this->SubParts = new PartCollection;
$this->LineParts = array();
$this->sBoundary = '';
$this->sParentCharset = \MailSo\Base\Enumerations\Charset::ISO_8859_1;
@ -128,41 +123,41 @@ class Part
public function HeaderCharset() : string
{
return ($this->Headers) ? trim(strtolower($this->Headers->ParameterValue(
\MailSo\Mime\Enumerations\Header::CONTENT_TYPE,
\MailSo\Mime\Enumerations\Parameter::CHARSET))) : '';
Enumerations\Header::CONTENT_TYPE,
Enumerations\Parameter::CHARSET))) : '';
}
public function HeaderBoundary() : string
{
return ($this->Headers) ? trim($this->Headers->ParameterValue(
\MailSo\Mime\Enumerations\Header::CONTENT_TYPE,
\MailSo\Mime\Enumerations\Parameter::BOUNDARY)) : '';
Enumerations\Header::CONTENT_TYPE,
Enumerations\Parameter::BOUNDARY)) : '';
}
public function ContentType() : string
{
return ($this->Headers) ?
trim(strtolower($this->Headers->ValueByName(
\MailSo\Mime\Enumerations\Header::CONTENT_TYPE))) : '';
Enumerations\Header::CONTENT_TYPE))) : '';
}
public function ContentTransferEncoding() : string
{
return ($this->Headers) ?
trim(strtolower($this->Headers->ValueByName(
\MailSo\Mime\Enumerations\Header::CONTENT_TRANSFER_ENCODING))) : '';
Enumerations\Header::CONTENT_TRANSFER_ENCODING))) : '';
}
public function ContentID() : string
{
return ($this->Headers) ? trim($this->Headers->ValueByName(
\MailSo\Mime\Enumerations\Header::CONTENT_ID)) : '';
Enumerations\Header::CONTENT_ID)) : '';
}
public function ContentLocation() : string
{
return ($this->Headers) ? trim($this->Headers->ValueByName(
\MailSo\Mime\Enumerations\Header::CONTENT_LOCATION)) : '';
Enumerations\Header::CONTENT_LOCATION)) : '';
}
public function IsFlowedFormat() : bool
@ -171,8 +166,8 @@ class Part
if ($this->Headers)
{
$bResult = 'flowed' === \trim(\strtolower($this->Headers->ParameterValue(
\MailSo\Mime\Enumerations\Header::CONTENT_TYPE,
\MailSo\Mime\Enumerations\Parameter::FORMAT)));
Enumerations\Header::CONTENT_TYPE,
Enumerations\Parameter::FORMAT)));
if ($bResult && \in_array(\strtolower($this->MailEncodingName()), array('base64', 'quoted-printable')))
{
@ -189,14 +184,14 @@ class Part
if ($this->Headers)
{
$sResult = trim($this->Headers->ParameterValue(
\MailSo\Mime\Enumerations\Header::CONTENT_DISPOSITION,
\MailSo\Mime\Enumerations\Parameter::FILENAME));
Enumerations\Header::CONTENT_DISPOSITION,
Enumerations\Parameter::FILENAME));
if (0 === strlen($sResult))
{
$sResult = trim($this->Headers->ParameterValue(
\MailSo\Mime\Enumerations\Header::CONTENT_TYPE,
\MailSo\Mime\Enumerations\Parameter::NAME));
Enumerations\Header::CONTENT_TYPE,
Enumerations\Parameter::NAME));
}
}
@ -245,7 +240,7 @@ class Part
{
$this->Reset();
$oParserClass = new \MailSo\Mime\Parser\ParserMemory();
$oParserClass = new Parser\ParserMemory;
$oMimePart = null;
$bIsOef = false;
@ -262,7 +257,7 @@ class Part
$sPrevBuffer, $sBuffer, $aBoundaryStack, $bIsOef);
$sFirstNotNullCharset = null;
foreach ($this->LineParts as /* @var $oMimePart \MailSo\Mime\Part */ $oMimePart)
foreach ($this->LineParts as /* @var $oMimePart Part */ $oMimePart)
{
$sCharset = $oMimePart->HeaderCharset();
if (0 < strlen($sCharset))
@ -275,7 +270,7 @@ class Part
$sForceCharset = self::$ForceCharset;
if (0 < strlen($sForceCharset))
{
foreach ($this->LineParts as /* @var $oMimePart \MailSo\Mime\Part */ $oMimePart)
foreach ($this->LineParts as /* @var $oMimePart Part */ $oMimePart)
{
$oMimePart->SetParentCharset($sForceCharset);
$oMimePart->Headers->SetParentCharset($sForceCharset);
@ -286,7 +281,7 @@ class Part
$sFirstNotNullCharset = (null !== $sFirstNotNullCharset)
? $sFirstNotNullCharset : self::$DefaultCharset;
foreach ($this->LineParts as /* @var $oMimePart \MailSo\Mime\Part */ $oMimePart)
foreach ($this->LineParts as /* @var $oMimePart Part */ $oMimePart)
{
$sHeaderCharset = $oMimePart->HeaderCharset();
$oMimePart->SetParentCharset((0 < strlen($sHeaderCharset)) ? $sHeaderCharset : $sFirstNotNullCharset);
@ -489,7 +484,7 @@ class Part
{
$iOffset = $iPos + $sBoundaryLen;
$oSubPart = self::NewInstance();
$oSubPart = new self;
$oSubPart
->SetParseBuffer($this->iParseBuffer)
@ -573,12 +568,12 @@ class Part
$aSubStreams = array(
$this->Headers->ToEncodedString().
\MailSo\Mime\Enumerations\Constants::CRLF.
\MailSo\Mime\Enumerations\Constants::CRLF,
Enumerations\Constants::CRLF.
Enumerations\Constants::CRLF,
null === $this->Body ? '' : $this->Body,
\MailSo\Mime\Enumerations\Constants::CRLF
Enumerations\Constants::CRLF
);
if (0 < $this->SubParts->Count())
@ -586,7 +581,7 @@ class Part
$sBoundary = $this->HeaderBoundary();
if (0 < strlen($sBoundary))
{
$aSubStreams[] = '--'.$sBoundary.\MailSo\Mime\Enumerations\Constants::CRLF;
$aSubStreams[] = '--'.$sBoundary.Enumerations\Constants::CRLF;
$rSubPartsStream = $this->SubParts->ToStream($sBoundary);
if (is_resource($rSubPartsStream))
@ -594,8 +589,8 @@ class Part
$aSubStreams[] = $rSubPartsStream;
}
$aSubStreams[] = \MailSo\Mime\Enumerations\Constants::CRLF.
'--'.$sBoundary.'--'.\MailSo\Mime\Enumerations\Constants::CRLF;
$aSubStreams[] = Enumerations\Constants::CRLF.
'--'.$sBoundary.'--'.Enumerations\Constants::CRLF;
}
}

View file

@ -17,11 +17,6 @@ namespace MailSo\Mime;
*/
class PartCollection extends \MailSo\Base\Collection
{
public static function NewInstance() : self
{
return new self();
}
public function append($oPart, bool $bToTop = false) : void
{
assert($oPart instanceof Part);