vendor/swiftmailer/lib/classes/Swift/Mime/Headers/IdentificationHeader.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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 /**
       
    13  * An ID MIME Header for something like Message-ID or Content-ID.
       
    14  * @package Swift
       
    15  * @subpackage Mime
       
    16  * @author Chris Corbyn
       
    17  */
       
    18 class Swift_Mime_Headers_IdentificationHeader
       
    19   extends Swift_Mime_Headers_AbstractHeader
       
    20 {
       
    21   
       
    22   /**
       
    23    * The IDs used in the value of this Header.
       
    24    * This may hold multiple IDs or just a single ID.
       
    25    * @var string[]
       
    26    * @access private
       
    27    */
       
    28   private $_ids = array();
       
    29   
       
    30   /**
       
    31    * Creates a new IdentificationHeader with the given $name and $id.
       
    32    * @param string $name
       
    33    * @param Swift_Mime_Grammar $grammar
       
    34    */
       
    35   public function __construct($name, Swift_Mime_Grammar $grammar)
       
    36   {
       
    37     $this->setFieldName($name);
       
    38     parent::__construct($grammar);
       
    39   }
       
    40   
       
    41   /**
       
    42    * Get the type of Header that this instance represents.
       
    43    * @return int
       
    44    * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
       
    45    * @see TYPE_DATE, TYPE_ID, TYPE_PATH
       
    46    */
       
    47   public function getFieldType()
       
    48   {
       
    49     return self::TYPE_ID;
       
    50   }
       
    51   
       
    52   /**
       
    53    * Set the model for the field body.
       
    54    * This method takes a string ID, or an array of IDs
       
    55    * @param mixed $model
       
    56    * @throws Swift_RfcComplianceException
       
    57    */
       
    58   public function setFieldBodyModel($model)
       
    59   {
       
    60     $this->setId($model);
       
    61   }
       
    62   
       
    63   /**
       
    64    * Get the model for the field body.
       
    65    * This method returns an array of IDs
       
    66    * @return array
       
    67    */
       
    68   public function getFieldBodyModel()
       
    69   {
       
    70     return $this->getIds();
       
    71   }
       
    72   
       
    73   /**
       
    74    * Set the ID used in the value of this header.
       
    75    * @param string $id
       
    76    * @throws Swift_RfcComplianceException
       
    77    */
       
    78   public function setId($id)
       
    79   {
       
    80     $this->setIds(array($id));
       
    81   }
       
    82   
       
    83   /**
       
    84    * Get the ID used in the value of this Header.
       
    85    * If multiple IDs are set only the first is returned.
       
    86    * @return string
       
    87    */
       
    88   public function getId()
       
    89   {
       
    90     if (count($this->_ids) > 0)
       
    91     {
       
    92       return $this->_ids[0];
       
    93     }
       
    94   }
       
    95   
       
    96   /**
       
    97    * Set a collection of IDs to use in the value of this Header.
       
    98    * @param string[] $ids
       
    99    * @throws Swift_RfcComplianceException
       
   100    */
       
   101   public function setIds(array $ids)
       
   102   {
       
   103     $actualIds = array();
       
   104     
       
   105     foreach ($ids as $k => $id)
       
   106     {
       
   107       $this->_assertValidId($id);
       
   108       $actualIds[] = $id;
       
   109     }
       
   110     
       
   111     $this->clearCachedValueIf($this->_ids != $actualIds);
       
   112     $this->_ids = $actualIds;
       
   113   }
       
   114   
       
   115   /**
       
   116    * Get the list of IDs used in this Header.
       
   117    * @return string[]
       
   118    */
       
   119   public function getIds()
       
   120   {
       
   121     return $this->_ids;
       
   122   }
       
   123   
       
   124   /**
       
   125    * Get the string value of the body in this Header.
       
   126    * This is not necessarily RFC 2822 compliant since folding white space will
       
   127    * not be added at this stage (see {@link toString()} for that).
       
   128    * @return string
       
   129    * @see toString()
       
   130    * @throws Swift_RfcComplianceException
       
   131    */
       
   132   public function getFieldBody()
       
   133   {
       
   134     if (!$this->getCachedValue())
       
   135     {
       
   136       $angleAddrs = array();
       
   137     
       
   138       foreach ($this->_ids as $id)
       
   139       {
       
   140         $angleAddrs[] = '<' . $id . '>';
       
   141       }
       
   142     
       
   143       $this->setCachedValue(implode(' ', $angleAddrs));
       
   144     }
       
   145     return $this->getCachedValue();
       
   146   }
       
   147   
       
   148   /**
       
   149    * Throws an Exception if the id passed does not comply with RFC 2822.
       
   150    * @param string $id
       
   151    * @throws Swift_RfcComplianceException
       
   152    */
       
   153   private function _assertValidId($id)
       
   154   {
       
   155     if (!preg_match(
       
   156       '/^' . $this->getGrammar()->getDefinition('id-left') . '@' .
       
   157       $this->getGrammar()->getDefinition('id-right') . '$/D',
       
   158       $id
       
   159       ))
       
   160     {
       
   161       throw new Swift_RfcComplianceException(
       
   162         'Invalid ID given <' . $id . '>'
       
   163         );
       
   164     }
       
   165   }
       
   166 }