SimpleHeaderFactory.php 5.1KB

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