vendor/swiftmailer/lib/classes/Swift/Mime/Headers/PathHeader.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  * A Path Header in Swift Mailer, such a Return-Path.
       
    14  * @package Swift
       
    15  * @subpackage Mime
       
    16  * @author Chris Corbyn
       
    17  */
       
    18 class Swift_Mime_Headers_PathHeader extends Swift_Mime_Headers_AbstractHeader
       
    19 {
       
    20   
       
    21   /**
       
    22    * The address in this Header (if specified).
       
    23    * @var string
       
    24    * @access private
       
    25    */
       
    26   private $_address;
       
    27   
       
    28   /**
       
    29    * Creates a new PathHeader with the given $name.
       
    30    * @param string $name
       
    31    * @param Swift_Mime_Grammar $grammar
       
    32    */
       
    33   public function __construct($name, Swift_Mime_Grammar $grammar)
       
    34   {
       
    35     $this->setFieldName($name);
       
    36     parent::__construct($grammar);
       
    37   }
       
    38   
       
    39   /**
       
    40    * Get the type of Header that this instance represents.
       
    41    * @return int
       
    42    * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
       
    43    * @see TYPE_DATE, TYPE_ID, TYPE_PATH
       
    44    */
       
    45   public function getFieldType()
       
    46   {
       
    47     return self::TYPE_PATH;
       
    48   }
       
    49   
       
    50   /**
       
    51    * Set the model for the field body.
       
    52    * This method takes a string for an address.
       
    53    * @param string $model
       
    54    * @throws Swift_RfcComplianceException
       
    55    */
       
    56   public function setFieldBodyModel($model)
       
    57   {
       
    58     $this->setAddress($model);
       
    59   }
       
    60   
       
    61   /**
       
    62    * Get the model for the field body.
       
    63    * This method returns a string email address.
       
    64    * @return mixed
       
    65    */
       
    66   public function getFieldBodyModel()
       
    67   {
       
    68     return $this->getAddress();
       
    69   }
       
    70   
       
    71   /**
       
    72    * Set the Address which should appear in this Header.
       
    73    * @param string $address
       
    74    * @throws Swift_RfcComplianceException
       
    75    */
       
    76   public function setAddress($address)
       
    77   {
       
    78     if (is_null($address))
       
    79     {
       
    80       $this->_address = null;
       
    81     }
       
    82     elseif ('' == $address)
       
    83     {
       
    84       $this->_address = '';
       
    85     }
       
    86     else
       
    87     {
       
    88       $this->_assertValidAddress($address);
       
    89       $this->_address = $address;
       
    90     }
       
    91     $this->setCachedValue(null);
       
    92   }
       
    93   
       
    94   /**
       
    95    * Get the address which is used in this Header (if any).
       
    96    * Null is returned if no address is set.
       
    97    * @return string
       
    98    */
       
    99   public function getAddress()
       
   100   {
       
   101     return $this->_address;
       
   102   }
       
   103   
       
   104   /**
       
   105    * Get the string value of the body in this Header.
       
   106    * This is not necessarily RFC 2822 compliant since folding white space will
       
   107    * not be added at this stage (see {@link toString()} for that).
       
   108    * @return string
       
   109    * @see toString()
       
   110    */
       
   111   public function getFieldBody()
       
   112   {
       
   113     if (!$this->getCachedValue())
       
   114     {
       
   115       if (isset($this->_address))
       
   116       {
       
   117         $this->setCachedValue('<' . $this->_address . '>');
       
   118       }
       
   119     }
       
   120     return $this->getCachedValue();
       
   121   }
       
   122   
       
   123   /**
       
   124    * Throws an Exception if the address passed does not comply with RFC 2822.
       
   125    * @param string $address
       
   126    * @throws Swift_RfcComplianceException If invalid.
       
   127    * @access private
       
   128    */
       
   129   private function _assertValidAddress($address)
       
   130   {
       
   131     if (!preg_match('/^' . $this->getGrammar()->getDefinition('addr-spec') . '$/D',
       
   132       $address))
       
   133     {
       
   134       throw new Swift_RfcComplianceException(
       
   135         'Address set in PathHeader does not comply with addr-spec of RFC 2822.'
       
   136         );
       
   137     }
       
   138   }
       
   139   
       
   140 }