Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Address.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @experimental in 4.3
  21. */
  22. class Address
  23. {
  24. private static $validator;
  25. private static $encoder;
  26. private $address;
  27. public function __construct(string $address)
  28. {
  29. if (!class_exists(EmailValidator::class)) {
  30. throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s"; try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
  31. }
  32. if (null === self::$validator) {
  33. self::$validator = new EmailValidator();
  34. }
  35. if (!self::$validator->isValid($address, new RFCValidation())) {
  36. throw new RfcComplianceException(sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address));
  37. }
  38. $this->address = $address;
  39. }
  40. public function getAddress(): string
  41. {
  42. return $this->address;
  43. }
  44. public function getEncodedAddress(): string
  45. {
  46. if (null === self::$encoder) {
  47. self::$encoder = new IdnAddressEncoder();
  48. }
  49. return self::$encoder->encodeString($this->address);
  50. }
  51. public function toString(): string
  52. {
  53. return $this->getEncodedAddress();
  54. }
  55. /**
  56. * @param Address|string $address
  57. */
  58. public static function create($address): self
  59. {
  60. if ($address instanceof self) {
  61. return $address;
  62. }
  63. if (\is_string($address)) {
  64. return new self($address);
  65. }
  66. 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)));
  67. }
  68. /**
  69. * @param (Address|string)[] $addresses
  70. *
  71. * @return Address[]
  72. */
  73. public static function createArray(array $addresses): array
  74. {
  75. $addrs = [];
  76. foreach ($addresses as $address) {
  77. $addrs[] = self::create($address);
  78. }
  79. return $addrs;
  80. }
  81. }