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