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 * Creates MIME headers.
13 *
14 * @author Chris Corbyn
15 */
16class Swift_Mime_SimpleHeaderFactory implements Swift_Mime_HeaderFactory
17{
18    /** The HeaderEncoder used by these headers */
19    private $_encoder;
20
21    /** The Encoder used by parameters */
22    private $_paramEncoder;
23
24    /** The Grammar */
25    private $_grammar;
26
27    /** The charset of created Headers */
28    private $_charset;
29
30    /**
31     * Creates a new SimpleHeaderFactory using $encoder and $paramEncoder.
32     *
33     * @param Swift_Mime_HeaderEncoder $encoder
34     * @param Swift_Encoder            $paramEncoder
35     * @param Swift_Mime_Grammar       $grammar
36     * @param string|null              $charset
37     */
38    public function __construct(Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder, Swift_Mime_Grammar $grammar, $charset = null)
39    {
40        $this->_encoder = $encoder;
41        $this->_paramEncoder = $paramEncoder;
42        $this->_grammar = $grammar;
43        $this->_charset = $charset;
44    }
45
46    /**
47     * Create a new Mailbox Header with a list of $addresses.
48     *
49     * @param string            $name
50     * @param array|string|null $addresses
51     *
52     * @return Swift_Mime_Header
53     */
54    public function createMailboxHeader($name, $addresses = null)
55    {
56        $header = new Swift_Mime_Headers_MailboxHeader($name, $this->_encoder, $this->_grammar);
57        if (isset($addresses)) {
58            $header->setFieldBodyModel($addresses);
59        }
60        $this->_setHeaderCharset($header);
61
62        return $header;
63    }
64
65    /**
66     * Create a new Date header using $timestamp (UNIX time).
67     *
68     * @param string   $name
69     * @param int|null $timestamp
70     *
71     * @return Swift_Mime_Header
72     */
73    public function createDateHeader($name, $timestamp = null)
74    {
75        $header = new Swift_Mime_Headers_DateHeader($name, $this->_grammar);
76        if (isset($timestamp)) {
77            $header->setFieldBodyModel($timestamp);
78        }
79        $this->_setHeaderCharset($header);
80
81        return $header;
82    }
83
84    /**
85     * Create a new basic text header with $name and $value.
86     *
87     * @param string $name
88     * @param string $value
89     *
90     * @return Swift_Mime_Header
91     */
92    public function createTextHeader($name, $value = null)
93    {
94        $header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->_encoder, $this->_grammar);
95        if (isset($value)) {
96            $header->setFieldBodyModel($value);
97        }
98        $this->_setHeaderCharset($header);
99
100        return $header;
101    }
102
103    /**
104     * Create a new ParameterizedHeader with $name, $value and $params.
105     *
106     * @param string $name
107     * @param string $value
108     * @param array  $params
109     *
110     * @return Swift_Mime_ParameterizedHeader
111     */
112    public function createParameterizedHeader($name, $value = null,
113        $params = array())
114    {
115        $header = new Swift_Mime_Headers_ParameterizedHeader($name, $this->_encoder, strtolower($name) == 'content-disposition' ? $this->_paramEncoder : null, $this->_grammar);
116        if (isset($value)) {
117            $header->setFieldBodyModel($value);
118        }
119        foreach ($params as $k => $v) {
120            $header->setParameter($k, $v);
121        }
122        $this->_setHeaderCharset($header);
123
124        return $header;
125    }
126
127    /**
128     * Create a new ID header for Message-ID or Content-ID.
129     *
130     * @param string       $name
131     * @param string|array $ids
132     *
133     * @return Swift_Mime_Header
134     */
135    public function createIdHeader($name, $ids = null)
136    {
137        $header = new Swift_Mime_Headers_IdentificationHeader($name, $this->_grammar);
138        if (isset($ids)) {
139            $header->setFieldBodyModel($ids);
140        }
141        $this->_setHeaderCharset($header);
142
143        return $header;
144    }
145
146    /**
147     * Create a new Path header with an address (path) in it.
148     *
149     * @param string $name
150     * @param string $path
151     *
152     * @return Swift_Mime_Header
153     */
154    public function createPathHeader($name, $path = null)
155    {
156        $header = new Swift_Mime_Headers_PathHeader($name, $this->_grammar);
157        if (isset($path)) {
158            $header->setFieldBodyModel($path);
159        }
160        $this->_setHeaderCharset($header);
161
162        return $header;
163    }
164
165    /**
166     * Notify this observer that the entity's charset has changed.
167     *
168     * @param string $charset
169     */
170    public function charsetChanged($charset)
171    {
172        $this->_charset = $charset;
173        $this->_encoder->charsetChanged($charset);
174        $this->_paramEncoder->charsetChanged($charset);
175    }
176
177    /**
178     * Make a deep copy of object.
179     */
180    public function __clone()
181    {
182        $this->_encoder = clone $this->_encoder;
183        $this->_paramEncoder = clone $this->_paramEncoder;
184    }
185
186    /** Apply the charset to the Header */
187    private function _setHeaderCharset(Swift_Mime_Header $header)
188    {
189        if (isset($this->_charset)) {
190            $header->setCharset($this->_charset);
191        }
192    }
193}
194