Address.php 3.7 KB

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