PageRenderTime 61ms CodeModel.GetById 27ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/Mime/SimpleHeaderSet.php

http://zoop.googlecode.com/
PHP | 396 lines | 193 code | 38 blank | 165 comment | 14 complexity | 2e5c664af8c7faefdff5ef604dc62ca7 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/HeaderSet.php';
 12//@require 'Swift/Mime/HeaderFactory.php';
 13
 14/**
 15 * A collection of MIME headers.
 16 * 
 17 * @package Swift
 18 * @subpackage Mime
 19 * 
 20 * @author Chris Corbyn
 21 */
 22class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
 23{
 24  
 25  /** HeaderFactory */
 26  private $_factory;
 27  
 28  /** Collection of set Headers */
 29  private $_headers = array();
 30  
 31  /** Field ordering details */
 32  private $_order = array();
 33  
 34  /** List of fields which are required to be displayed */
 35  private $_required = array();
 36  
 37  /** The charset used by Headers */
 38  private $_charset;
 39  
 40  /**
 41   * Create a new SimpleHeaderSet with the given $factory.
 42   * 
 43   * @param Swift_Mime_HeaderFactory $factory
 44   * @param string $charset
 45   */
 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  
 56  /**
 57   * Set the charset used by these headers.
 58   * 
 59   * @param string $charset
 60   */
 61  public function setCharset($charset)
 62  {
 63    $this->_charset = $charset;
 64    $this->_factory->charsetChanged($charset);
 65    $this->_notifyHeadersOfCharset($charset);
 66  }
 67  
 68  /**
 69   * Add a new Mailbox Header with a list of $addresses.
 70   * 
 71   * @param string $name
 72   * @param array|string $addresses
 73   */
 74  public function addMailboxHeader($name, $addresses = null)
 75  {
 76    $this->_storeHeader($name,
 77      $this->_factory->createMailboxHeader($name, $addresses));
 78  }
 79  
 80  /**
 81   * Add a new Date header using $timestamp (UNIX time).
 82   * 
 83   * @param string $name
 84   * @param int $timestamp
 85   */
 86  public function addDateHeader($name, $timestamp = null)
 87  {
 88    $this->_storeHeader($name,
 89      $this->_factory->createDateHeader($name, $timestamp));
 90  }
 91  
 92  /**
 93   * Add a new basic text header with $name and $value.
 94   * 
 95   * @param string $name
 96   * @param string $value
 97   */
 98  public function addTextHeader($name, $value = null)
 99  {
100    $this->_storeHeader($name,
101      $this->_factory->createTextHeader($name, $value));
102  }
103  
104  /**
105   * Add a new ParameterizedHeader with $name, $value and $params.
106   * 
107   * @param string $name
108   * @param string $value
109   * @param array $params
110   */
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  
119  /**
120   * Add a new ID header for Message-ID or Content-ID.
121   * 
122   * @param string $name
123   * @param string|array $ids
124   */
125  public function addIdHeader($name, $ids = null)
126  {
127    $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
128  }
129  
130  /**
131   * Add a new Path header with an address (path) in it.
132   * 
133   * @param string $name
134   * @param string $path
135   */
136  public function addPathHeader($name, $path = null)
137  {
138    $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
139  }
140  
141  /**
142   * Returns true if at least one header with the given $name exists.
143   * 
144   * If multiple headers match, the actual one may be specified by $index.
145   * 
146   * @param string $name
147   * @param int $index
148   * 
149   * @return boolean
150   */
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  
158  /**
159   * Set a header in the HeaderSet.
160   * 
161   * The header may be a previously fetched header via {@link get()} or it may
162   * be one that has been created separately.
163   * 
164   * If $index is specified, the header will be inserted into the set at this
165   * offset.
166   * 
167   * @param Swift_Mime_Header $header
168   * @param int $index
169   */
170  public function set(Swift_Mime_Header $header, $index = 0)
171  {
172    $this->_storeHeader($header->getFieldName(), $header, $index);
173  }
174  
175  /**
176   * Get the header with the given $name.
177   * 
178   * If multiple headers match, the actual one may be specified by $index.
179   * Returns NULL if none present.
180   * 
181   * @param string $name
182   * @param int $index
183   * 
184   * @return Swift_Mime_Header
185   */
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  
195  /**
196   * Get all headers with the given $name.
197   * 
198   * @param string $name
199   * 
200   * @return array
201   */
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  
222  /**
223   * Remove the header with the given $name if it's set.
224   * 
225   * If multiple headers match, the actual one may be specified by $index.
226   * 
227   * @param string $name
228   * @param int $index
229   */
230  public function remove($name, $index = 0)
231  {
232    $lowerName = strtolower($name);
233    unset($this->_headers[$lowerName][$index]);
234  }
235  
236  /**
237   * Remove all headers with the given $name.
238   * 
239   * @param string $name
240   */
241  public function removeAll($name)
242  {
243    $lowerName = strtolower($name);
244    unset($this->_headers[$lowerName]);
245  }
246  
247  /**
248   * Create a new instance of this HeaderSet.
249   * 
250   * @return Swift_Mime_HeaderSet
251   */
252  public function newInstance()
253  {
254    return new self($this->_factory);
255  }
256  
257  /**
258   * Define a list of Header names as an array in the correct order.
259   * 
260   * These Headers will be output in the given order where present.
261   * 
262   * @param array $sequence
263   */
264  public function defineOrdering(array $sequence)
265  {
266    $this->_order = array_flip(array_map('strtolower', $sequence));
267  }
268  
269  /**
270   * Set a list of header names which must always be displayed when set.
271   * 
272   * Usually headers without a field value won't be output unless set here.
273   * 
274   * @param array $names
275   */
276  public function setAlwaysDisplayed(array $names)
277  {
278    $this->_required = array_flip(array_map('strtolower', $names));
279  }
280
281  /**
282   * Notify this observer that the entity's charset has changed.
283   * 
284   * @param string $charset
285   */
286  public function charsetChanged($charset)
287  {
288    $this->setCharset($charset);
289  }
290  
291  /**
292   * Returns a string with a representation of all headers.
293   * 
294   * @return string
295   */
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  
317  /**
318   * Returns a string representation of this object.
319   *
320   * @return string
321   *
322   * @see toString()
323   */
324  public function __toString()
325  {
326    return $this->toString();
327  }
328  
329  // -- Private methods
330  
331  /** Save a Header to the internal collection */
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  
348  /** Test if the headers can be sorted */
349  private function _canSort()
350  {
351    return count($this->_order) > 0;
352  }
353  
354  /** uksort() algorithm for Header ordering */
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  
378  /** Test if the given Header is always displayed */
379  private function _isDisplayed(Swift_Mime_Header $header)
380  {
381    return array_key_exists(strtolower($header->getFieldName()), $this->_required);
382  }
383  
384  /** Notify all Headers of the new charset */
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}