4
* This file is part of SwiftMailer.
5
* (c) 2004-2009 Chris Corbyn
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
11
//@require 'Swift/Mime/HeaderSet.php';
12
//@require 'Swift/Mime/HeaderFactory.php';
15
* A collection of MIME headers.
20
* @author Chris Corbyn
22
class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
28
/** Collection of set Headers */
29
private $_headers = array();
31
/** Field ordering details */
32
private $_order = array();
34
/** List of fields which are required to be displayed */
35
private $_required = array();
37
/** The charset used by Headers */
41
* Create a new SimpleHeaderSet with the given $factory.
43
* @param Swift_Mime_HeaderFactory $factory
44
* @param string $charset
46
public function __construct(Swift_Mime_HeaderFactory $factory,
49
$this->_factory = $factory;
52
$this->setCharset($charset);
57
* Set the charset used by these headers.
59
* @param string $charset
61
public function setCharset($charset)
63
$this->_charset = $charset;
64
$this->_factory->charsetChanged($charset);
65
$this->_notifyHeadersOfCharset($charset);
69
* Add a new Mailbox Header with a list of $addresses.
72
* @param array|string $addresses
74
public function addMailboxHeader($name, $addresses = null)
76
$this->_storeHeader($name,
77
$this->_factory->createMailboxHeader($name, $addresses));
81
* Add a new Date header using $timestamp (UNIX time).
84
* @param int $timestamp
86
public function addDateHeader($name, $timestamp = null)
88
$this->_storeHeader($name,
89
$this->_factory->createDateHeader($name, $timestamp));
93
* Add a new basic text header with $name and $value.
96
* @param string $value
98
public function addTextHeader($name, $value = null)
100
$this->_storeHeader($name,
101
$this->_factory->createTextHeader($name, $value));
105
* Add a new ParameterizedHeader with $name, $value and $params.
107
* @param string $name
108
* @param string $value
109
* @param array $params
111
public function addParameterizedHeader($name, $value = null,
114
$this->_storeHeader($name,
115
$this->_factory->createParameterizedHeader($name, $value,
120
* Add a new ID header for Message-ID or Content-ID.
122
* @param string $name
123
* @param string|array $ids
125
public function addIdHeader($name, $ids = null)
127
$this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
131
* Add a new Path header with an address (path) in it.
133
* @param string $name
134
* @param string $path
136
public function addPathHeader($name, $path = null)
138
$this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
142
* Returns true if at least one header with the given $name exists.
144
* If multiple headers match, the actual one may be specified by $index.
146
* @param string $name
151
public function has($name, $index = 0)
153
$lowerName = strtolower($name);
154
return array_key_exists($lowerName, $this->_headers)
155
&& array_key_exists($index, $this->_headers[$lowerName]);
159
* Set a header in the HeaderSet.
161
* The header may be a previously fetched header via {@link get()} or it may
162
* be one that has been created separately.
164
* If $index is specified, the header will be inserted into the set at this
167
* @param Swift_Mime_Header $header
170
public function set(Swift_Mime_Header $header, $index = 0)
172
$this->_storeHeader($header->getFieldName(), $header, $index);
176
* Get the header with the given $name.
178
* If multiple headers match, the actual one may be specified by $index.
179
* Returns NULL if none present.
181
* @param string $name
184
* @return Swift_Mime_Header
186
public function get($name, $index = 0)
188
if ($this->has($name, $index))
190
$lowerName = strtolower($name);
191
return $this->_headers[$lowerName][$index];
196
* Get all headers with the given $name.
198
* @param string $name
202
public function getAll($name = null)
207
foreach ($this->_headers as $collection)
209
$headers = array_merge($headers, $collection);
214
$lowerName = strtolower($name);
215
if (!array_key_exists($lowerName, $this->_headers))
219
return $this->_headers[$lowerName];
223
* Remove the header with the given $name if it's set.
225
* If multiple headers match, the actual one may be specified by $index.
227
* @param string $name
230
public function remove($name, $index = 0)
232
$lowerName = strtolower($name);
233
unset($this->_headers[$lowerName][$index]);
237
* Remove all headers with the given $name.
239
* @param string $name
241
public function removeAll($name)
243
$lowerName = strtolower($name);
244
unset($this->_headers[$lowerName]);
248
* Create a new instance of this HeaderSet.
250
* @return Swift_Mime_HeaderSet
252
public function newInstance()
254
return new self($this->_factory);
258
* Define a list of Header names as an array in the correct order.
260
* These Headers will be output in the given order where present.
262
* @param array $sequence
264
public function defineOrdering(array $sequence)
266
$this->_order = array_flip(array_map('strtolower', $sequence));
270
* Set a list of header names which must always be displayed when set.
272
* Usually headers without a field value won't be output unless set here.
274
* @param array $names
276
public function setAlwaysDisplayed(array $names)
278
$this->_required = array_flip(array_map('strtolower', $names));
282
* Notify this observer that the entity's charset has changed.
284
* @param string $charset
286
public function charsetChanged($charset)
288
$this->setCharset($charset);
292
* Returns a string with a representation of all headers.
296
public function toString()
299
$headers = $this->_headers;
300
if ($this->_canSort())
302
uksort($headers, array($this, '_sortHeaders'));
304
foreach ($headers as $collection)
306
foreach ($collection as $header)
308
if ($this->_isDisplayed($header) || $header->getFieldBody() != '')
310
$string .= $header->toString();
318
* Returns a string representation of this object.
324
public function __toString()
326
return $this->toString();
329
// -- Private methods
331
/** Save a Header to the internal collection */
332
private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
334
if (!isset($this->_headers[strtolower($name)]))
336
$this->_headers[strtolower($name)] = array();
340
$this->_headers[strtolower($name)][] = $header;
344
$this->_headers[strtolower($name)][$offset] = $header;
348
/** Test if the headers can be sorted */
349
private function _canSort()
351
return count($this->_order) > 0;
354
/** uksort() algorithm for Header ordering */
355
private function _sortHeaders($a, $b)
357
$lowerA = strtolower($a);
358
$lowerB = strtolower($b);
359
$aPos = array_key_exists($lowerA, $this->_order)
360
? $this->_order[$lowerA]
362
$bPos = array_key_exists($lowerB, $this->_order)
363
? $this->_order[$lowerB]
375
return ($aPos < $bPos) ? -1 : 1;
378
/** Test if the given Header is always displayed */
379
private function _isDisplayed(Swift_Mime_Header $header)
381
return array_key_exists(strtolower($header->getFieldName()), $this->_required);
384
/** Notify all Headers of the new charset */
385
private function _notifyHeadersOfCharset($charset)
387
foreach ($this->_headers as $headerGroup)
389
foreach ($headerGroup as $header)
391
$header->setCharset($charset);