Completed
Push — 1.0.0 ( 608796...a0ab23 )
by Zaahid
04:48
created

MimePartHeaderTrait::getHeaderParameter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
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 \ZBateson\MailMimeParser\Header\HeaderFactory the HeaderFactory
21
     *      object used for created headers
22
     */
23
    protected $headerFactory;
24
25
    /**
26
     * @var string[][] array of headers, with keys set to lower-cased,
27
     *      alphanumeric characters of the header's name, and values set to an
28
     *      array of 2 elements, the first being the header's original name with
29
     *      non-alphanumeric characters and original case, and the second set to
30
     *      the header's value.
31
     */
32
    protected $rawHeaders;
33
34
    /**
35
     * @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of parsed
36
     * header objects populated on-demand, the key is set to the header's name
37
     * lower-cased, and with non-alphanumeric characters removed.
38
     */
39
    protected $headers;
40
41
    /**
42
     * @param \ZBateson\MailMimeParser\Message\Part\HeaderFactory $headerFactory
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Message\Part\HeaderFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    public function __construct(HeaderFactory $headerFactory, PartBuilder $partBuilder) {
45
        $this->headerFactory = $headerFactory;
46
        $this->headers['contenttype'] = $partBuilder->getContentType();
47
        $this->rawHeaders = $partBuilder->getRawHeaders();
48
    }
49
50
    /**
51
     * Returns the AbstractHeader object for the header with the given $name
52
     *
53
     * Note that mime headers aren't case sensitive.
54
     *
55
     * @param string $name
56
     * @return \ZBateson\MailMimeParser\Header\AbstractHeader
57
     */
58
    public function getHeader($name)
59
    {
60
        $nameKey = preg_replace('/[^a-z0-9]/', '', strtolower($name));
61
        if (isset($this->rawHeaders[$nameKey])) {
62
            if (!isset($this->headers[$nameKey])) {
63
                $this->headers[$nameKey] = $this->headerFactory->newInstance(
64
                    $this->rawHeaders[$nameKey][0],
65
                    $this->rawHeaders[$nameKey][1]
66
                );
67
            }
68
            return $this->headers[$nameKey];
69
        }
70
        return null;
71
    }
72
73
    /**
74
     * Returns the string value for the header with the given $name.
75
     *
76
     * Note that mime headers aren't case sensitive.
77
     *
78
     * @param string $name
79
     * @param string $defaultValue
80
     * @return string
81
     */
82
    public function getHeaderValue($name, $defaultValue = null)
83
    {
84
        $header = $this->getHeader($name);
85
        if ($header !== null) {
86
            return $header->getValue();
87
        }
88
        return $defaultValue;
89
    }
90
91
    /**
92
     * Returns a parameter of the header $header, given the parameter named
93
     * $param.
94
     *
95
     * Only headers of type
96
     * \ZBateson\MailMimeParser\Header\ParameterHeader have parameters.
97
     * Content-Type and Content-Disposition are examples of headers with
98
     * parameters. "Charset" is a common parameter of Content-Type.
99
     *
100
     * @param string $header
101
     * @param string $param
102
     * @param string $defaultValue
103
     * @return string
104
     */
105
    public function getHeaderParameter($header, $param, $defaultValue = null)
106
    {
107
        $obj = $this->getHeader($header);
108
        if ($obj && $obj instanceof ParameterHeader) {
109
            return $obj->getValueFor($param, $defaultValue);
110
        }
111
        return $defaultValue;
112
    }
113
}
114