TYPO3 API  SVNRelease
MailboxHeader.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/Mime/Headers/AbstractHeader.php';
00012 //@require 'Swift/Mime/HeaderEncoder.php';
00013 
00014 /**
00015  * A Mailbox Address MIME Header for something like From or Sender.
00016  * @package Swift
00017  * @subpackage Mime
00018  * @author Chris Corbyn
00019  */
00020 class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader
00021 {
00022 
00023   /**
00024    * The mailboxes used in this Header.
00025    * @var string[]
00026    * @access private
00027    */
00028   private $_mailboxes = array();
00029 
00030   /**
00031    * Creates a new MailboxHeader with $name.
00032    * @param string $name of Header
00033    * @param Swift_Mime_HeaderEncoder $encoder
00034    */
00035   public function __construct($name, Swift_Mime_HeaderEncoder $encoder)
00036   {
00037     $this->setFieldName($name);
00038     $this->setEncoder($encoder);
00039     $this->initializeGrammar();
00040   }
00041 
00042   /**
00043    * Get the type of Header that this instance represents.
00044    * @return int
00045    * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
00046    * @see TYPE_DATE, TYPE_ID, TYPE_PATH
00047    */
00048   public function getFieldType()
00049   {
00050     return self::TYPE_MAILBOX;
00051   }
00052 
00053   /**
00054    * Set the model for the field body.
00055    * This method takes a string, or an array of addresses.
00056    * @param mixed $model
00057    * @throws Swift_RfcComplianceException
00058    */
00059   public function setFieldBodyModel($model)
00060   {
00061     $this->setNameAddresses($model);
00062   }
00063 
00064   /**
00065    * Get the model for the field body.
00066    * This method returns an associative array like {@link getNameAddresses()}
00067    * @return array
00068    * @throws Swift_RfcComplianceException
00069    */
00070   public function getFieldBodyModel()
00071   {
00072     return $this->getNameAddresses();
00073   }
00074 
00075   /**
00076    * Set a list of mailboxes to be shown in this Header.
00077    * The mailboxes can be a simple array of addresses, or an array of
00078    * key=>value pairs where (email => personalName).
00079    * Example:
00080    * <code>
00081    * <?php
00082    * //Sets two mailboxes in the Header, one with a personal name
00083    * $header->setNameAddresses(array(
00084    *  'chris@swiftmailer.org' => 'Chris Corbyn',
00085    *  'mark@swiftmailer.org' //No associated personal name
00086    *  ));
00087    * ?>
00088    * </code>
00089    * @param string|string[] $mailboxes
00090    * @throws Swift_RfcComplianceException
00091    * @see __construct()
00092    * @see setAddresses()
00093    * @see setValue()
00094    */
00095   public function setNameAddresses($mailboxes)
00096   {
00097     $this->_mailboxes = $this->normalizeMailboxes((array) $mailboxes);
00098     $this->setCachedValue(null); //Clear any cached value
00099   }
00100 
00101   /**
00102    * Get the full mailbox list of this Header as an array of valid RFC 2822 strings.
00103    * Example:
00104    * <code>
00105    * <?php
00106    * $header = new Swift_Mime_Headers_MailboxHeader('From',
00107    *  array('chris@swiftmailer.org' => 'Chris Corbyn',
00108    *  'mark@swiftmailer.org' => 'Mark Corbyn')
00109    *  );
00110    * print_r($header->getNameAddressStrings());
00111    * // array (
00112    * // 0 => Chris Corbyn <chris@swiftmailer.org>,
00113    * // 1 => Mark Corbyn <mark@swiftmailer.org>
00114    * // )
00115    * ?>
00116    * </code>
00117    * @return string[]
00118    * @throws Swift_RfcComplianceException
00119    * @see getNameAddresses()
00120    * @see toString()
00121    */
00122   public function getNameAddressStrings()
00123   {
00124     return $this->_createNameAddressStrings($this->getNameAddresses());
00125   }
00126 
00127   /**
00128    * Get all mailboxes in this Header as key=>value pairs.
00129    * The key is the address and the value is the name (or null if none set).
00130    * Example:
00131    * <code>
00132    * <?php
00133    * $header = new Swift_Mime_Headers_MailboxHeader('From',
00134    *  array('chris@swiftmailer.org' => 'Chris Corbyn',
00135    *  'mark@swiftmailer.org' => 'Mark Corbyn')
00136    *  );
00137    * print_r($header->getNameAddresses());
00138    * // array (
00139    * // chris@swiftmailer.org => Chris Corbyn,
00140    * // mark@swiftmailer.org => Mark Corbyn
00141    * // )
00142    * ?>
00143    * </code>
00144    * @return string[]
00145    * @see getAddresses()
00146    * @see getNameAddressStrings()
00147    */
00148   public function getNameAddresses()
00149   {
00150     return $this->_mailboxes;
00151   }
00152 
00153   /**
00154    * Makes this Header represent a list of plain email addresses with no names.
00155    * Example:
00156    * <code>
00157    * <?php
00158    * //Sets three email addresses as the Header data
00159    * $header->setAddresses(
00160    *  array('one@domain.tld', 'two@domain.tld', 'three@domain.tld')
00161    *  );
00162    * ?>
00163    * </code>
00164    * @param string[] $addresses
00165    * @throws Swift_RfcComplianceException
00166    * @see setNameAddresses()
00167    * @see setValue()
00168    */
00169   public function setAddresses($addresses)
00170   {
00171     return $this->setNameAddresses(array_values((array) $addresses));
00172   }
00173 
00174   /**
00175    * Get all email addresses in this Header.
00176    * @return string[]
00177    * @see getNameAddresses()
00178    */
00179   public function getAddresses()
00180   {
00181     return array_keys($this->_mailboxes);
00182   }
00183 
00184   /**
00185    * Remove one or more addresses from this Header.
00186    * @param string|string[] $addresses
00187    */
00188   public function removeAddresses($addresses)
00189   {
00190     $this->setCachedValue(null);
00191     foreach ((array) $addresses as $address)
00192     {
00193       unset($this->_mailboxes[$address]);
00194     }
00195   }
00196 
00197   /**
00198    * Get the string value of the body in this Header.
00199    * This is not necessarily RFC 2822 compliant since folding white space will
00200    * not be added at this stage (see {@link toString()} for that).
00201    * @return string
00202    * @throws Swift_RfcComplianceException
00203    * @see toString()
00204    */
00205   public function getFieldBody()
00206   {
00207     //Compute the string value of the header only if needed
00208     if (is_null($this->getCachedValue()))
00209     {
00210       $this->setCachedValue($this->createMailboxListString($this->_mailboxes));
00211     }
00212     return $this->getCachedValue();
00213   }
00214 
00215   // -- Points of extension
00216 
00217   /**
00218    * Normalizes a user-input list of mailboxes into consistent key=>value pairs.
00219    * @param string[] $mailboxes
00220    * @return string[]
00221    * @access protected
00222    */
00223   protected function normalizeMailboxes(array $mailboxes)
00224   {
00225     $actualMailboxes = array();
00226 
00227     foreach ($mailboxes as $key => $value)
00228     {
00229       if (is_string($key)) //key is email addr
00230       {
00231         $address = $key;
00232         $name = $value;
00233       }
00234       else
00235       {
00236         $address = $value;
00237         $name = null;
00238       }
00239       $this->_assertValidAddress($address);
00240       $actualMailboxes[$address] = $name;
00241     }
00242 
00243     return $actualMailboxes;
00244   }
00245 
00246   /**
00247    * Produces a compliant, formatted display-name based on the string given.
00248    * @param string $displayName as displayed
00249    * @param boolean $shorten the first line to make remove for header name
00250    * @return string
00251    * @access protected
00252    */
00253   protected function createDisplayNameString($displayName, $shorten = false)
00254   {
00255     return $this->createPhrase($this, $displayName,
00256       $this->getCharset(), $this->getEncoder(), $shorten
00257       );
00258   }
00259 
00260   /**
00261    * Creates a string form of all the mailboxes in the passed array.
00262    * @param string[] $mailboxes
00263    * @return string
00264    * @throws Swift_RfcComplianceException
00265    * @access protected
00266    */
00267   protected function createMailboxListString(array $mailboxes)
00268   {
00269     return implode(', ', $this->_createNameAddressStrings($mailboxes));
00270   }
00271 
00272   // -- Private methods
00273 
00274   /**
00275    * Return an array of strings conforming the the name-addr spec of RFC 2822.
00276    * @param string[] $mailboxes
00277    * @return string[]
00278    * @access private
00279    */
00280   private function _createNameAddressStrings(array $mailboxes)
00281   {
00282     $strings = array();
00283 
00284     foreach ($mailboxes as $email => $name)
00285     {
00286       $mailboxStr = $email;
00287       if (!is_null($name))
00288       {
00289         $nameStr = $this->createDisplayNameString($name, empty($strings));
00290         $mailboxStr = $nameStr . ' <' . $mailboxStr . '>';
00291       }
00292       $strings[] = $mailboxStr;
00293     }
00294 
00295     return $strings;
00296   }
00297 
00298   /**
00299    * Throws an Exception if the address passed does not comply with RFC 2822.
00300    * @param string $address
00301    * @throws Exception If invalid.
00302    * @access protected
00303    */
00304   private function _assertValidAddress($address)
00305   {
00306     if (!preg_match('/^' . $this->getGrammar('addr-spec') . '$/D',
00307       $address))
00308     {
00309       throw new Swift_RfcComplianceException(
00310         'Address in mailbox given [' . $address .
00311         '] does not comply with RFC 2822, 3.6.2.'
00312         );
00313     }
00314   }
00315 
00316 }