[ Index ]

MailPress 7.1

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

title

Body

[close]

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


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1