TYPO3  7.6
SimpleHeaderSet.php
Go to the documentation of this 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 
17 {
19  private $_factory;
20 
22  private $_headers = array();
23 
25  private $_order = array();
26 
28  private $_required = array();
29 
31  private $_charset;
32 
39  public function __construct(Swift_Mime_HeaderFactory $factory, $charset = null)
40  {
41  $this->_factory = $factory;
42  if (isset($charset)) {
43  $this->setCharset($charset);
44  }
45  }
46 
52  public function setCharset($charset)
53  {
54  $this->_charset = $charset;
55  $this->_factory->charsetChanged($charset);
56  $this->_notifyHeadersOfCharset($charset);
57  }
58 
65  public function addMailboxHeader($name, $addresses = null)
66  {
67  $this->_storeHeader($name,
68  $this->_factory->createMailboxHeader($name, $addresses));
69  }
70 
77  public function addDateHeader($name, $timestamp = null)
78  {
79  $this->_storeHeader($name,
80  $this->_factory->createDateHeader($name, $timestamp));
81  }
82 
89  public function addTextHeader($name, $value = null)
90  {
91  $this->_storeHeader($name,
92  $this->_factory->createTextHeader($name, $value));
93  }
94 
102  public function addParameterizedHeader($name, $value = null, $params = array())
103  {
104  $this->_storeHeader($name, $this->_factory->createParameterizedHeader($name, $value, $params));
105  }
106 
113  public function addIdHeader($name, $ids = null)
114  {
115  $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
116  }
117 
124  public function addPathHeader($name, $path = null)
125  {
126  $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
127  }
128 
139  public function has($name, $index = 0)
140  {
141  $lowerName = strtolower($name);
142 
143  return array_key_exists($lowerName, $this->_headers) && array_key_exists($index, $this->_headers[$lowerName]);
144  }
145 
158  public function set(Swift_Mime_Header $header, $index = 0)
159  {
160  $this->_storeHeader($header->getFieldName(), $header, $index);
161  }
162 
174  public function get($name, $index = 0)
175  {
176  if ($this->has($name, $index)) {
177  $lowerName = strtolower($name);
178 
179  return $this->_headers[$lowerName][$index];
180  }
181  }
182 
190  public function getAll($name = null)
191  {
192  if (!isset($name)) {
193  $headers = array();
194  foreach ($this->_headers as $collection) {
195  $headers = array_merge($headers, $collection);
196  }
197 
198  return $headers;
199  }
200 
201  $lowerName = strtolower($name);
202  if (!array_key_exists($lowerName, $this->_headers)) {
203  return array();
204  }
205 
206  return $this->_headers[$lowerName];
207  }
208 
214  public function listAll()
215  {
216  $headers = $this->_headers;
217  if ($this->_canSort()) {
218  uksort($headers, array($this, '_sortHeaders'));
219  }
220 
221  return array_keys($headers);
222  }
223 
232  public function remove($name, $index = 0)
233  {
234  $lowerName = strtolower($name);
235  unset($this->_headers[$lowerName][$index]);
236  }
237 
243  public function removeAll($name)
244  {
245  $lowerName = strtolower($name);
246  unset($this->_headers[$lowerName]);
247  }
248 
254  public function newInstance()
255  {
256  return new self($this->_factory);
257  }
258 
266  public function defineOrdering(array $sequence)
267  {
268  $this->_order = array_flip(array_map('strtolower', $sequence));
269  }
270 
278  public function setAlwaysDisplayed(array $names)
279  {
280  $this->_required = array_flip(array_map('strtolower', $names));
281  }
282 
288  public function charsetChanged($charset)
289  {
290  $this->setCharset($charset);
291  }
292 
298  public function toString()
299  {
300  $string = '';
301  $headers = $this->_headers;
302  if ($this->_canSort()) {
303  uksort($headers, array($this, '_sortHeaders'));
304  }
305  foreach ($headers as $collection) {
306  foreach ($collection as $header) {
307  if ($this->_isDisplayed($header) || $header->getFieldBody() != '') {
308  $string .= $header->toString();
309  }
310  }
311  }
312 
313  return $string;
314  }
315 
323  public function __toString()
324  {
325  return $this->toString();
326  }
327 
329  private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
330  {
331  if (!isset($this->_headers[strtolower($name)])) {
332  $this->_headers[strtolower($name)] = array();
333  }
334  if (!isset($offset)) {
335  $this->_headers[strtolower($name)][] = $header;
336  } else {
337  $this->_headers[strtolower($name)][$offset] = $header;
338  }
339  }
340 
342  private function _canSort()
343  {
344  return count($this->_order) > 0;
345  }
346 
348  private function _sortHeaders($a, $b)
349  {
350  $lowerA = strtolower($a);
351  $lowerB = strtolower($b);
352  $aPos = array_key_exists($lowerA, $this->_order)
353  ? $this->_order[$lowerA]
354  : -1;
355  $bPos = array_key_exists($lowerB, $this->_order)
356  ? $this->_order[$lowerB]
357  : -1;
358 
359  if ($aPos == -1) {
360  return 1;
361  } elseif ($bPos == -1) {
362  return -1;
363  }
364 
365  return ($aPos < $bPos) ? -1 : 1;
366  }
367 
369  private function _isDisplayed(Swift_Mime_Header $header)
370  {
371  return array_key_exists(strtolower($header->getFieldName()), $this->_required);
372  }
373 
375  private function _notifyHeadersOfCharset($charset)
376  {
377  foreach ($this->_headers as $headerGroup) {
378  foreach ($headerGroup as $header) {
379  $header->setCharset($charset);
380  }
381  }
382  }
383 
387  public function __clone()
388  {
389  $this->_factory = clone $this->_factory;
390  foreach ($this->_headers as $groupKey => $headerGroup) {
391  foreach ($headerGroup as $key => $header) {
392  $this->_headers[$groupKey][$key] = clone $header;
393  }
394  }
395  }
396 }