XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
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 
11 //@require 'Swift/Mime/HeaderSet.php';
12 //@require 'Swift/Mime/HeaderFactory.php';
13 
23 {
24 
26  private $_factory;
27 
29  private $_headers = array();
30 
32  private $_order = array();
33 
35  private $_required = array();
36 
38  private $_charset;
39 
46  public function __construct(Swift_Mime_HeaderFactory $factory,
47  $charset = null)
48  {
49  $this->_factory = $factory;
50  if (isset($charset))
51  {
52  $this->setCharset($charset);
53  }
54  }
55 
61  public function setCharset($charset)
62  {
63  $this->_charset = $charset;
64  $this->_factory->charsetChanged($charset);
65  $this->_notifyHeadersOfCharset($charset);
66  }
67 
74  public function addMailboxHeader($name, $addresses = null)
75  {
76  $this->_storeHeader($name,
77  $this->_factory->createMailboxHeader($name, $addresses));
78  }
79 
86  public function addDateHeader($name, $timestamp = null)
87  {
88  $this->_storeHeader($name,
89  $this->_factory->createDateHeader($name, $timestamp));
90  }
91 
98  public function addTextHeader($name, $value = null)
99  {
100  $this->_storeHeader($name,
101  $this->_factory->createTextHeader($name, $value));
102  }
103 
111  public function addParameterizedHeader($name, $value = null,
112  $params = array())
113  {
114  $this->_storeHeader($name,
115  $this->_factory->createParameterizedHeader($name, $value,
116  $params));
117  }
118 
125  public function addIdHeader($name, $ids = null)
126  {
127  $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
128  }
129 
136  public function addPathHeader($name, $path = null)
137  {
138  $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
139  }
140 
151  public function has($name, $index = 0)
152  {
153  $lowerName = strtolower($name);
154  return array_key_exists($lowerName, $this->_headers)
155  && array_key_exists($index, $this->_headers[$lowerName]);
156  }
157 
170  public function set(Swift_Mime_Header $header, $index = 0)
171  {
172  $this->_storeHeader($header->getFieldName(), $header, $index);
173  }
174 
186  public function get($name, $index = 0)
187  {
188  if ($this->has($name, $index))
189  {
190  $lowerName = strtolower($name);
191  return $this->_headers[$lowerName][$index];
192  }
193  }
194 
202  public function getAll($name = null)
203  {
204  if (!isset($name))
205  {
206  $headers = array();
207  foreach ($this->_headers as $collection)
208  {
209  $headers = array_merge($headers, $collection);
210  }
211  return $headers;
212  }
213 
214  $lowerName = strtolower($name);
215  if (!array_key_exists($lowerName, $this->_headers))
216  {
217  return array();
218  }
219  return $this->_headers[$lowerName];
220  }
221 
230  public function remove($name, $index = 0)
231  {
232  $lowerName = strtolower($name);
233  unset($this->_headers[$lowerName][$index]);
234  }
235 
241  public function removeAll($name)
242  {
243  $lowerName = strtolower($name);
244  unset($this->_headers[$lowerName]);
245  }
246 
252  public function newInstance()
253  {
254  return new self($this->_factory);
255  }
256 
264  public function defineOrdering(array $sequence)
265  {
266  $this->_order = array_flip(array_map('strtolower', $sequence));
267  }
268 
276  public function setAlwaysDisplayed(array $names)
277  {
278  $this->_required = array_flip(array_map('strtolower', $names));
279  }
280 
286  public function charsetChanged($charset)
287  {
288  $this->setCharset($charset);
289  }
290 
296  public function toString()
297  {
298  $string = '';
299  $headers = $this->_headers;
300  if ($this->_canSort())
301  {
302  uksort($headers, array($this, '_sortHeaders'));
303  }
304  foreach ($headers as $collection)
305  {
306  foreach ($collection as $header)
307  {
308  if ($this->_isDisplayed($header) || $header->getFieldBody() != '')
309  {
310  $string .= $header->toString();
311  }
312  }
313  }
314  return $string;
315  }
316 
324  public function __toString()
325  {
326  return $this->toString();
327  }
328 
329  // -- Private methods
330 
332  private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
333  {
334  if (!isset($this->_headers[strtolower($name)]))
335  {
336  $this->_headers[strtolower($name)] = array();
337  }
338  if (!isset($offset))
339  {
340  $this->_headers[strtolower($name)][] = $header;
341  }
342  else
343  {
344  $this->_headers[strtolower($name)][$offset] = $header;
345  }
346  }
347 
349  private function _canSort()
350  {
351  return count($this->_order) > 0;
352  }
353 
355  private function _sortHeaders($a, $b)
356  {
357  $lowerA = strtolower($a);
358  $lowerB = strtolower($b);
359  $aPos = array_key_exists($lowerA, $this->_order)
360  ? $this->_order[$lowerA]
361  : -1;
362  $bPos = array_key_exists($lowerB, $this->_order)
363  ? $this->_order[$lowerB]
364  : -1;
365 
366  if ($aPos == -1)
367  {
368  return 1;
369  }
370  elseif ($bPos == -1)
371  {
372  return -1;
373  }
374 
375  return ($aPos < $bPos) ? -1 : 1;
376  }
377 
379  private function _isDisplayed(Swift_Mime_Header $header)
380  {
381  return array_key_exists(strtolower($header->getFieldName()), $this->_required);
382  }
383 
385  private function _notifyHeadersOfCharset($charset)
386  {
387  foreach ($this->_headers as $headerGroup)
388  {
389  foreach ($headerGroup as $header)
390  {
391  $header->setCharset($charset);
392  }
393  }
394  }
395 
396 }