SimpleHeaderFactory.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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,
  33. Swift_Encoder $paramEncoder, Swift_Mime_Grammar $grammar, $charset = null)
  34. {
  35. $this->_encoder = $encoder;
  36. $this->_paramEncoder = $paramEncoder;
  37. $this->_grammar = $grammar;
  38. $this->_charset = $charset;
  39. }
  40. /**
  41. * Create a new Mailbox Header with a list of $addresses.
  42. * @param string $name
  43. * @param array|string $addresses
  44. * @return Swift_Mime_Header
  45. */
  46. public function createMailboxHeader($name, $addresses = null)
  47. {
  48. $header = new Swift_Mime_Headers_MailboxHeader($name, $this->_encoder, $this->_grammar);
  49. if (isset($addresses))
  50. {
  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 $timestamp
  60. * @return Swift_Mime_Header
  61. */
  62. public function createDateHeader($name, $timestamp = null)
  63. {
  64. $header = new Swift_Mime_Headers_DateHeader($name, $this->_grammar);
  65. if (isset($timestamp))
  66. {
  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. * @param string $name
  75. * @param string $value
  76. * @return Swift_Mime_Header
  77. */
  78. public function createTextHeader($name, $value = null)
  79. {
  80. $header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->_encoder, $this->_grammar);
  81. if (isset($value))
  82. {
  83. $header->setFieldBodyModel($value);
  84. }
  85. $this->_setHeaderCharset($header);
  86. return $header;
  87. }
  88. /**
  89. * Create a new ParameterizedHeader with $name, $value and $params.
  90. * @param string $name
  91. * @param string $value
  92. * @param array $params
  93. * @return Swift_Mime_ParameterizedHeader
  94. */
  95. public function createParameterizedHeader($name, $value = null,
  96. $params = array())
  97. {
  98. $header = new Swift_Mime_Headers_ParameterizedHeader($name,
  99. $this->_encoder, (strtolower($name) == 'content-disposition')
  100. ? $this->_paramEncoder
  101. : null,
  102. $this->_grammar
  103. );
  104. if (isset($value))
  105. {
  106. $header->setFieldBodyModel($value);
  107. }
  108. foreach ($params as $k => $v)
  109. {
  110. $header->setParameter($k, $v);
  111. }
  112. $this->_setHeaderCharset($header);
  113. return $header;
  114. }
  115. /**
  116. * Create a new ID header for Message-ID or Content-ID.
  117. * @param string $name
  118. * @param string|array $ids
  119. * @return Swift_Mime_Header
  120. */
  121. public function createIdHeader($name, $ids = null)
  122. {
  123. $header = new Swift_Mime_Headers_IdentificationHeader($name, $this->_grammar);
  124. if (isset($ids))
  125. {
  126. $header->setFieldBodyModel($ids);
  127. }
  128. $this->_setHeaderCharset($header);
  129. return $header;
  130. }
  131. /**
  132. * Create a new Path header with an address (path) in it.
  133. * @param string $name
  134. * @param string $path
  135. * @return Swift_Mime_Header
  136. */
  137. public function createPathHeader($name, $path = null)
  138. {
  139. $header = new Swift_Mime_Headers_PathHeader($name, $this->_grammar);
  140. if (isset($path))
  141. {
  142. $header->setFieldBodyModel($path);
  143. }
  144. $this->_setHeaderCharset($header);
  145. return $header;
  146. }
  147. /**
  148. * Notify this observer that the entity's charset has changed.
  149. * @param string $charset
  150. */
  151. public function charsetChanged($charset)
  152. {
  153. $this->_charset = $charset;
  154. $this->_encoder->charsetChanged($charset);
  155. $this->_paramEncoder->charsetChanged($charset);
  156. }
  157. // -- Private methods
  158. /** Apply the charset to the Header */
  159. private function _setHeaderCharset(Swift_Mime_Header $header)
  160. {
  161. if (isset($this->_charset))
  162. {
  163. $header->setCharset($this->_charset);
  164. }
  165. }
  166. }