IdentificationHeader.php 4.1KB

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