IdentificationHeader.php 4.0KB

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