MailboxHeader.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 MIME Header for something like Sender (one named address).
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. final class MailboxHeader extends AbstractHeader
  19. {
  20. private $address;
  21. public function __construct(string $name, Address $address)
  22. {
  23. parent::__construct($name);
  24. $this->setAddress($address);
  25. }
  26. /**
  27. * @param Address $body
  28. *
  29. * @throws RfcComplianceException
  30. */
  31. public function setBody($body)
  32. {
  33. $this->setAddress($body);
  34. }
  35. /**
  36. * @throws RfcComplianceException
  37. */
  38. public function getBody(): Address
  39. {
  40. return $this->getAddress();
  41. }
  42. /**
  43. * @throws RfcComplianceException
  44. */
  45. public function setAddress(Address $address)
  46. {
  47. $this->address = $address;
  48. }
  49. public function getAddress(): Address
  50. {
  51. return $this->address;
  52. }
  53. public function getBodyAsString(): string
  54. {
  55. $str = $this->address->getEncodedAddress();
  56. if ($name = $this->address->getName()) {
  57. $str = $this->createPhrase($this, $name, $this->getCharset(), true).' <'.$str.'>';
  58. }
  59. return $str;
  60. }
  61. /**
  62. * Redefine the encoding requirements for an address.
  63. *
  64. * All "specials" must be encoded as the full header value will not be quoted
  65. *
  66. * @see RFC 2822 3.2.1
  67. */
  68. protected function tokenNeedsEncoding(string $token): bool
  69. {
  70. return preg_match('/[()<>\[\]:;@\,."]/', $token) || parent::tokenNeedsEncoding($token);
  71. }
  72. }