Add Address::fromString
This will allow to create an Address from a string such as 'Name <name@example.com>'
This commit is contained in:
parent
153e081119
commit
75ea8d0d67
@ -23,6 +23,15 @@ use Symfony\Component\Mime\Exception\RfcComplianceException;
|
||||
*/
|
||||
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;
|
||||
|
||||
@ -100,4 +109,15 @@ final class Address
|
||||
|
||||
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'], ' \'"'));
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
-----
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\Mime\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Mime\Exception\InvalidArgumentException;
|
||||
|
||||
class AddressTest extends TestCase
|
||||
{
|
||||
@ -77,4 +78,79 @@ class AddressTest extends TestCase
|
||||
{
|
||||
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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user