Completed
Push — 5.x ( 7e5ff6...c87723 )
by Lars
09:55
created

Swift_Mime_SimpleHeaderSet::addIdHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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
 * A collection of MIME headers.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * HeaderFactory
20
     *
21
     * @var Swift_Mime_HeaderFactory
22
     */
23
    private $_factory;
24
25
    /**
26
     * Collection of set Headers
27
     *
28
     * @var array[]
29
     */
30
    private $_headers = array();
31
32
    /**
33
     * Field ordering details
34
     *
35
     * @var array
36
     */
37
    private $_order = array();
38
39
    /**
40
     * List of fields which are required to be displayed
41
     *
42
     * @var array
43
     */
44
    private $_required = array();
45
46
    /**
47
     * The charset used by Headers
48
     */
49
    private $_charset;
50
51
    /**
52
     * Create a new SimpleHeaderSet with the given $factory.
53
     *
54
     * @param Swift_Mime_HeaderFactory $factory
55
     * @param string                   $charset
56
     */
57 190
    public function __construct(Swift_Mime_HeaderFactory $factory, $charset = null)
58
    {
59 190
        $this->_factory = $factory;
60 190
        if (isset($charset)) {
61 44
            $this->setCharset($charset);
62
        }
63 190
    }
64
65
    /**
66
     * Set the charset used by these headers.
67
     *
68
     * @param string $charset
69
     */
70 132
    public function setCharset($charset)
71
    {
72 132
        $this->_charset = $charset;
73 132
        $this->_factory->charsetChanged($charset);
74 132
        $this->_notifyHeadersOfCharset($charset);
75 132
    }
76
77
    /**
78
     * Add a new Mailbox Header with a list of $addresses.
79
     *
80
     * @param string       $name
81
     * @param array|string $addresses
82
     */
83 111
    public function addMailboxHeader($name, $addresses = null)
84
    {
85 111
        $this->_storeHeader($name,
86 111
        $this->_factory->createMailboxHeader($name, $addresses));
87 111
    }
88
89
    /**
90
     * Add a new Date header using $timestamp (UNIX time).
91
     *
92
     * @param string $name
93
     * @param int    $timestamp
94
     */
95 111
    public function addDateHeader($name, $timestamp = null)
96
    {
97 111
        $this->_storeHeader($name,
98 111
        $this->_factory->createDateHeader($name, $timestamp));
99 111
    }
100
101
    /**
102
     * Add a new basic text header with $name and $value.
103
     *
104
     * @param string $name
105
     * @param string $value
106
     */
107 157
    public function addTextHeader($name, $value = null)
108
    {
109 157
        $this->_storeHeader($name,
110 157
        $this->_factory->createTextHeader($name, $value));
111 157
    }
112
113
    /**
114
     * Add a new ParameterizedHeader with $name, $value and $params.
115
     *
116
     * @param string $name
117
     * @param string $value
118
     * @param array  $params
119
     */
120 145
    public function addParameterizedHeader($name, $value = null, $params = array())
121
    {
122 145
        $this->_storeHeader($name, $this->_factory->createParameterizedHeader($name, $value, $params));
123 145
    }
124
125
    /**
126
     * Add a new ID header for Message-ID or Content-ID.
127
     *
128
     * @param string       $name
129
     * @param string|array $ids
130
     */
131 140
    public function addIdHeader($name, $ids = null)
132
    {
133 140
        $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids));
134 140
    }
135
136
    /**
137
     * Add a new Path header with an address (path) in it.
138
     *
139
     * @param string $name
140
     * @param string $path
141
     */
142 32
    public function addPathHeader($name, $path = null)
143
    {
144 32
        $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path));
145 32
    }
146
147
    /**
148
     * Returns true if at least one header with the given $name exists.
149
     *
150
     * If multiple headers match, the actual one may be specified by $index.
151
     *
152
     * @param string $name
153
     * @param int    $index
154
     *
155
     * @return bool
156
     */
157 171
    public function has($name, $index = 0)
158
    {
159 171
        $lowerName = strtolower($name);
160
161 171
        return array_key_exists($lowerName, $this->_headers) && array_key_exists($index, $this->_headers[$lowerName]);
162
    }
