SimpleHeaderFactory.php 5.0 KB

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