IdentificationHeader.php 4.0 KB

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