IdentificationHeader.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. use Egulias\EmailValidator\EmailValidator;
  10. use Egulias\EmailValidator\Validation\MessageIDValidation;
  11. use Egulias\EmailValidator\Validation\RFCValidation;
  12. /**
  13. * An ID MIME Header for something like Message-ID or Content-ID.
  14. *
  15. * @author Chris Corbyn
  16. */
  17. class Swift_Mime_Headers_IdentificationHeader extends Swift_Mime_Headers_AbstractHeader
  18. {
  19. /**
  20. * The IDs used in the value of this Header.
  21. *
  22. * This may hold multiple IDs or just a single ID.
  23. *
  24. * @var string[]
  25. */
  26. private $ids = [];
  27. /**
  28. * The strict EmailValidator.
  29. *
  30. * @var EmailValidator
  31. */
  32. private $emailValidator;
  33. private $addressEncoder;
  34. /**
  35. * Creates a new IdentificationHeader with the given $name and $id.
  36. *
  37. * @param string $name
  38. */
  39. public function __construct($name, EmailValidator $emailValidator, Swift_AddressEncoder $addressEncoder = null)
  40. {
  41. $this->setFieldName($name);
  42. $this->emailValidator = $emailValidator;
  43. $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder();
  44. }
  45. /**
  46. * Get the type of Header that this instance represents.
  47. *
  48. * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  49. * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  50. *
  51. * @return int
  52. */
  53. public function getFieldType()
  54. {
  55. return self::TYPE_ID;
  56. }
  57. /**
  58. * Set the model for the field body.
  59. *
  60. * This method takes a string ID, or an array of IDs.
  61. *
  62. * @param mixed $model
  63. *
  64. * @throws Swift_RfcComplianceException
  65. */
  66. public function setFieldBodyModel($model)
  67. {
  68. $this->setId($model);
  69. }
  70. /**
  71. * Get the model for the field body.
  72. *
  73. * This method returns an array of IDs
  74. *
  75. * @return array
  76. */
  77. public function getFieldBodyModel()
  78. {
  79. return $this->getIds();
  80. }
  81. /**
  82. * Set the ID used in the value of this header.
  83. *
  84. * @param string|array $id
  85. *
  86. * @throws Swift_RfcComplianceException
  87. */
  88. public function setId($id)
  89. {
  90. $this->setIds(\is_array($id) ? $id : [$id]);
  91. }
  92. /**
  93. * Get the ID used in the value of this Header.
  94. *
  95. * If multiple IDs are set only the first is returned.
  96. *
  97. * @return string
  98. */
  99. public function getId()
  100. {
  101. if (\count($this->ids) > 0) {
  102. return $this->ids[0];
  103. }
  104. }
  105. /**
  106. * Set a collection of IDs to use in the value of this Header.
  107. *
  108. * @param string[] $ids
  109. *
  110. * @throws Swift_RfcComplianceException
  111. */
  112. public function setIds(array $ids)
  113. {
  114. $actualIds = [];
  115. foreach ($ids as $id) {
  116. $this->assertValidId($id);
  117. $actualIds[] = $id;
  118. }
  119. $this->clearCachedValueIf($this->ids != $actualIds);
  120. $this->ids = $actualIds;
  121. }
  122. /**
  123. * Get the list of IDs used in this Header.
  124. *
  125. * @return string[]
  126. */
  127. public function getIds()
  128. {
  129. return $this->ids;
  130. }
  131. /**
  132. * Get the string value of the body in this Header.
  133. *
  134. * This is not necessarily RFC 2822 compliant since folding white space will
  135. * not be added at this stage (see {@see toString()} for that).
  136. *
  137. * @see toString()
  138. *
  139. * @throws Swift_RfcComplianceException
  140. *
  141. * @return string
  142. */
  143. public function getFieldBody()
  144. {
  145. if (!$this->getCachedValue()) {
  146. $angleAddrs = [];
  147. foreach ($this->ids as $id) {
  148. $angleAddrs[] = '<'.$this->addressEncoder->encodeString($id).'>';
  149. }
  150. $this->setCachedValue(implode(' ', $angleAddrs));
  151. }
  152. return $this->getCachedValue();
  153. }
  154. /**
  155. * Throws an Exception if the id passed does not comply with RFC 2822.
  156. *
  157. * @param string $id
  158. *
  159. * @throws Swift_RfcComplianceException
  160. */
  161. private function assertValidId($id)
  162. {
  163. $emailValidation = class_exists(MessageIDValidation::class) ? new MessageIDValidation() : new RFCValidation();
  164. if (!$this->emailValidator->isValid($id, $emailValidation)) {
  165. throw new Swift_RfcComplianceException('Invalid ID given <'.$id.'>');
  166. }
  167. }
  168. }