Cleanup & Speedup *Collection objects

This commit is contained in:
djmaze 2020-03-19 15:21:00 +01:00
parent f6fdb69c65
commit ebfde6e360
12 changed files with 169 additions and 209 deletions

View file

@ -17,43 +17,49 @@ namespace MailSo\Mime;
*/
class AttachmentCollection extends \MailSo\Base\Collection
{
/**
* @access protected
*/
protected function __construct()
{
parent::__construct();
}
public static function NewInstance() : \MailSo\Mime\AttachmentCollection
public static function NewInstance() : self
{
return new self();
}
public function append(Attachment $oFolder, bool $bToTop = false) : void
{
parent::append($oFolder, $bToTop);
}
public function LinkedAttachments() : array
{
return $this->FilterList(function ($oItem) {
return $oItem && $oItem->IsLinked();
});
$aResult = array();
foreach ($this as $oAttachment) {
if ($oAttachment->IsLinked()) {
$aResult[] = $oAttachment;
}
}
return $aResult;
}
public function UnlinkedAttachments() : array
{
return $this->FilterList(function ($oItem) {
return $oItem && !$oItem->IsLinked();
});
$aResult = array();
foreach ($this as $oAttachment) {
if (!$oAttachment->IsLinked()) {
$aResult[] = $oAttachment;
}
}
return $aResult;
}
public function SizeOfAttachments() : int
{
$iResult = 0;
$this->ForeachList(function ($oItem) use (&$iResult) {
if ($oItem)
{
$iResult += $oItem->FileSize();
}
});
foreach ($this as $oAttachment) {
$iResult += $oAttachment->FileSize();
}
return $iResult;
}
}

View file

