MailboxListHeader.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Header;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\Mime\Exception\RfcComplianceException;
  13. /**
  14. * A Mailbox list MIME Header for something like From, To, Cc, and Bcc (one or more named addresses).
  15. *
  16. * @author Chris Corbyn
  17. */
  18. final class MailboxListHeader extends AbstractHeader
  19. {
  20. private $addresses = [];
  21. /**
  22. * @param Address[] $addresses
  23. */
  24. public function __construct(string $name, array $addresses)
  25. {
  26. parent::__construct($name);
  27. $this->setAddresses($addresses);
  28. }
  29. /**
  30. * @param Address[] $body
  31. *
  32. * @throws RfcComplianceException
  33. */
  34. public function setBody($body)
  35. {
  36. $this->setAddresses($body);
  37. }
  38. /**
  39. * @throws RfcComplianceException
  40. *
  41. * @return Address[]
  42. */
  43. public function getBody(): array
  44. {
  45. return $this->getAddresses();
  46. }
  47. /**
  48. * Sets a list of addresses to be shown in this Header.
  49. *
  50. * @param Address[] $addresses
  51. *
  52. * @throws RfcComplianceException
  53. */
  54. public function setAddresses(array $addresses)
  55. {
  56. $this->addresses = [];
  57. $this->addAddresses($addresses);
  58. }
  59. /**
  60. * Sets a list of addresses to be shown in this Header.
  61. *
  62. * @param Address[] $addresses
  63. *
  64. * @throws RfcComplianceException
  65. */
  66. public function addAddresses(array $addresses)
  67. {
  68. foreach ($addresses as $address) {
  69. $this->addAddress($address);
  70. }
  71. }
  72. /**
  73. * @throws RfcComplianceException
  74. */
  75. public function addAddress(Address $address)
  76. {
  77. $this->addresses[] = $address;
  78. }
  79. /**
  80. * @return Address[]
  81. */
  82. public function getAddresses(): array
  83. {
  84. return $this->addresses;
  85. }
  86. /**
  87. * Gets the full mailbox list of this Header as an array of valid RFC 2822 strings.
  88. *
  89. * @throws RfcComplianceException
  90. *
  91. * @return string[]
  92. */
  93. public function getAddressStrings(): array
  94. {
  95. $strings = [];
  96. foreach ($this->addresses as $address) {
  97. $str = $address->getEncodedAddress();
  98. if ($name = $address->getName()) {
  99. $str = $this->createPhrase($this, $name, $this->getCharset(), !$strings).' <'.$str.'>';
  100. }
  101. $strings[] = $str;
  102. }
  103. return $strings;
  104. }
  105. public function getBodyAsString(): string
  106. {
  107. return implode(', ', $this->getAddressStrings());
  108. }
  109. /**
  110. * Redefine the encoding requirements for addresses.
  111. *
  112. * All "specials" must be encoded as the full header value will not be quoted
  113. *
  114. * @see RFC 2822 3.2.1
  115. */
  116. protected function tokenNeedsEncoding(string $token): bool
  117. {
  118. return preg_match('/[()<>\[\]:;@\,."]/', $token) || parent::tokenNeedsEncoding($token);
  119. }
  120. }