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