@ -33,6 +33,11 @@ class EmailCollection extends \MailSo\Base\Collection
return new self($sEmailAddresses);
}
public function append(Email $oEmail, bool $bToTop = false) : void
{
parent::append($oEmail, $bToTop);
}
public static function Parse(string $sEmailAddresses) : self
{
return self::NewInstance($sEmailAddresses);
@ -41,7 +46,7 @@ class EmailCollection extends \MailSo\Base\Collection
public function ToArray() : array
{
$aReturn = array();
foreach ($this as /* @var $oEmail \MailSo\Mime\Email */ $oEmail)
foreach ($this as $oEmail)
{
$aReturn[] = $oEmail->ToArray();
}
@ -51,7 +56,7 @@ class EmailCollection extends \MailSo\Base\Collection
public function MergeWithOtherCollection(EmailCollection $oEmails) : self
{
foreach ($oEmails as /* @var $oEmail \MailSo\Mime\Email */ $oEmail)
foreach ($oEmails as $oEmail)
{
$this->append($oEmail);
}
@ -63,7 +68,7 @@ class EmailCollection extends \MailSo\Base\Collection
{
$aReturn = array();
foreach ($this as /* @var $oEmail \MailSo\Mime\Email */ $oEmail)
foreach ($this as $oEmail)
{
$sEmail = $oEmail->GetEmail();
if (!isset($aReturn[$sEmail]))
@ -80,7 +85,7 @@ class EmailCollection extends \MailSo\Base\Collection
public function ToString(bool $bConvertSpecialsName = false, bool $bIdn = false) : string
{
$aReturn = array();
foreach ($this as /* @var $oEmail \MailSo\Mime\Email */ $oEmail)
foreach ($this as $oEmail)
{
$aReturn[] = $oEmail->ToString($bConvertSpecialsName, $bIdn);
}
@ -169,7 +174,7 @@ class EmailCollection extends \MailSo\Base\Collection
try
{
$this->append(
\MailSo\Mime\Email::Parse(\substr($sWorkingRecipients, $iEmailStartPos, $iEmailEndPos - $iEmailStartPos))
Email::Parse(\substr($sWorkingRecipients, $iEmailStartPos, $iEmailEndPos - $iEmailStartPos))
);
$iEmailStartPos = $iCurrentPos + 1;
@ -189,7 +194,7 @@ class EmailCollection extends \MailSo\Base\Collection
try
{
$this->append(
\MailSo\Mime\Email::Parse(\substr($sWorkingRecipients, $iEmailStartPos, $iCurrentPos - $iEmailStartPos))
Email::Parse(\substr($sWorkingRecipients, $iEmailStartPos, $iCurrentPos - $iEmailStartPos))
);
}
catch (\MailSo\Base\Exceptions\InvalidArgumentException $oException) {}

View file

@ -18,22 +18,18 @@ namespace MailSo\Mime;
class HeaderCollection extends \MailSo\Base\Collection
{
protected $sRawHeaders;
protected $sRawHeaders = '';
/**
* @var string
*/
protected $sParentCharset;
protected $sParentCharset = '';
protected function __construct(string $sRawHeaders = '', bool $bStoreRawHeaders = true)
{
parent::__construct();
$this->sRawHeaders = '';
$this->sParentCharset = '';
if (0 < \strlen($sRawHeaders))
{
if (0 < \strlen($sRawHeaders)) {
$this->Parse($sRawHeaders, $bStoreRawHeaders);
}
}
@ -43,6 +39,11 @@ class HeaderCollection extends \MailSo\Base\Collection
return new self($sRawHeaders, $bStoreRawHeaders);
}
public function append(Header $oHeader, bool $bToTop = false) : void
{
parent::append($oHeader, $bToTop);
}
public function AddByName(string $sName, string $sValue, bool $bToTop = false) : self
{
$this->append(Header::NewInstance($sName, $sValue), $bToTop);
@ -63,16 +64,12 @@ class HeaderCollection extends \MailSo\Base\Collection
public function ValuesByName(string $sHeaderName, bool $bCharsetAutoDetect = false) : array
{
$aResult = array();
$sHeaderNameLower = \strtolower($sHeaderName);
foreach ($this as /* @var $oHeader \MailSo\Mime\Header */ $oHeader)
{
if ($sHeaderNameLower === \strtolower($oHeader->Name()))
{
foreach ($this as $oHeader) {
if ($sHeaderNameLower === \strtolower($oHeader->Name())) {
$aResult[] = $bCharsetAutoDetect ? $oHeader->ValueWithCharsetAutoDetect() : $oHeader->Value();
}
}
return $aResult;
}
@ -85,19 +82,17 @@ class HeaderCollection extends \MailSo\Base\Collection
return $this;
}
public function GetAsEmailCollection(string $sHeaderName, bool $bCharsetAutoDetect = false) : ?\MailSo\Mime\EmailCollection
public function GetAsEmailCollection(string $sHeaderName, bool $bCharsetAutoDetect = false) : ?EmailCollection
{
$oResult = null;
$sValue = $this->ValueByName($sHeaderName, $bCharsetAutoDetect);
if (0 < \strlen($sValue))
{
$oResult = \MailSo\Mime\EmailCollection::NewInstance($sValue);
if (0 < \strlen($sValue)) {
$oResult = EmailCollection::NewInstance($sValue);
}
return $oResult && 0 < $oResult->Count() ? $oResult : null;
}
public function ParametersByName(string $sHeaderName) : ?\MailSo\Mime\ParameterCollection
public function ParametersByName(string $sHeaderName) : ?ParameterCollection
{
$oHeader = $this->GetByName($sHeaderName);
return $oHeader ? $oHeader->Parameters() : null;
@ -109,35 +104,27 @@ class HeaderCollection extends \MailSo\Base\Collection
return (null !== $oParameters) ? $oParameters->ParameterValueByName($sParamName) : '';
}
public function GetByName(string $sHeaderName) : ?\MailSo\Mime\Header
public function GetByName(string $sHeaderName) : ?Header
{
$sHeaderNameLower = \strtolower($sHeaderName);
foreach ($this as /* @var $oHeader \MailSo\Mime\Header */ $oHeader)
{
if ($sHeaderNameLower === \strtolower($oHeader->Name()))
{
foreach ($this as $oHeader) {
if ($sHeaderNameLower === \strtolower($oHeader->Name())) {
return $oHeader;
}
}
return null;
}
public function SetParentCharset(string $sParentCharset) : self
{
if (0 < \strlen($sParentCharset))
{
if ($this->sParentCharset !== $sParentCharset)
{
foreach ($this as /* @var $oHeader \MailSo\Mime\Header */ $oHeader)
{
if (0 < \strlen($sParentCharset)){
if ($this->sParentCharset !== $sParentCharset) {
foreach ($this as $oHeader) {
$oHeader->SetParentCharset($sParentCharset);
}
$this->sParentCharset = $sParentCharset;
}
}
return $this;
}
@ -151,13 +138,11 @@ class HeaderCollection extends \MailSo\Base\Collection
{
$this->Clear();
if ($bStoreRawHeaders)
{
if ($bStoreRawHeaders) {
$this->sRawHeaders = $sRawHeaders;
}
if (0 === \strlen($this->sParentCharset))
{
if (0 === \strlen($this->sParentCharset)) {
$this->sParentCharset = $sParentCharset;
}
@ -165,10 +150,8 @@ class HeaderCollection extends \MailSo\Base\Collection
$sName = null;
$sValue = null;
foreach ($aHeaders as $sHeadersValue)
{
if (0 === strlen($sHeadersValue))
{
foreach ($aHeaders as $sHeadersValue) {
if (0 === strlen($sHeadersValue)) {
continue;
}
@ -241,7 +224,7 @@ class HeaderCollection extends \MailSo\Base\Collection
{
$aResult = array();
$aHeaders = $this->ValuesByName(\MailSo\Mime\Enumerations\Header::AUTHENTICATION_RESULTS);
$aHeaders = $this->ValuesByName(Enumerations\Header::AUTHENTICATION_RESULTS);
if (\is_array($aHeaders) && 0 < \count($aHeaders))
{
foreach ($aHeaders as $sHeaderValue)
@ -280,7 +263,7 @@ class HeaderCollection extends \MailSo\Base\Collection
else
{
// X-DKIM-Authentication-Results: signer="hostinger.com" status="pass"
$aHeaders = $this->ValuesByName(\MailSo\Mime\Enumerations\Header::X_DKIM_AUTHENTICATION_RESULTS);
$aHeaders = $this->ValuesByName(Enumerations\Header::X_DKIM_AUTHENTICATION_RESULTS);
if (\is_array($aHeaders) && 0 < \count($aHeaders))
{
foreach ($aHeaders as $sHeaderValue)
@ -313,27 +296,19 @@ class HeaderCollection extends \MailSo\Base\Collection
return $aResult;
}
public function PopulateEmailColectionByDkim($oEmails) : void
public function PopulateEmailColectionByDkim(EmailCollection $oEmails) : void
{
if ($oEmails && $oEmails instanceof \MailSo\Mime\EmailCollection)
{
$aDkimStatuses = $this->DkimStatuses();
if (\is_array($aDkimStatuses) && 0 < \count($aDkimStatuses))
{
$oEmails->ForeachList(function (/* @var $oItem \MailSo\Mime\Email */ $oItem) use ($aDkimStatuses) {
if ($oItem && $oItem instanceof \MailSo\Mime\Email)
$aDkimStatuses = $this->DkimStatuses();
if (\is_array($aDkimStatuses) && 0 < \count($aDkimStatuses)) {
foreach ($oEmails as $oEmail) {
$sEmail = $oEmail->GetEmail();
foreach ($aDkimStatuses as $aDkimData) {
if (isset($aDkimData[0], $aDkimData[1]) &&
$aDkimData[1] === \strstr($sEmail, $aDkimData[1]))
{
$sEmail = $oItem->GetEmail();
foreach ($aDkimStatuses as $aDkimData)
{
if (isset($aDkimData[0], $aDkimData[1]) &&
$aDkimData[1] === \strstr($sEmail, $aDkimData[1]))
{
$oItem->SetDkimStatusAndValue($aDkimData[0], empty($aDkimData[2]) ? '' : $aDkimData[2]);
}
}
$oEmail->SetDkimStatusAndValue($aDkimData[0], empty($aDkimData[2]) ? '' : $aDkimData[2]);
}
});
}
}
}
}
@ -341,11 +316,9 @@ class HeaderCollection extends \MailSo\Base\Collection
public function ToEncodedString() : string
{
$aResult = array();
foreach ($this as /* @var $oHeader \MailSo\Mime\Header */ $oHeader)
{
foreach ($this as $oHeader) {
$aResult[] = $oHeader->EncodedValue();
}
return \implode(\MailSo\Mime\Enumerations\Constants::CRLF, $aResult);
return \implode(Enumerations\Constants::CRLF, $aResult);
}
}

View file

@ -32,11 +32,16 @@ class ParameterCollection extends \MailSo\Base\Collection
return new self($sRawParams);
}
public function append(Parameter $oHeader, bool $bToTop = false) : void
{
parent::append($oHeader, $bToTop);
}
public function ParameterValueByName(string $sName) : string
{
$sName = \strtolower(\trim($sName));
foreach ($this as /* @var $oParam \MailSo\Mime\ParameterCollection */ $oParam)
foreach ($this as $oParam)
{
if ($sName === \strtolower($oParam->Name()))
{
@ -66,7 +71,7 @@ class ParameterCollection extends \MailSo\Base\Collection
public function ToString(bool $bConvertSpecialsName = false) : string
{
$aResult = array();
foreach ($this as /* @var $oParam \MailSo\Mime\Parameter */ $oParam)
foreach ($this as $oParam)
{
$sLine = $oParam->ToString($bConvertSpecialsName);
if (0 < \strlen($sLine))
@ -86,7 +91,7 @@ class ParameterCollection extends \MailSo\Base\Collection
$this->Clear();
$aPreParams = array();
foreach ($aDataToReParse as /* @var $oParam \MailSo\Mime\Parameter */ $oParam)
foreach ($aDataToReParse as $oParam)
{
$aMatch = array();
$sParamName = $oParam->Name();

View file

@ -27,6 +27,11 @@ class PartCollection extends \MailSo\Base\Collection
return new self();
}
public function append(Part $oPart, bool $bToTop = false) : void
{
parent::append($oPart, $bToTop);
}
/**
* @return resource
*/
@ -37,12 +42,12 @@ class PartCollection extends \MailSo\Base\Collection
{
$aResult = array();
foreach ($this as /* @var $oPart \MailSo\Mime\Part */ $oPart)
foreach ($this as $oPart)
{
if (0 < count($aResult))
{
$aResult[] = \MailSo\Mime\Enumerations\Constants::CRLF.
'--'.$sBoundary.\MailSo\Mime\Enumerations\Constants::CRLF;
$aResult[] = Enumerations\Constants::CRLF.
'--'.$sBoundary.Enumerations\Constants::CRLF;
}
$aResult[] = $oPart->ToStream();