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.      * @param DateTimeInterface|null $dateTime
  62.      *
  63.      * @return Swift_Mime_Header
  64.      */
  65.     public function createDateHeader($nameDateTimeInterface $dateTime null)
  66.     {
  67.         $header = new Swift_Mime_Headers_DateHeader($name);
  68.         if (isset($dateTime)) {
  69.             $header->setFieldBodyModel($dateTime);
  70.         }
  71.         $this->setHeaderCharset($header);
  72.         return $header;
  73.     }
  74.     /**
  75.      * Create a new basic text header with $name and $value.
  76.      *
  77.      * @param string $name
  78.      * @param string $value
  79.      *
  80.      * @return Swift_Mime_Header
  81.      */
  82.     public function createTextHeader($name$value null)
  83.     {
  84.         $header = new Swift_Mime_Headers_UnstructuredHeader($name$this->encoder);
  85.         if (isset($value)) {
  86.             $header->setFieldBodyModel($value);
  87.         }
  88.         $this->setHeaderCharset($header);
  89.         return $header;
  90.     }
  91.     /**
  92.      * Create a new ParameterizedHeader with $name, $value and $params.
  93.      *
  94.      * @param string $name
  95.      * @param string $value
  96.      * @param array  $params
  97.      *
  98.      * @return Swift_Mime_Headers_ParameterizedHeader
  99.      */
  100.     public function createParameterizedHeader($name$value null$params = [])
  101.     {
  102.         $header = new Swift_Mime_Headers_ParameterizedHeader($name$this->encoder, ('content-disposition' == strtolower($name)) ? $this->paramEncoder null);
  103.         if (isset($value)) {
  104.             $header->setFieldBodyModel($value);
  105.         }
  106.         foreach ($params as $k => $v) {
  107.             $header->setParameter($k$v);
  108.         }
  109.         $this->setHeaderCharset($header);
  110.         return $header;
  111.     }
  112.     /**
  113.      * Create a new ID header for Message-ID or Content-ID.
  114.      *
  115.      * @param string       $name
  116.      * @param string|array $ids
  117.      *
  118.      * @return Swift_Mime_Header
  119.      */
  120.     public function createIdHeader($name$ids null)
  121.     {
  122.         $header = new Swift_Mime_Headers_IdentificationHeader($name$this->emailValidator);
  123.         if (isset($ids)) {
  124.             $header->setFieldBodyModel($ids);
  125.         }
  126.         $this->setHeaderCharset($header);
  127.         return $header;
  128.     }
  129.     /**
  130.      * Create a new Path header with an address (path) in it.
  131.      *
  132.      * @param string $name
  133.      * @param string $path
  134.      *
  135.      * @return Swift_Mime_Header
  136.      */
  137.     public function createPathHeader($name$path null)
  138.     {
  139.         $header = new Swift_Mime_Headers_PathHeader($name$this->emailValidator);
  140.         if (isset($path)) {
  141.             $header->setFieldBodyModel($path);
  142.         }
  143.         $this->setHeaderCharset($header);
  144.         return $header;
  145.     }
  146.     /**
  147.      * Notify this observer that the entity's charset has changed.
  148.      *
  149.      * @param string $charset
  150.      */
  151.     public function charsetChanged($charset)
  152.     {
  153.         $this->charset $charset;
  154.         $this->encoder->charsetChanged($charset);
  155.         $this->paramEncoder->charsetChanged($charset);
  156.     }
  157.     /**
  158.      * Make a deep copy of object.
  159.      */
  160.     public function __clone()
  161.     {
  162.         $this->encoder = clone $this->encoder;
  163.         $this->paramEncoder = clone $this->paramEncoder;
  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. }