[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Mime/ -> SimpleHeaderSet.php (source)

   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   * A collection of MIME headers.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
  17  {
  18      /** HeaderFactory */
  19      private $_factory;
  20  
  21      /** Collection of set Headers */
  22      private $_headers = array();
  23  
  24      /** Field ordering details */
  25      private $_order = array();
  26  
  27      /** List of fields which are required to be displayed */
  28      private $_required = array();
  29  
  30      /** The charset used by Headers */
  31      private $_charset;
  32  
  33      /**
  34       * Create a new SimpleHeaderSet with the given $factory.
  35       *
  36       * @param Swift_Mime_HeaderFactory $factory
  37       * @param string                   $charset
  38       */
  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  
  47      /**
  48       * Set the charset used by these headers.
  49       *
  50       * @param string $charset
  51       */
  52      public function setCharset($charset)
  53      {
  54          $this->_charset = $charset;
  55          $this->_factory->charsetChanged($charset);
  56          $this->_notifyHeadersOfCharset($charset);
  57      }
  58  
  59      /**
  60       * Add a new Mailbox Header with a list of $addresses.
  61       *
  62       * @param string       $name
  63       * @param array|string $addresses
  64       */
  65      public function addMailboxHeader($name, $addresses = null)
  66      {
  67          $this->_storeHeader($name,
  68          $this->_factory->createMailboxHeader($name, $addresses));
  69      }
  70  
  71      /**
  72       * Add a new Date header using $timestamp (UNIX time).
  73       *
  74       * @param string $name
  75       * @param int    $timestamp
  76       */
  77      public function addDateHeader($name, $timestamp = null)
  78      {
  79          $this->_storeHeader($name,
  80          $this->_factory->createDateHeader($name, $timestamp));
  81      }
  82  
  83      /**
  84       * Add a new basic text header with $name and $value.
  85       *
  86       * @param string $name
  87       * @param string $value
  88       */
  89      public function addTextHeader($name, $value = null)
  90      {
  91          $this->_storeHeader($name,
  92          $this->_factory->createTextHeader($name, $value));
  93      }
  94  
  95      /**
  96       * Add a new ParameterizedHeader with $name, $value and $params.
  97       *
  98       * @param string $name
  99       * @param string $value
 100       * @param array  $params
 101       */
 102      public function addParameterizedHeader($name, $value = null, $params = array())
 103      {
 104          $this->_storeHeader($name, $this->_factory->createParameterizedHeader($name, $value, $params));
 105      }
 106  
 107      /**
 108       * Add a new ID header for Message-ID or Content-ID.
 109       *
 110       * @param string       $name
 111       * @param string|array $ids
 112       */
 113      public function addIdHeader($name, $ids = null)
 114      {
 115          $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
 116      }
 117  
 118      /**
 119       * Add a new Path header with an address (path) in it.
 120       *
 121       * @param string $name
 122       * @param string $path
 123       */
 124      public function addPathHeader($name, $path = null)
 125      {
 126          $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
 127      }
 128  
 129      /**
 130       * Returns true if at least one header with the given $name exists.
 131       *
 132       * If multiple headers match, the actual one may be specified by $index.
 133       *
 134       * @param string $name
 135       * @param int    $index
 136       *
 137       * @return bool
 138       */
 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  
 146      /**
 147       * Set a header in the HeaderSet.
 148       *
 149       * The header may be a previously fetched header via {@link get()} or it may
 150       * be one that has been created separately.
 151       *
 152       * If $index is specified, the header will be inserted into the set at this
 153       * offset.
 154       *
 155       * @param Swift_Mime_Header $header
 156       * @param int               $index
 157       */
 158      public function set(Swift_Mime_Header $header, $index = 0)
 159      {
 160          $this->_storeHeader($header->getFieldName(), $header, $index);
 161      }
 162  
 163      /**
 164       * Get the header with the given $name.
 165       *
 166       * If multiple headers match, the actual one may be specified by $index.
 167       * Returns NULL if none present.
 168       *
 169       * @param string $name
 170       * @param int    $index
 171       *
 172       * @return Swift_Mime_Header
 173       */
 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  
 183      /**
 184       * Get all headers with the given $name.
 185       *
 186       * @param string $name
 187       *
 188       * @return array
 189       */
 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  
 209      /**
 210       * Return the name of all Headers.
 211       *
 212       * @return array
 213       */
 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  
 224      /**
 225       * Remove the header with the given $name if it's set.
 226       *
 227       * If multiple headers match, the actual one may be specified by $index.
 228       *
 229       * @param string $name
 230       * @param int    $index
 231       */
 232      public function remove($name, $index = 0)
 233      {
 234          $lowerName = strtolower($name);
 235          unset($this->_headers[$lowerName][$index]);
 236      }
 237  
 238      /**
 239       * Remove all headers with the given $name.
 240       *
 241       * @param string $name
 242       */
 243      public function removeAll($name)
 244      {
 245          $lowerName = strtolower($name);
 246          unset($this->_headers[$lowerName]);
 247      }
 248  
 249      /**
 250       * Create a new instance of this HeaderSet.
 251       *
 252       * @return Swift_Mime_HeaderSet
 253       */
 254      public function newInstance()
 255      {
 256          return new self($this->_factory);
 257      }
 258  
 259      /**
 260       * Define a list of Header names as an array in the correct order.
 261       *
 262       * These Headers will be output in the given order where present.
 263       *
 264       * @param array $sequence
 265       */
 266      public function defineOrdering(array $sequence)
 267      {
 268          $this->_order = array_flip(array_map('strtolower', $sequence));
 269      }
 270  
 271      /**
 272       * Set a list of header names which must always be displayed when set.
 273       *
 274       * Usually headers without a field value won't be output unless set here.
 275       *
 276       * @param array $names
 277       */
 278      public function setAlwaysDisplayed(array $names)
 279      {
 280          $this->_required = array_flip(array_map('strtolower', $names));
 281      }
 282  
 283      /**
 284       * Notify this observer that the entity's charset has changed.
 285       *
 286       * @param string $charset
 287       */
 288      public function charsetChanged($charset)
 289      {
 290          $this->setCharset($charset);
 291      }
 292  
 293      /**
 294       * Returns a string with a representation of all headers.
 295       *
 296       * @return string
 297       */
 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  
 316      /**
 317       * Returns a string representation of this object.
 318       *
 319       * @return string
 320       *
 321       * @see toString()
 322       */
 323      public function __toString()
 324      {
 325          return $this->toString();
 326      }
 327  
 328      /** Save a Header to the internal collection */
 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  
 341      /** Test if the headers can be sorted */
 342      private function _canSort()
 343      {
 344          return count($this->_order) > 0;
 345      }
 346  
 347      /** uksort() algorithm for Header ordering */
 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  
 368      /** Test if the given Header is always displayed */
 369      private function _isDisplayed(Swift_Mime_Header $header)
 370      {
 371          return array_key_exists(strtolower($header->getFieldName()), $this->_required);
 372      }
 373  
 374      /** Notify all Headers of the new charset */
 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  
 384      /**
 385       * Make a deep copy of object.
 386       */
 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  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1