163
164
    /**
165
     * Set a header in the HeaderSet.
166
     *
167
     * The header may be a previously fetched header via {@link get()} or it may
168
     * be one that has been created separately.
169
     *
170
     * If $index is specified, the header will be inserted into the set at this
171
     * offset.
172
     *
173
     * @param Swift_Mime_Header $header
174
     * @param int               $index
175
     */
176 5
    public function set(Swift_Mime_Header $header, $index = 0)
177
    {
178 5
        $this->_storeHeader($header->getFieldName(), $header, $index);
179 5
    }
180
181
    /**
182
     * Get the header with the given $name.
183
     *
184
     * If multiple headers match, the actual one may be specified by $index.
185
     * Returns NULL if none present.
186
     *
187
     * @param string $name
188
     * @param int    $index
189
     *
190
     * @return Swift_Mime_Header
191
     */
192 147
    public function get($name, $index = 0)
193
    {
194 147
        if ($this->has($name, $index)) {
195 146
            $lowerName = strtolower($name);
196
197 146
            return $this->_headers[$lowerName][$index];
198
        }
199 1
    }
200
201
    /**
202
     * Get all headers with the given $name.
203
     *
204
     * @param string $name
205
     *
206
     * @return array
207
     */
208 21
    public function getAll($name = null)
209
    {
210 21
        if (!isset($name)) {
211 1
            $headers = array();
212 1
            foreach ($this->_headers as $collection) {
213 1
                $headers = array_merge($headers, $collection);
214
            }
215
216 1
            return $headers;
217
        }
218
219 20
        $lowerName = strtolower($name);
220 20
        if (!array_key_exists($lowerName, $this->_headers)) {
221 10
            return array();
222
        }
223
224 19
        return $this->_headers[$lowerName];
225
    }
226
227
    /**
228
     * Return the name of all Headers.
229
     *
230
     * @return array
231
     */
232 5
    public function listAll()
233
    {
234 5
        $headers = $this->_headers;
235 5
        if ($this->_canSort()) {
236
            uksort($headers, array($this, '_sortHeaders'));
237
        }
238
239 5
        return array_keys($headers);
240
    }
241
242
    /**
243
     * Remove the header with the given $name if it's set.
244
     *
245
     * If multiple headers match, the actual one may be specified by $index.
246
     *
247
     * @param string $name
248
     * @param int    $index
249
     */
250 48
    public function remove($name, $index = 0)
251
    {
252 48
        $lowerName = strtolower($name);
253 48
        if (array_key_exists($lowerName, $this->_headers)) {
254 48
            array_splice($this->_headers[$lowerName], $index, 1);
255
256 48
            if (empty($this->_headers[$lowerName])) {
257 46
                $this->removeAll($lowerName);
258
            }
259
        }
260 48
    }
261
262
    /**
263
     * Remove all headers with the given $name.
264
     *
265
     * @param string $name
266
     */
267 51
    public function removeAll($name)
268
    {
269 51
        $lowerName = strtolower($name);
270 51
        unset($this->_headers[$lowerName]);
271 51
    }
272
273
    /**
274
     * Create a new instance of this HeaderSet.
275
     *
276
     * @return Swift_Mime_HeaderSet
277
     */
278 26
    public function newInstance()
279
    {
280 26
        return new self($this->_factory);
281
    }
282
283
    /**
284
     * Define a list of Header names as an array in the correct order.
285
     *
286
     * These Headers will be output in the given order where present.
287
     *
288
     * @param array $sequence
289
     */
290 145
    public function defineOrdering(array $sequence)
291
    {
292 145
        $this->_order = array_flip(array_map('strtolower', $sequence));
293 145
    }
294
295
    /**
296
     * Set a list of header names which must always be displayed when set.
297
     *
298
     * Usually headers without a field value won't be output unless set here.
299
     *
300
     * @param array $names
301
     */
302 110
    public function setAlwaysDisplayed(array $names)
303
    {
304 110
        $this->_required = array_flip(array_map('strtolower', $names));
305 110
    }
306
307
    /**
308
     * Notify this observer that the entity's charset has changed.
309
     *
310
     * @param string $charset
311
     */
