/framework/vendor/swift/lib/classes/Swift/Mime/Headers/IdentificationHeader.php

http://zoop.googlecode.com/ · PHP · 161 lines · 74 code · 18 blank · 69 comment · 4 complexity · 85fe7888f3dacc2b1f989bc408209199 MD5 · raw file

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