| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Mime; |
| 13 |
|
| 14 |
use Egulias\EmailValidator\EmailValidator; |
| 15 |
use Egulias\EmailValidator\Validation\MessageIDValidation; |
| 16 |
use Egulias\EmailValidator\Validation\RFCValidation; |
| 17 |
use Symfony\Component\Mime\Encoder\IdnAddressEncoder; |
| 18 |
use Symfony\Component\Mime\Exception\InvalidArgumentException; |
| 19 |
use Symfony\Component\Mime\Exception\LogicException; |
| 20 |
use Symfony\Component\Mime\Exception\RfcComplianceException; |
| 21 |
|
| 22 |
/** |
| 23 |
* @author Fabien Potencier <fabien@symfony.com> |
| 24 |
*/ |
| 25 |
final class Address |
| 26 |
{ |
| 27 |
/** |
| 28 |
* A regex that matches a structure like 'Name <email@address.com>'. |
| 29 |
* It matches anything between the first < and last > as email address. |
| 30 |
* This allows to use a single string to construct an Address, which can be convenient to use in |
| 31 |
* config, and allows to have more readable config. |
| 32 |
* This does not try to cover all edge cases for address. |
| 33 |
*/ |
| 34 |
private const FROM_STRING_PATTERN = '~(?<displayName>[^<]*)<(?<addrSpec>.*)>[^>]*~'; |
| 35 |
|
| 36 |
private static $validator; |
| 37 |
private static $encoder; |
| 38 |
|
| 39 |
private $address; |
| 40 |
private $name; |
| 41 |
|
| 42 |
public function __construct(string $address, string $name = '') |
| 43 |
{ |
| 44 |
if (!class_exists(EmailValidator::class)) { |
| 45 |
throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s"; try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class)); |
| 46 |
} |
| 47 |
|
| 48 |
if (null === self::$validator) { |
| 49 |
self::$validator = new EmailValidator(); |
| 50 |
} |
| 51 |
|
| 52 |
$this->address = trim($address); |
| 53 |
$this->name = trim(str_replace(["\n", "\r"], '', $name)); |
| 54 |
|
| 55 |
if (!self::$validator->isValid($this->address, class_exists(MessageIDValidation::class) ? new MessageIDValidation() : new RFCValidation())) { |
| 56 |
throw new RfcComplianceException(sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address)); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public function getAddress(): string |
| 61 |
{ |
| 62 |
return $this->address; |
| 63 |
} |
| 64 |
|
| 65 |
public function getName(): string |
| 66 |
{ |
| 67 |
return $this->name; |
| 68 |
} |
| 69 |
|
| 70 |
public function getEncodedAddress(): string |
| 71 |
{ |
| 72 |
if (null === self::$encoder) { |
| 73 |
self::$encoder = new IdnAddressEncoder(); |
| 74 |
} |
| 75 |
|
| 76 |
return self::$encoder->encodeString($this->address); |
| 77 |
} |
| 78 |
|
| 79 |
public function toString(): string |
| 80 |
{ |
| 81 |
return ($n = $this->getEncodedName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress(); |
| 82 |
} |
| 83 |
|
| 84 |
public function getEncodedName(): string |
| 85 |
{ |
| 86 |
if ('' === $this->getName()) { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
|
| 90 |
return sprintf('"%s"', preg_replace('/"/u', '\"', $this->getName())); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param Address|string $address |
| 95 |
*/ |
| 96 |
public static function create($address): self |
| 97 |
{ |
| 98 |
if ($address instanceof self) { |
| 99 |
return $address; |
| 100 |
} |
| 101 |
|
| 102 |
if (!\is_string($address)) { |
| 103 |
throw new InvalidArgumentException(sprintf('An address can be an instance of Address or a string ("%s" given).', get_debug_type($address))); |
| 104 |
} |
| 105 |
|
| 106 |
if (false === strpos($address, '<')) { |
| 107 |
return new self($address); |
| 108 |
} |
| 109 |
|
| 110 |
if (!preg_match(self::FROM_STRING_PATTERN, $address, $matches)) { |
| 111 |
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $address, self::class)); |
| 112 |
} |
| 113 |
|
| 114 |
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"')); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @param array<Address|string> $addresses |
| 119 |
* |
| 120 |
* @return Address[] |
| 121 |
*/ |
| 122 |
public static function createArray(array $addresses): array |
| 123 |
{ |
| 124 |
$addrs = []; |
| 125 |
foreach ($addresses as $address) { |
| 126 |
$addrs[] = self::create($address); |
| 127 |
} |
| 128 |
|
| 129 |
return $addrs; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @deprecated since Symfony 5.2, use "create()" instead. |
| 134 |
*/ |
| 135 |
public static function fromString(string $string): self |
| 136 |
{ |
| 137 |
trigger_deprecation('symfony/mime', '5.2', '"%s()" is deprecated, use "%s::create()" instead.', __METHOD__, __CLASS__); |
| 138 |
|
| 139 |
if (!str_contains($string, '<')) { |
| 140 |
return new self($string, ''); |
| 141 |
} |
| 142 |
|
| 143 |
if (!preg_match(self::FROM_STRING_PATTERN, $string, $matches)) { |
| 144 |
throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $string, self::class)); |
| 145 |
} |
| 146 |
|
| 147 |
return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"')); |
| 148 |
} |
| 149 |
} |
| 150 |
|