Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IdentificationHeader.php 2.4KB

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