Skip to content

Commit

Permalink
Fix to allow empty address groups #241
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Aug 10, 2024
1 parent 48cf726 commit e0d4423
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Header/Consumer/AddressGroupConsumerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace ZBateson\MailMimeParser\Header\Consumer;

use Psr\Log\LoggerInterface;
use Iterator;
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart;
use ZBateson\MailMimeParser\Header\Part\HeaderPartFactory;

Expand Down Expand Up @@ -72,6 +73,22 @@ protected function isStartToken(string $token) : bool
return ($token === ':');
}

/**
* Overridden to always call processParts even for an empty set of
* addresses, since a group could be empty.
*
* @param Iterator $tokens
* @return IHeaderPart[]
*/
protected function parseTokensIntoParts(Iterator $tokens) : array
{
$ret = parent::parseTokensIntoParts($tokens);
if ($ret === []) {
return $this->processParts([]);
}
return $ret;
}

/**
* Performs post-processing on parsed parts.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/MailMimeParser/Header/AddressHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,18 @@ public function testGetGroups() : void
$this->assertSame($parts[0], $groups[0]);
$this->assertSame($parts[1], $groups[1]);
}

public function testEmptyAddressGroup() : void
{
$header = $this->newAddressHeader(
'Cc',
'House Stark:;'
);
$parts = $header->getParts();
$this->assertCount(1, $parts);

$starks = $parts[0];
$this->assertEquals('House Stark', $starks->getName());
$this->assertCount(0, $starks->getAddresses());
}
}

0 comments on commit e0d4423

Please sign in to comment.