Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Handle simple comments in address lists #12

Merged
merged 1 commit into from
Sep 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Header/AbstractAddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function fromString($headerLine)
$values,
function (&$value) {
$value = trim($value);
$value = self::stripComments($value);
}
);

Expand Down Expand Up @@ -155,4 +156,19 @@ public function toString()
$value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED);
return (empty($value)) ? '' : sprintf('%s: %s', $name, $value);
}

// Supposed to be private, protected as a workaround for PHP bug 68194
protected static function stripComments($value)
{
return preg_replace(
'/\\(
(
\\\\.|
[^\\\\)]
)+
\\)/x',
'',
$value
);
}
}
20 changes: 20 additions & 0 deletions test/Header/AddressListHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ public function getStringHeadersWithNoWhitespaceSeparator()
];
}

/**
* @dataProvider getHeadersWithComments
*/
public function testDeserializationFromStringWithComments($value)
{
$header = From::fromString($value);
$list = $header->getAddressList();
$this->assertEquals(1, count($list));
$this->assertTrue($list->has('user@example.com'));
}

public function getHeadersWithComments()
{
return [
['From: user@example.com (Comment)'],
['From: user@example.com (Comm\\)ent)'],
['From: (Comment\\\\)user@example.com(Another)'],
];
}

/**
* @group 3789
* @dataProvider getStringHeadersWithNoWhitespaceSeparator
Expand Down