vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/SimpleHeaderFactory.php line 61

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of SwiftMailer.
  4.  * (c) 2004-2009 Chris Corbyn
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. use Egulias\EmailValidator\EmailValidator;
  10. /**
  11.  * Creates MIME headers.
  12.  *
  13.  * @author Chris Corbyn
  14.  */
  15. class Swift_Mime_SimpleHeaderFactory implements Swift_Mime_CharsetObserver
  16. {
  17.     /** The HeaderEncoder used by these headers */
  18.     private $encoder;
  19.     /** The Encoder used by parameters */
  20.     private $paramEncoder;
  21.     /** Strict EmailValidator */
  22.     private $emailValidator;
  23.     /** The charset of created Headers */
  24.     private $charset;
  25.     /** Swift_AddressEncoder */
  26.     private $addressEncoder;
  27.     /**
  28.      * Creates a new SimpleHeaderFactory using $encoder and $paramEncoder.
  29.      *
  30.      * @param string|null $charset
  31.      */
  32.     public function __construct(Swift_Mime_HeaderEncoder $encoderSwift_Encoder $paramEncoderEmailValidator $emailValidator$charset nullSwift_AddressEncoder $addressEncoder null)
  33.     {
  34.         $this->encoder $encoder;
  35.         $this->paramEncoder $paramEncoder;
  36.         $this->emailValidator $emailValidator;
  37.         $this->charset $charset;
  38.         $this->addressEncoder $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder();
  39.     }
  40.     /**
  41.      * Create a new Mailbox Header with a list of $addresses.
  42.      *
  43.      * @param string            $name
  44.      * @param array|string|null $addresses
  45.      *
  46.      * @return Swift_Mime_Header
  47.      */
  48.     public function createMailboxHeader($name$addresses null)
  49.     {
  50.         $header = new Swift_Mime_Headers_MailboxHeader($name$this->encoder$this->emailValidator$this->addressEncoder);
  51.         if (isset($addresses)) {
  52.             $header->setFieldBodyModel($addresses);
  53.         }
  54.         $this->setHeaderCharset($header);
  55.         return $header;
  56.     }
  57.     /**
  58.      * Create a new Date header using $dateTime.
  59.      *
  60.      * @param string $name
  61.      *
  62.      * @return Swift_Mime_Header
  63.      */
  64.     public function createDateHeader($nameDateTimeInterface $dateTime null)
  65.     {
  66.         $header = new Swift_Mime_Headers_DateHeader($name);
  67.         if (isset($dateTime)) {
  68.             $header->setFieldBodyModel($dateTime);
  69.         }
  70.         $this->setHeaderCharset($header);
  71.         return $header;
  72.     }
  73.     /**
  74.      * Create a new basic text header with $name and $value.
  75.      *
  76.      * @param string $name
  77.      * @param string $value
  78.      *
  79.      * @return Swift_Mime_Header
  80.      */
  81.     public function createTextHeader($name$value null)
  82.     {
  83.         $header = new Swift_Mime_Headers_UnstructuredHeader($name$this->encoder);
  84.         if (isset($value)) {
  85.             $header->setFieldBodyModel($value);
  86.         }
  87.         $this->setHeaderCharset($header);
  88.         return $header;
  89.     }
  90.     /**
  91.      * Create a new ParameterizedHeader with $name, $value and $params.
  92.      *
  93.      * @param string $name
  94.      * @param string $value
  95.      * @param array  $params
  96.      *
  97.      * @return Swift_Mime_Headers_ParameterizedHeader
  98.      */
  99.     public function createParameterizedHeader($name$value null$params = [])
  100.     {
  101.         $header = new Swift_Mime_Headers_ParameterizedHeader($name$this->encoder, ('content-disposition' == strtolower($name ?? '')) ? $this->paramEncoder null);
  102.         if (isset($value)) {
  103.             $header->setFieldBodyModel($value);
  104.         }
  105.         foreach ($params as $k => $v) {
  106.             $header->setParameter($k$v);
  107.         }
  108.         $this->setHeaderCharset($header);
  109.         return $header;
  110.     }
  111.     /**
  112.      * Create a new ID header for Message-ID or Content-ID.
  113.      *
  114.      * @param string       $name
  115.      * @param string|array $ids
  116.      *
  117.      * @return Swift_Mime_Header
  118.      */
  119.     public function createIdHeader($name$ids null)
  120.     {
  121.         $header = new Swift_Mime_Headers_IdentificationHeader($name$this->emailValidator);
  122.         if (isset($ids)) {
  123.             $header->setFieldBodyModel($ids);
  124.         }
  125.         $this->setHeaderCharset($header);
  126.         return $header;
  127.     }
  128.     /**
  129.      * Create a new Path header with an address (path) in it.
  130.      *
  131.      * @param string $name
  132.      * @param string $path
  133.      *
  134.      * @return Swift_Mime_Header
  135.      */
  136.     public function createPathHeader($name$path null)
  137.     {
  138.         $header = new Swift_Mime_Headers_PathHeader($name$this->emailValidator);
  139.         if (isset($path)) {
  140.             $header->setFieldBodyModel($path);
  141.         }
  142.         $this->setHeaderCharset($header);
  143.         return $header;
  144.     }
  145.     /**
  146.      * Notify this observer that the entity's charset has changed.
  147.      *
  148.      * @param string $charset
  149.      */
  150.     public function charsetChanged($charset)
  151.     {
  152.         $this->charset $charset;
  153.         $this->encoder->charsetChanged($charset);
  154.         $this->paramEncoder->charsetChanged($charset);
  155.     }
  156.     /**
  157.      * Make a deep copy of object.
  158.      */
  159.     public function __clone()
  160.     {
  161.         $this->encoder = clone $this->encoder;
  162.         $this->paramEncoder = clone $this->paramEncoder;
  163.         $this->addressEncoder = clone $this->addressEncoder;
  164.     }
  165.     /** Apply the charset to the Header */
  166.     private function setHeaderCharset(Swift_Mime_Header $header)
  167.     {
  168.         if (isset($this->charset)) {
  169.             $header->setCharset($this->charset);
  170.         }
  171.     }
  172. }