SimpleHeaderFactory.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * Creates MIME headers.
  11. * @package Swift
  12. * @subpackage Mime
  13. * @author Chris Corbyn
  14. */
  15. class Swift_Mime_SimpleHeaderFactory implements Swift_Mime_HeaderFactory
  16. {
  17. /** The HeaderEncoder used by these headers */
  18. private $_encoder;
  19. /** The Encoder used by parameters */
  20. private $_paramEncoder;
  21. /** The Grammar */
  22. private $_grammar;
  23. /** The charset of created Headers */
  24. private $_charset;
  25. /**
  26. * Creates a new SimpleHeaderFactory using $encoder and $paramEncoder.
  27. * @param Swift_Mime_HeaderEncoder $encoder
  28. * @param Swift_Encoder $paramEncoder
  29. * @param Swift_Mime_Grammar $grammar
  30. * @param string $charset
  31. */
  32. public function __construct(Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder, Swift_Mime_Grammar $grammar, $charset = null)
  33. {
  34. $this->_encoder = $encoder;
  35. $this->_paramEncoder = $paramEncoder;
  36. $this->_grammar = $grammar;
  37. $this->_charset = $charset;
  38. }
  39. /**
  40. * Create a new Mailbox Header with a list of $addresses.
  41. * @param string $name
  42. * @param array|string $addresses
  43. * @return Swift_Mime_Header
  44. */
  45. public function createMailboxHeader($name, $addresses = null)
  46. {
  47. $header = new Swift_Mime_Headers_MailboxHeader($name, $this->_encoder, $this->_grammar);
  48. if (isset($addresses)) {
  49. $header->setFieldBodyModel($addresses);
  50. }
  51. $this->_setHeaderCharset($header);
  52. return $header;
  53. }
  54. /**
  55. * Create a new Date header using $timestamp (UNIX time).
  56. * @param string $name
  57. * @param int $timestamp
  58. * @return Swift_Mime_Header
  59. */
  60. public function createDateHeader($name, $timestamp = null)
  61. {
  62. $header = new Swift_Mime_Headers_DateHeader($name, $this->_grammar);
  63. if (isset($timestamp)) {
  64. $header->setFieldBodyModel($timestamp);
  65. }
  66. $this->_setHeaderCharset($header);
  67. return $header;
  68. }
  69. /**
  70. * Create a new basic text header with $name and $value.
  71. * @param string $name
  72. * @param string $value
  73. * @return Swift_Mime_Header
  74. */
  75. public function createTextHeader($name, $value = null)
  76. {
  77. $header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->_encoder, $this->_grammar);
  78. if (isset($value)) {
  79. $header->setFieldBodyModel($value);
  80. }
  81. $this->_setHeaderCharset($header);
  82. return $header;
  83. }
  84. /**
  85. * Create a new ParameterizedHeader with $name, $value and $params.
  86. * @param string $name
  87. * @param string $value
  88. * @param array $params
  89. * @return Swift_Mime_ParameterizedHeader
  90. */
  91. public function createParameterizedHeader($name, $value = null,
  92. $params = array())
  93. {
  94. $header = new Swift_Mime_Headers_ParameterizedHeader($name,
  95. $this->_encoder, (strtolower($name) == 'content-disposition')
  96. ? $this->_paramEncoder
  97. : null,
  98. $this->_grammar
  99. );
  100. if (isset($value)) {
  101. $header->setFieldBodyModel($value);
  102. }
  103. foreach ($params as $k => $v) {
  104. $header->setParameter($k, $v);
  105. }
  106. $this->_setHeaderCharset($header);
  107. return $header;
  108. }
  109. /**
  110. * Create a new ID header for Message-ID or Content-ID.
  111. * @param string $name
  112. * @param string|array $ids
  113. * @return Swift_Mime_Header
  114. */
  115. public function createIdHeader($name, $ids = null)
  116. {
  117. $header = new Swift_Mime_Headers_IdentificationHeader($name, $this->_grammar);
  118. if (isset($ids)) {
  119. $header->setFieldBodyModel($ids);
  120. }
  121. $this->_setHeaderCharset($header);
  122. return $header;
  123. }
  124. /**
  125. * Create a new Path header with an address (path) in it.
  126. * @param string $name
  127. * @param string $path
  128. * @return Swift_Mime_Header
  129. */
  130. public function createPathHeader($name, $path = null)
  131. {
  132. $header = new Swift_Mime_Headers_PathHeader($name, $this->_grammar);
  133. if (isset($path)) {
  134. $header->setFieldBodyModel($path);
  135. }
  136. $this->_setHeaderCharset($header);
  137. return $header;
  138. }
  139. /**
  140. * Notify this observer that the entity's charset has changed.
  141. * @param string $charset
  142. */
  143. public function charsetChanged($charset)
  144. {
  145. $this->_charset = $charset;
  146. $this->_encoder->charsetChanged($charset);
  147. $this->_paramEncoder->charsetChanged($charset);
  148. }
  149. // -- Private methods
  150. /** Apply the charset to the Header */
  151. private function _setHeaderCharset(Swift_Mime_Header $header)
  152. {
  153. if (isset($this->_charset)) {
  154. $header->setCharset($this->_charset);
  155. }
  156. }
  157. }