[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Mime/Headers/ -> MailboxHeader.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 Mailbox Address MIME Header for something like From or Sender.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader
  17  {
  18      /**
  19       * The mailboxes used in this Header.
  20       *
  21       * @var string[]
  22       */
  23      private $_mailboxes = array();
  24  
  25      /**
  26       * Creates a new MailboxHeader with $name.
  27       *
  28       * @param string                   $name    of Header
  29       * @param Swift_Mime_HeaderEncoder $encoder
  30       * @param Swift_Mime_Grammar       $grammar
  31       */
  32      public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Mime_Grammar $grammar)
  33      {
  34          $this->setFieldName($name);
  35          $this->setEncoder($encoder);
  36          parent::__construct($grammar);
  37      }
  38  
  39      /**
  40       * Get the type of Header that this instance represents.
  41       *
  42       * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  43       * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  44       *
  45       * @return int
  46       */
  47      public function getFieldType()
  48      {
  49          return self::TYPE_MAILBOX;
  50      }
  51  
  52      /**
  53       * Set the model for the field body.
  54       *
  55       * This method takes a string, or an array of addresses.
  56       *
  57       * @param mixed $model
  58       *
  59       * @throws Swift_RfcComplianceException
  60       */
  61      public function setFieldBodyModel($model)
  62      {
  63          $this->setNameAddresses($model);
  64      }
  65  
  66      /**
  67       * Get the model for the field body.
  68       *
  69       * This method returns an associative array like {@link getNameAddresses()}
  70       *
  71       * @throws Swift_RfcComplianceException
  72       *
  73       * @return array
  74       */
  75      public function getFieldBodyModel()
  76      {
  77          return $this->getNameAddresses();
  78      }
  79  
  80      /**
  81       * Set a list of mailboxes to be shown in this Header.
  82       *
  83       * The mailboxes can be a simple array of addresses, or an array of
  84       * key=>value pairs where (email => personalName).
  85       * Example:
  86       * <code>
  87       * <?php
  88       * //Sets two mailboxes in the Header, one with a personal name
  89       * $header->setNameAddresses(array(
  90       *  'chris@swiftmailer.org' => 'Chris Corbyn',
  91       *  'mark@swiftmailer.org' //No associated personal name
  92       *  ));
  93       * ?>
  94       * </code>
  95       *
  96       * @see __construct()
  97       * @see setAddresses()
  98       * @see setValue()
  99       *
 100       * @param string|string[] $mailboxes
 101       *
 102       * @throws Swift_RfcComplianceException
 103       */
 104      public function setNameAddresses($mailboxes)
 105      {
 106          $this->_mailboxes = $this->normalizeMailboxes((array) $mailboxes);
 107          $this->setCachedValue(null); //Clear any cached value
 108      }
 109  
 110      /**
 111       * Get the full mailbox list of this Header as an array of valid RFC 2822 strings.
 112       *
 113       * Example:
 114       * <code>
 115       * <?php
 116       * $header = new Swift_Mime_Headers_MailboxHeader('From',
 117       *  array('chris@swiftmailer.org' => 'Chris Corbyn',
 118       *  'mark@swiftmailer.org' => 'Mark Corbyn')
 119       *  );
 120       * print_r($header->getNameAddressStrings());
 121       * // array (
 122       * // 0 => Chris Corbyn <chris@swiftmailer.org>,
 123       * // 1 => Mark Corbyn <mark@swiftmailer.org>
 124       * // )
 125       * ?>
 126       * </code>
 127       *
 128       * @see getNameAddresses()
 129       * @see toString()
 130       *
 131       * @throws Swift_RfcComplianceException
 132       *
 133       * @return string[]
 134       */
 135      public function getNameAddressStrings()
 136      {
 137          return $this->_createNameAddressStrings($this->getNameAddresses());
 138      }
 139  
 140      /**
 141       * Get all mailboxes in this Header as key=>value pairs.
 142       *
 143       * The key is the address and the value is the name (or null if none set).
 144       * Example:
 145       * <code>
 146       * <?php
 147       * $header = new Swift_Mime_Headers_MailboxHeader('From',
 148       *  array('chris@swiftmailer.org' => 'Chris Corbyn',
 149       *  'mark@swiftmailer.org' => 'Mark Corbyn')
 150       *  );
 151       * print_r($header->getNameAddresses());
 152       * // array (
 153       * // chris@swiftmailer.org => Chris Corbyn,
 154       * // mark@swiftmailer.org => Mark Corbyn
 155       * // )
 156       * ?>
 157       * </code>
 158       *
 159       * @see getAddresses()
 160       * @see getNameAddressStrings()
 161       *
 162       * @return string[]
 163       */
 164      public function getNameAddresses()
 165      {
 166          return $this->_mailboxes;
 167      }
 168  
 169      /**
 170       * Makes this Header represent a list of plain email addresses with no names.
 171       *
 172       * Example:
 173       * <code>
 174       * <?php
 175       * //Sets three email addresses as the Header data
 176       * $header->setAddresses(
 177       *  array('one@domain.tld', 'two@domain.tld', 'three@domain.tld')
 178       *  );
 179       * ?>
 180       * </code>
 181       *
 182       * @see setNameAddresses()
 183       * @see setValue()
 184       *
 185       * @param string[] $addresses
 186       *
 187       * @throws Swift_RfcComplianceException
 188       */
 189      public function setAddresses($addresses)
 190      {
 191          $this->setNameAddresses(array_values((array) $addresses));
 192      }
 193  
 194      /**
 195       * Get all email addresses in this Header.
 196       *
 197       * @see getNameAddresses()
 198       *
 199       * @return string[]
 200       */
 201      public function getAddresses()
 202      {
 203          return array_keys($this->_mailboxes);
 204      }
 205  
 206      /**
 207       * Remove one or more addresses from this Header.
 208       *
 209       * @param string|string[] $addresses
 210       */
 211      public function removeAddresses($addresses)
 212      {
 213          $this->setCachedValue(null);
 214          foreach ((array) $addresses as $address) {
 215              unset($this->_mailboxes[$address]);
 216          }
 217      }
 218  
 219      /**
 220       * Get the string value of the body in this Header.
 221       *
 222       * This is not necessarily RFC 2822 compliant since folding white space will
 223       * not be added at this stage (see {@link toString()} for that).
 224       *
 225       * @see toString()
 226       *
 227       * @throws Swift_RfcComplianceException
 228       *
 229       * @return string
 230       */
 231      public function getFieldBody()
 232      {
 233          // Compute the string value of the header only if needed
 234          if (is_null($this->getCachedValue())) {
 235              $this->setCachedValue($this->createMailboxListString($this->_mailboxes));
 236          }
 237  
 238          return $this->getCachedValue();
 239      }
 240  
 241      // -- Points of extension
 242  
 243      /**
 244       * Normalizes a user-input list of mailboxes into consistent key=>value pairs.
 245       *
 246       * @param string[] $mailboxes
 247       *
 248       * @return string[]
 249       */
 250      protected function normalizeMailboxes(array $mailboxes)
 251      {
 252          $actualMailboxes = array();
 253  
 254          foreach ($mailboxes as $key => $value) {
 255              if (is_string($key)) {
 256                  //key is email addr
 257                  $address = $key;
 258                  $name = $value;
 259              } else {
 260                  $address = $value;
 261                  $name = null;
 262              }
 263              $this->_assertValidAddress($address);
 264              $actualMailboxes[$address] = $name;
 265          }
 266  
 267          return $actualMailboxes;
 268      }
 269  
 270      /**
 271       * Produces a compliant, formatted display-name based on the string given.
 272       *
 273       * @param string $displayName as displayed
 274       * @param bool   $shorten     the first line to make remove for header name
 275       *
 276       * @return string
 277       */
 278      protected function createDisplayNameString($displayName, $shorten = false)
 279      {
 280          return $this->createPhrase($this, $displayName,
 281              $this->getCharset(), $this->getEncoder(), $shorten
 282              );
 283      }
 284  
 285      /**
 286       * Creates a string form of all the mailboxes in the passed array.
 287       *
 288       * @param string[] $mailboxes
 289       *
 290       * @throws Swift_RfcComplianceException
 291       *
 292       * @return string
 293       */
 294      protected function createMailboxListString(array $mailboxes)
 295      {
 296          return implode(', ', $this->_createNameAddressStrings($mailboxes));
 297      }
 298  
 299      /**
 300       * Redefine the encoding requirements for mailboxes.
 301       *
 302       * Commas and semicolons are used to separate
 303       * multiple addresses, and should therefore be encoded
 304       *
 305       * @param string $token
 306       *
 307       * @return bool
 308       */
 309      protected function tokenNeedsEncoding($token)
 310      {
 311          return preg_match('/[,;]/', $token) || parent::tokenNeedsEncoding($token);
 312      }
 313  
 314      /**
 315       * Return an array of strings conforming the the name-addr spec of RFC 2822.
 316       *
 317       * @param string[] $mailboxes
 318       *
 319       * @return string[]
 320       */
 321      private function _createNameAddressStrings(array $mailboxes)
 322      {
 323          $strings = array();
 324  
 325          foreach ($mailboxes as $email => $name) {
 326              $mailboxStr = $email;
 327              if (!is_null($name)) {
 328                  $nameStr = $this->createDisplayNameString($name, empty($strings));
 329                  $mailboxStr = $nameStr.' <'.$mailboxStr.'>';
 330              }
 331              $strings[] = $mailboxStr;
 332          }
 333  
 334          return $strings;
 335      }
 336  
 337      /**
 338       * Throws an Exception if the address passed does not comply with RFC 2822.
 339       *
 340       * @param string $address
 341       *
 342       * @throws Swift_RfcComplianceException If invalid.
 343       */
 344      private function _assertValidAddress($address)
 345      {
 346          if (!preg_match('/^'.$this->getGrammar()->getDefinition('addr-spec').'$/D',
 347              $address)) {
 348              throw new Swift_RfcComplianceException(
 349                  'Address in mailbox given ['.$address.
 350                  '] does not comply with RFC 2822, 3.6.2.'
 351                  );
 352          }
 353      }
 354  }


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