IdentificationHeader.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * An ID MIME Header for something like Message-ID or Content-ID (one or more addresses).
  15. *
  16. * @author Chris Corbyn
  17. */
  18. final class IdentificationHeader extends AbstractHeader
  19. {
  20. private $ids = [];
  21. private $idsAsAddresses = [];
  22. /**
  23. * @param string|array $ids
  24. */
  25. public function __construct(string $name, $ids)
  26. {
  27. parent::__construct($name);
  28. $this->setId($ids);
  29. }
  30. /**
  31. * @param string|array $body a string ID or an array of IDs
  32. *
  33. * @throws RfcComplianceException
  34. */
  35. public function setBody($body)
  36. {
  37. $this->setId($body);
  38. }
  39. public function getBody(): array
  40. {
  41. return $this->getIds();
  42. }
  43. /**
  44. * Set the ID used in the value of this header.
  45. *
  46. * @param string|array $id
  47. *
  48. * @throws RfcComplianceException
  49. */
  50. public function setId($id)
  51. {
  52. $this->setIds(\is_array($id) ? $id : [$id]);
  53. }
  54. /**
  55. * Get the ID used in the value of this Header.
  56. *
  57. * If multiple IDs are set only the first is returned.
  58. */
  59. public function getId(): ?string
  60. {
  61. return $this->ids[0] ?? null;
  62. }
  63. /**
  64. * Set a collection of IDs to use in the value of this Header.
  65. *
  66. * @param string[] $ids
  67. *
  68. * @throws RfcComplianceException
  69. */
  70. public function setIds(array $ids)
  71. {
  72. $this->ids = [];
  73. $this->idsAsAddresses = [];
  74. foreach ($ids as $id) {
  75. $this->idsAsAddresses[] = new Address($id);
  76. $this->ids[] = $id;
  77. }
  78. }
  79. /**
  80. * Get the list of IDs used in this Header.
  81. *
  82. * @return string[]
  83. */
  84. public function getIds(): array
  85. {
  86. return $this->ids;
  87. }
  88. public function getBodyAsString(): string
  89. {
  90. $addrs = [];
  91. foreach ($this->idsAsAddresses as $address) {
  92. $addrs[] = '<'.$address->toString().'>';
  93. }
  94. return implode(' ', $addrs);
  95. }
  96. }