TYPO3 API  SVNRelease
IdentificationHeader.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/RfcComplianceException.php';
00013 
00014 /**
00015  * An ID MIME Header for something like Message-ID or Content-ID.
00016  * @package Swift
00017  * @subpackage Mime
00018  * @author Chris Corbyn
00019  */
00020 class Swift_Mime_Headers_IdentificationHeader
00021   extends Swift_Mime_Headers_AbstractHeader
00022 {
00023 
00024   /**
00025    * The IDs used in the value of this Header.
00026    * This may hold multiple IDs or just a single ID.
00027    * @var string[]
00028    * @access private
00029    */
00030   private $_ids = array();
00031 
00032   /**
00033    * Creates a new IdentificationHeader with the given $name and $id.
00034    * @param string $name
00035    */
00036   public function __construct($name)
00037   {
00038     $this->setFieldName($name);
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_ID;
00051   }
00052 
00053   /**
00054    * Set the model for the field body.
00055    * This method takes a string ID, or an array of IDs
00056    * @param mixed $model
00057    * @throws Swift_RfcComplianceException
00058    */
00059   public function setFieldBodyModel($model)
00060   {
00061     $this->setId($model);
00062   }
00063 
00064   /**
00065    * Get the model for the field body.
00066    * This method returns an array of IDs
00067    * @return array
00068    */
00069   public function getFieldBodyModel()
00070   {
00071     return $this->getIds();
00072   }
00073 
00074   /**
00075    * Set the ID used in the value of this header.
00076    * @param string $id
00077    * @throws Swift_RfcComplianceException
00078    */
00079   public function setId($id)
00080   {
00081     return $this->setIds(array($id));
00082   }
00083 
00084   /**
00085    * Get the ID used in the value of this Header.
00086    * If multiple IDs are set only the first is returned.
00087    * @return string
00088    */
00089   public function getId()
00090   {
00091     if (count($this->_ids) > 0)
00092     {
00093       return $this->_ids[0];
00094     }
00095   }
00096 
00097   /**
00098    * Set a collection of IDs to use in the value of this Header.
00099    * @param string[] $ids
00100    * @throws Swift_RfcComplianceException
00101    */
00102   public function setIds(array $ids)
00103   {
00104     $actualIds = array();
00105 
00106     foreach ($ids as $k => $id)
00107     {
00108       if (preg_match(
00109         '/^' . $this->getGrammar('id-left') . '@' .
00110         $this->getGrammar('id-right') . '$/D',
00111         $id
00112         ))
00113       {
00114         $actualIds[] = $id;
00115       }
00116       else
00117       {
00118         throw new Swift_RfcComplianceException(
00119           'Invalid ID given <' . $id . '>'
00120           );
00121       }
00122     }
00123 
00124     $this->clearCachedValueIf($this->_ids != $actualIds);
00125     $this->_ids = $actualIds;
00126   }
00127 
00128   /**
00129    * Get the list of IDs used in this Header.
00130    * @return string[]
00131    */
00132   public function getIds()
00133   {
00134     return $this->_ids;
00135   }
00136 
00137   /**
00138    * Get the string value of the body in this Header.
00139    * This is not necessarily RFC 2822 compliant since folding white space will
00140    * not be added at this stage (see {@link toString()} for that).
00141    * @return string
00142    * @see toString()
00143    * @throws Swift_RfcComplianceException
00144    */
00145   public function getFieldBody()
00146   {
00147     if (!$this->getCachedValue())
00148     {
00149       $angleAddrs = array();
00150 
00151       foreach ($this->_ids as $id)
00152       {
00153         $angleAddrs[] = '<' . $id . '>';
00154       }
00155 
00156       $this->setCachedValue(implode(' ', $angleAddrs));
00157     }
00158     return $this->getCachedValue();
00159   }
00160 
00161 }