Open Journal Systems  3.3.0
Address.php
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 
13 
14 use Egulias\EmailValidator\EmailValidator;
15 use Egulias\EmailValidator\Validation\RFCValidation;
20 
24 final class Address
25 {
33  private const FROM_STRING_PATTERN = '~(?<displayName>[^<]*)<(?<addrSpec>.*)>[^>]*~';
34 
35  private static $validator;
36  private static $encoder;
37 
38  private $address;
39  private $name;
40 
41  public function __construct(string $address, string $name = '')
42  {
43  if (!class_exists(EmailValidator::class)) {
44  throw new LogicException(sprintf('The "%s" class cannot be used as it needs "%s"; try running "composer require egulias/email-validator".', __CLASS__, EmailValidator::class));
45  }
46 
47  if (null === self::$validator) {
48  self::$validator = new EmailValidator();
49  }
50 
51  $this->address = trim($address);
52  $this->name = trim(str_replace(["\n", "\r"], '', $name));
53 
54  if (!self::$validator->isValid($this->address, new RFCValidation())) {
55  throw new RfcComplianceException(sprintf('Email "%s" does not comply with addr-spec of RFC 2822.', $address));
56  }
57  }
58 
59  public function getAddress(): string
60  {
61  return $this->address;
62  }
63 
64  public function getName(): string
65  {
66  return $this->name;
67  }
68 
69  public function getEncodedAddress(): string
70  {
71  if (null === self::$encoder) {
72  self::$encoder = new IdnAddressEncoder();
73  }
74 
75  return self::$encoder->encodeString($this->address);
76  }
77 
78  public function toString(): string
79  {
80  return ($n = $this->getName()) ? $n.' <'.$this->getEncodedAddress().'>' : $this->getEncodedAddress();
81  }
82 
86  public static function create($address): self
87  {
88  if ($address instanceof self) {
89  return $address;
90  }
91  if (\is_string($address)) {
92  return new self($address);
93  }
94 
95  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)));
96  }
97 
103  public static function createArray(array $addresses): array
104  {
105  $addrs = [];
106  foreach ($addresses as $address) {
107  $addrs[] = self::create($address);
108  }
109 
110  return $addrs;
111  }
112 
113  public static function fromString(string $string): self
114  {
115  if (false === strpos($string, '<')) {
116  return new self($string, '');
117  }
118 
119  if (!preg_match(self::FROM_STRING_PATTERN, $string, $matches)) {
120  throw new InvalidArgumentException(sprintf('Could not parse "%s" to a "%s" instance.', $string, static::class));
121  }
122 
123  return new self($matches['addrSpec'], trim($matches['displayName'], ' \'"'));
124  }
125 }
Symfony\Component\Mime\Encoder\IdnAddressEncoder
Definition: IdnAddressEncoder.php:27
Symfony\Component\Mime\Address\create
static create($address)
Definition: Address.php:86
Symfony\Component\Mime
Definition: Address.php:12
Symfony\Component\Mime\Address\__construct
__construct(string $address, string $name='')
Definition: Address.php:41
Symfony\Component\Mime\Address\getName
getName()
Definition: Address.php:64
Symfony\Component\Mime\Address\toString
toString()
Definition: Address.php:78
Symfony\Component\Mime\Address
Definition: Address.php:24
Symfony\Component\Mime\Exception\RfcComplianceException
Definition: RfcComplianceException.php:17
Symfony\Component\Mime\Exception\InvalidArgumentException
Definition: vendor/symfony/mime/Exception/InvalidArgumentException.php:17
Symfony\Component\Mime\Exception\LogicException
Definition: LogicException.php:17
Symfony\Component\Mime\Address\createArray
static createArray(array $addresses)
Definition: Address.php:103
Symfony\Component\Mime\Address\getAddress
getAddress()
Definition: Address.php:59
Symfony\Component\Mime\Address\getEncodedAddress
getEncodedAddress()
Definition: Address.php:69
Symfony\Component\Mime\Address\fromString
static fromString(string $string)
Definition: Address.php:113