IdentificationHeader.php 3.6KB

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