312 118
    public function charsetChanged($charset)
313
    {
314 118
        $this->setCharset($charset);
315 118
    }
316
317
    /**
318
     * Returns a string with a representation of all headers.
319
     *
320
     * @return string
321
     */
322 135
    public function toString()
323
    {
324 135
        $string = '';
325 135
        $headers = $this->_headers;
326 135
        if ($this->_canSort()) {
327 132
            uksort($headers, array($this, '_sortHeaders'));
328
        }
329 135
        foreach ($headers as $collection) {
330 135
            foreach ($collection as $header) {
331
                /* @var $header Swift_Mime_Header */
332 135
                if ($this->_isDisplayed($header) || $header->getFieldBody() != '') {
333 135
                    $string .= $header->toString();
334
                }
335
            }
336
        }
337
338 135
        return $string;
339
    }
340
341
    /**
342
     * Returns a string representation of this object.
343
     *
344
     * @return string
345
     *
346
     * @see toString()
347
     */
348
    public function __toString()
349
    {
350
        return $this->toString();
351
    }
352
353
    /**
354
     * Save a Header to the internal collection
355
     *
356
     * @param string            $name
357
     * @param Swift_Mime_Header $header
358
     * @param null|int          $offset
359
     */
360 185
    private function _storeHeader($name, Swift_Mime_Header $header, $offset = null)
361
    {
362 185
        if (!isset($this->_headers[strtolower($name)])) {
363 185
            $this->_headers[strtolower($name)] = array();
364
        }
365
366 185
        if (!isset($offset)) {
367 184
            $this->_headers[strtolower($name)][] = $header;
368
        } else {
369 5
            $this->_headers[strtolower($name)][$offset] = $header;
370
        }
371 185
    }
372
373
    /** Test if the headers can be sorted */
374 140
    private function _canSort()
375
    {
376 140
        return count($this->_order) > 0;
377
    }
378
379
    /**
380
     * uksort() algorithm for Header ordering
381
     *
382
     * @param string $a
383
     * @param string $b
384
     *
385
     * @return int
386
     */
387 132
    private function _sortHeaders($a, $b)
388
    {
389 132
        $lowerA = strtolower($a);
390 132
        $lowerB = strtolower($b);
391 132
        $aPos = array_key_exists($lowerA, $this->_order) ? $this->_order[$lowerA] : -1;
392 132
        $bPos = array_key_exists($lowerB, $this->_order) ? $this->_order[$lowerB] : -1;
393
394 132
        if (-1 === $aPos && -1 === $bPos) {
395
            // just be sure to be determinist here
396 24
            return $a > $b ? -1 : 1;
397
        }
398
399 132
        if ($aPos == -1) {
400 49
            return 1;
401 132
        } elseif ($bPos == -1) {
402 52
            return -1;
403
        }
404
405 132
        return $aPos < $bPos ? -1 : 1;
406
    }
407
408
    /**
409
     * Test if the given Header is always displayed
410
     *
411
     * @param Swift_Mime_Header $header
412
     *
413
     * @return bool
414
     */
415 135
    private function _isDisplayed(Swift_Mime_Header $header)
416
    {
417 135
        return array_key_exists(strtolower($header->getFieldName()), $this->_required);
418
    }
419
420
    /**
421
     * Notify all Headers of the new charset
422
     *
423
     * @param $charset
424
     */
425 132
    private function _notifyHeadersOfCharset($charset)
426
    {
427 132
        foreach ($this->_headers as $headerGroup) {
428 119
            foreach ($headerGroup as $header) {
429
                /* @var $header Swift_Mime_Header */
430 119
                $header->setCharset($charset);
431
            }
432
        }
433 132
    }
434
435
    /**
436
     * Make a deep copy of object.
437
     */
438 5
    public function __clone()
439
    {
440 5
        $this->_factory = clone $this->_factory;
441
442 5
        foreach ($this->_headers as $groupKey => $headerGroup) {
443 5
            foreach ($headerGroup as $key => $header) {
444 5
                $this->_headers[$groupKey][$key] = clone $header;
445
            }
446
        }
447 5
    }
448
}
449