[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Mime/Headers/ -> IdentificationHeader.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   * An ID MIME Header for something like Message-ID or Content-ID.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_Headers_IdentificationHeader extends Swift_Mime_Headers_AbstractHeader
  17  {
  18      /**
  19       * The IDs used in the value of this Header.
  20       *
  21       * This may hold multiple IDs or just a single ID.
  22       *
  23       * @var string[]
  24       */
  25      private $_ids = array();
  26  
  27      /**
  28       * Creates a new IdentificationHeader with the given $name and $id.
  29       *
  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       *
  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_ID;
  50      }
  51  
  52      /**
  53       * Set the model for the field body.
  54       *
  55       * This method takes a string ID, or an array of IDs.
  56       *
  57       * @param mixed $model
  58       *
  59       * @throws Swift_RfcComplianceException
  60       */
  61      public function setFieldBodyModel($model)
  62      {
  63          $this->setId($model);
  64      }
  65  
  66      /**
  67       * Get the model for the field body.
  68       *
  69       * This method returns an array of IDs
  70       *
  71       * @return array
  72       */
  73      public function getFieldBodyModel()
  74      {
  75          return $this->getIds();
  76      }
  77  
  78      /**
  79       * Set the ID used in the value of this header.
  80       *
  81       * @param string|array $id
  82       *
  83       * @throws Swift_RfcComplianceException
  84       */
  85      public function setId($id)
  86      {
  87          $this->setIds(is_array($id) ? $id : array($id));
  88      }
  89  
  90      /**
  91       * Get the ID used in the value of this Header.
  92       *
  93       * If multiple IDs are set only the first is returned.
  94       *
  95       * @return string
  96       */
  97      public function getId()
  98      {
  99          if (count($this->_ids) > 0) {
 100              return $this->_ids[0];
 101          }
 102      }
 103  
 104      /**
 105       * Set a collection of IDs to use in the value of this Header.
 106       *
 107       * @param string[] $ids
 108       *
 109       * @throws Swift_RfcComplianceException
 110       */
 111      public function setIds(array $ids)
 112      {
 113          $actualIds = array();
 114  
 115          foreach ($ids as $id) {
 116              $this->_assertValidId($id);
 117              $actualIds[] = $id;
 118          }
 119  
 120          $this->clearCachedValueIf($this->_ids != $actualIds);
 121          $this->_ids = $actualIds;
 122      }
 123  
 124      /**
 125       * Get the list of IDs used in this Header.
 126       *
 127       * @return string[]
 128       */
 129      public function getIds()
 130      {
 131          return $this->_ids;
 132      }
 133  
 134      /**
 135       * Get the string value of the body in this Header.
 136       *
 137       * This is not necessarily RFC 2822 compliant since folding white space will
 138       * not be added at this stage (see {@see toString()} for that).
 139       *
 140       * @see toString()
 141       *
 142       * @throws Swift_RfcComplianceException
 143       *
 144       * @return string
 145       */
 146      public function getFieldBody()
 147      {
 148          if (!$this->getCachedValue()) {
 149              $angleAddrs = array();
 150  
 151              foreach ($this->_ids as $id) {
 152                  $angleAddrs[] = '<'.$id.'>';
 153              }
 154  
 155              $this->setCachedValue(implode(' ', $angleAddrs));
 156          }
 157  
 158          return $this->getCachedValue();
 159      }
 160  
 161      /**
 162       * Throws an Exception if the id passed does not comply with RFC 2822.
 163       *
 164       * @param string $id
 165       *
 166       * @throws Swift_RfcComplianceException
 167       */
 168      private function _assertValidId($id)
 169      {
 170          if (!preg_match(
 171              '/^'.$this->getGrammar()->getDefinition('id-left').'@'.
 172              $this->getGrammar()->getDefinition('id-right').'$/D',
 173              $id
 174              )) {
 175              throw new Swift_RfcComplianceException(
 176                  'Invalid ID given <'.$id.'>'
 177                  );
 178          }
 179      }
 180  }


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