feature #33091 [Mime] Add Address::fromString (gisostallenberg)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mime] Add Address::fromString

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #33086
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/12128

This will allow to create a Address from a string such as 'Name <name@example.com>'
Example:
```php
$address = Address::fromString("Name <name@example.com>");
```

Commits
-------

75ea8d0d67 Add Address::fromString
This commit is contained in:
Fabien Potencier 2019-08-22 11:13:11 +02:00
commit 8c2e128a1a
3 changed files with 97 additions and 0 deletions

View File

@ -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'], ' \'"'));
}
}

View File

@ -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
-----

View File

@ -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',
],
];
}
}