Skip to content

Commit

Permalink
Add Address::fromString
Browse files Browse the repository at this point in the history
This will allow to create an Address from a string such as 'Name <name@example.com>'
  • Loading branch information
gisostallenberg committed Aug 22, 2019
1 parent 153e081 commit 75ea8d0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Symfony/Component/Mime/Address.php
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
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
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',
],
];
}
}

0 comments on commit 75ea8d0

Please sign in to comment.