Passed
Push — 1.0.0 ( 1b586d...185e59 )
by Zaahid
03:51
created

MimePartHeaderTrait::getRawHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message\Part;
8
9
use ZBateson\MailMimeParser\Header\HeaderFactory;
10
use ZBateson\MailMimeParser\Header\ParameterHeader;
11
12
/**
13
 * Header related methods attached to a mime part.
14
 *
15
 * @author Zaahid Bateson
16
 */
17
trait MimePartHeaderTrait
18
{
19
    /**
20
     * @var HeaderFactory the HeaderFactory object used for created headers
21
     */
22
    protected $headerFactory;
23
24
    /**
25
     * @var string[][] array of headers, with keys set to lower-cased,
26
     *      alphanumeric characters of the header's name, and values set to an
27
     *      array of 2 elements, the first being the header's original name with
28
     *      non-alphanumeric characters and original case, and the second set to
29
     *      the header's value.
30
     */
31
    protected $rawHeaders;
32
33
    /**
34
     * @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of parsed
35
     * header objects populated on-demand, the key is set to the header's name
36
     * lower-cased, and with non-alphanumeric characters removed.
37
     */
38
    protected $headers;
39
40
    /**
41
     * @param HeaderFactory $headerFactory
42
     */
43 24
    public function __construct(HeaderFactory $headerFactory, PartBuilder $partBuilder) {
44 24
        $this->headerFactory = $headerFactory;
45 24
        $this->headers['contenttype'] = $partBuilder->getContentType();
46 24
        $this->rawHeaders = $partBuilder->getRawHeaders();
47 24
    }
48
49
    /**
50
     * Returns the string in lower-case, and with non-alphanumeric characters
51
     * stripped out.
52
     *
53
     * @param string $header
54
     * @return string
55
     */
56 16
    private function getNormalizedHeaderName($header)
57
    {
58 16
        return preg_replace('/[^a-z0-9]/', '', strtolower($header));
59
    }
60
61
    /**
62
     * Returns the AbstractHeader object for the header with the given $name
63
     *
64
     * Note that mime headers aren't case sensitive.
65
     *
66
     * @param string $name
67
     * @return \ZBateson\MailMimeParser\Header\AbstractHeader
68
     */
69 16
    public function getHeader($name)
70
    {
71 16
        $nameKey = $this->getNormalizedHeaderName($name);
72 16
        if (isset($this->rawHeaders[$nameKey])) {
73 15
            if (!isset($this->headers[$nameKey])) {
74 14
                $this->headers[$nameKey] = $this->headerFactory->newInstance(
75 14
                    $this->rawHeaders[$nameKey][0],
76 14
                    $this->rawHeaders[$nameKey][1]
77
                );
78
            }
79 15
            return $this->headers[$nameKey];
80
        }
81 1
        return null;
82
    }
83
84
    /**
85
     * Returns an array of all headers for the mime part with the first element
86
     * holding the name, and the second its value.
87
     *
88
     * @return string[][]
89
     */
90
    public function getRawHeaders()
91
    {
92
        return array_values($this->rawHeaders);
93
    }
94
95
    /**
96
     * Returns the string value for the header with the given $name.
97
     *
98
     * Note that mime headers aren't case sensitive.
99
     *
100
     * @param string $name
101
     * @param string $defaultValue
102
     * @return string
103
     */
104 15
    public function getHeaderValue($name, $defaultValue = null)
105
    {
106 15
        $header = $this->getHeader($name);
107 15
        if ($header !== null) {
108 14
            return $header->getValue();
109
        }
110 1
        return $defaultValue;
111
    }
112
113
    /**
114
     * Returns a parameter of the header $header, given the parameter named
115
     * $param.
116
     *
117
     * Only headers of type
118
     * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters.
119
     * Content-Type and Content-Disposition are examples of headers with
120
     * parameters. "Charset" is a common parameter of Content-Type.
121
     *
122
     * @param string $header
123
     * @param string $param
124
     * @param string $defaultValue
125
     * @return string
126
     */
127 14
    public function getHeaderParameter($header, $param, $defaultValue = null)
128
    {
129 14
        $obj = $this->getHeader($header);
130 14
        if ($obj && $obj instanceof ParameterHeader) {
131 13
            return $obj->getValueFor($param, $defaultValue);
132
        }
133 1
        return $defaultValue;
134
    }
135
}
136