PageRenderTime 39ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/Mime/MimePart.php

http://zoop.googlecode.com/
PHP | 196 lines | 92 code | 21 blank | 83 comment | 5 complexity | 60bf65875776765324efd0854c965c8e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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/SimpleMimeEntity.php';
  10. //@require 'Swift/Mime/ContentEncoder.php';
  11. //@require 'Swift/Mime/HeaderSet.php';
  12. //@require 'Swift/KeyCache.php';
  13. /**
  14. * A MIME part, in a multipart message.
  15. *
  16. * @package Swift
  17. * @subpackage Mime
  18. * @author Chris Corbyn
  19. */
  20. class Swift_Mime_MimePart extends Swift_Mime_SimpleMimeEntity
  21. {
  22. /** The format parameter last specified by the user */
  23. protected $_userFormat;
  24. /** The charset last specified by the user */
  25. protected $_userCharset;
  26. /** The delsp parameter last specified by the user */
  27. protected $_userDelSp;
  28. /** The nesting level of this MimePart */
  29. private $_nestingLevel = self::LEVEL_ALTERNATIVE;
  30. /**
  31. * Create a new MimePart with $headers, $encoder and $cache.
  32. *
  33. * @param Swift_Mime_HeaderSet $headers
  34. * @param Swift_Mime_ContentEncoder $encoder
  35. * @param Swift_KeyCache $cache
  36. * @param string $charset
  37. */
  38. public function __construct(Swift_Mime_HeaderSet $headers,
  39. Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, $charset = null)
  40. {
  41. parent::__construct($headers, $encoder, $cache);
  42. $this->setContentType('text/plain');
  43. if (!is_null($charset))
  44. {
  45. $this->setCharset($charset);
  46. }
  47. }
  48. /**
  49. * Set the body of this entity, either as a string, or as an instance of
  50. * {@link Swift_OutputByteStream}.
  51. *
  52. * @param mixed $body
  53. * @param string $contentType optional
  54. * @param string $charset optional
  55. */
  56. public function setBody($body, $contentType = null, $charset = null)
  57. {
  58. parent::setBody($body, $contentType);
  59. if (isset($charset))
  60. {
  61. $this->setCharset($charset);
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Get the character set of this entity.
  67. *
  68. * @return string
  69. */
  70. public function getCharset()
  71. {
  72. return $this->_getHeaderParameter('Content-Type', 'charset');
  73. }
  74. /**
  75. * Set the character set of this entity.
  76. *
  77. * @param string $charset
  78. */
  79. public function setCharset($charset)
  80. {
  81. $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  82. if ($charset !== $this->_userCharset)
  83. {
  84. $this->_clearCache();
  85. }
  86. $this->_userCharset = $charset;
  87. parent::charsetChanged($charset);
  88. return $this;
  89. }
  90. /**
  91. * Get the format of this entity (i.e. flowed or fixed).
  92. *
  93. * @return string
  94. */
  95. public function getFormat()
  96. {
  97. return $this->_getHeaderParameter('Content-Type', 'format');
  98. }
  99. /**
  100. * Set the format of this entity (flowed or fixed).
  101. *
  102. * @param string $format
  103. */
  104. public function setFormat($format)
  105. {
  106. $this->_setHeaderParameter('Content-Type', 'format', $format);
  107. $this->_userFormat = $format;
  108. return $this;
  109. }
  110. /**
  111. * Test if delsp is being used for this entity.
  112. *
  113. * @return boolean
  114. */
  115. public function getDelSp()
  116. {
  117. return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
  118. ? true
  119. : false;
  120. }
  121. /**
  122. * Turn delsp on or off for this entity.
  123. *
  124. * @param boolean $delsp
  125. */
  126. public function setDelSp($delsp = true)
  127. {
  128. $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  129. $this->_userDelSp = $delsp;
  130. return $this;
  131. }
  132. /**
  133. * Get the nesting level of this entity.
  134. *
  135. * @return int
  136. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  137. */
  138. public function getNestingLevel()
  139. {
  140. return $this->_nestingLevel;
  141. }
  142. /**
  143. * Receive notification that the charset has changed on this document, or a
  144. * parent document.
  145. *
  146. * @param string $charset
  147. */
  148. public function charsetChanged($charset)
  149. {
  150. $this->setCharset($charset);
  151. }
  152. // -- Protected methods
  153. /** Fix the content-type and encoding of this entity */
  154. protected function _fixHeaders()
  155. {
  156. parent::_fixHeaders();
  157. if (count($this->getChildren()))
  158. {
  159. $this->_setHeaderParameter('Content-Type', 'charset', null);
  160. $this->_setHeaderParameter('Content-Type', 'format', null);
  161. $this->_setHeaderParameter('Content-Type', 'delsp', null);
  162. }
  163. else
  164. {
  165. $this->setCharset($this->_userCharset);
  166. $this->setFormat($this->_userFormat);
  167. $this->setDelSp($this->_userDelSp);
  168. }
  169. }
  170. /** Set the nesting level of this entity */
  171. protected function _setNestingLevel($level)
  172. {
  173. $this->_nestingLevel = $level;
  174. }
  175. }