Skip to content

[Mime] Add Address::fromString #33091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2019
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
20 changes: 20 additions & 0 deletions src/Symfony/Component/Mime/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
*/
final class Address
{
/**
* A regex that matches a structure like 'Name <email@address.com>'.
* It matches anything between the first < and last > as email address.
* This allows to use a single string to construct an Address, which can be convenient to use in
* config, and allows to have more readable config.
* This does not try to cover all edge cases for address.
*/
private const FROM_STRING_PATTERN = '~(?<displayName>[^<]*)<(?<addrSpec>.*)>[^>]*~';

private static $validator;
private static $encoder;

Expand Down Expand Up @@ -100,4 +109,15 @@ public static function createArray(array $addresses): array

return $addrs;
}

public static function fromString(string $string): self
{
if (false === strpos($string, '<')) {
return new self($string, '');
}
if (!preg_match(self::FROM_STRING_PATTERN, $string, $matches)) {
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $string, static::class));
}
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Mime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* [BC BREAK] Removed `NamedAddress` (`Address` now supports a name)
* Added PHPUnit constraints
* Added `AbstractPart::asDebugString()`
* Added `Address::fromString()`

4.3.3
-----
Expand Down
76 changes: 76 additions & 0 deletions src/Symfony/Component/Mime/Tests/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Exception\InvalidArgumentException;

class AddressTest extends TestCase
{
Expand Down Expand Up @@ -77,4 +78,79 @@ public function nameEmptyDataProvider(): array
{
return [[''], [' '], [" \r\n "]];
}

/**
* @dataProvider fromStringProvider
*/
public function testFromString($string, $displayName, $addrSpec)
{
$address = Address::fromString($string);
$this->assertEquals($displayName, $address->getName());
$this->assertEquals($addrSpec, $address->getAddress());
$fromToStringAddress = Address::fromString($address->toString());
$this->assertEquals($displayName, $fromToStringAddress->getName());
$this->assertEquals($addrSpec, $fromToStringAddress->getAddress());
}

public function testFromStringFailure()
{
$this->expectException(InvalidArgumentException::class);
Address::fromString('Jane Doe <example@example.com');
}

public function fromStringProvider()
{
return [
[
'example@example.com',
'',
'example@example.com',
],
[
'<example@example.com>',
'',
'example@example.com',
],
[
'Jane Doe <example@example.com>',
'Jane Doe',
'example@example.com',
],
[
'Jane Doe<example@example.com>',
'Jane Doe',
'example@example.com',
],
[
'\'Jane Doe\' <example@example.com>',
'Jane Doe',
'example@example.com',
],
[
'"Jane Doe" <example@example.com>',
'Jane Doe',
'example@example.com',
],
[
'Jane Doe <"ex<ample"@example.com>',
'Jane Doe',
'"ex<ample"@example.com',
],
[
'Jane Doe <"ex<amp>le"@example.com>',
'Jane Doe',
'"ex<amp>le"@example.com',
],
[
'Jane Doe > <"ex<am p>le"@example.com>',
'Jane Doe >',
'"ex<am p>le"@example.com',
],
[
'Jane Doe <example@example.com>discarded',
'Jane Doe',
'example@example.com',
],
];
}
}