Passed
Push — 1.0.0 ( d49389...cbb558 )
by Zaahid
03:21
created

MimePart::isMultiPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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
/**
10
 * Represents a single part of a multi-part mime message.
11
 *
12
 * A MimePart object may have any number of child parts, or may be a child
13
 * itself with its own parent or parents.
14
 *
15
 * The content of the part can be read from its PartStream resource handle,
16
 * accessible via MessagePart::getContentResourceHandle.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class MimePart extends ParentHeaderPart
21
{
22
    /**
23
     * Returns true if this part's mime type is multipart/*
24
     *
25
     * @return bool
26
     */
27 7
    public function isMultiPart()
28
    {
29
        // casting to bool, preg_match returns 1 for true
30 7
        return (bool) (preg_match(
31 7
            '~multipart/\w+~i',
32 7
            $this->getContentType()
33
        ));
34
    }
35
    
36
    /**
37
     * Returns a filename for the part if one is defined, or null otherwise.
38
     * 
39
     * @return string
40
     */
41
    public function getFilename()
42
    {
43
        return $this->getHeaderParameter(
44
            'Content-Disposition',
45
            'filename',
46
            $this->getHeaderParameter(
47
                'Content-Type',
48
                'name'
49
            )
50
        );
51
    }
52
    
53
    /**
54
     * Returns true.
55
     * 
56
     * @return bool
57
     */
58 1
    public function isMime()
59
    {
60 1
        return true;
61
    }
62
    
63
    /**
64
     * Returns true if this part's mime type is text/plain, text/html or if the
65
     * Content-Type header defines a charset.
66
     * 
67
     * @return bool
68
     */
69 7
    public function isTextPart()
70
    {
71 7
        return ($this->getCharset() !== null);
72
    }
73
    
74
    /**
75
     * Returns the lower-cased, trimmed value of the Content-Type header.
76
     * 
77
     * Parses the Content-Type header, defaults to returning text/plain if not
78
     * defined.
79
     * 
80
     * @return string
81
     */
82 10
    public function getContentType($default = 'text/plain')
83
    {
84 10
        return trim(strtolower($this->getHeaderValue('Content-Type', $default)));
85
    }
86
    
87
    /**
88
     * Returns the upper-cased charset of the Content-Type header's charset
89
     * parameter if set, ISO-8859-1 if the Content-Type is text/plain or
90
     * text/html and the charset parameter isn't set, or null otherwise.
91
     * 
92
     * @return string
93
     */
94 12
    public function getCharset()
95
    {
96 12
        $charset = $this->getHeaderParameter('Content-Type', 'charset');
97 12
        if ($charset === null) {
0 ignored issues
show
introduced by
The condition $charset === null is always false.
Loading history...
98 8
            $contentType = $this->getContentType();
99 8
            if ($contentType === 'text/plain' || $contentType === 'text/html') {
100 3
                return 'ISO-8859-1';
101
            }
102 5
            return null;
103
        }
104 4
        return trim(strtoupper($charset));
105
    }
106
    
107
    /**
108
     * Returns the content's disposition, defaulting to 'inline' if not set.
109
     * 
110
     * @return string
111
     */
112 1
    public function getContentDisposition($default = 'inline')
113
    {
114 1
        return strtolower($this->getHeaderValue('Content-Disposition', $default));
115
    }
116
    
117
    /**
118
     * Returns the content-transfer-encoding used for this part, defaulting to
119
     * '7bit' if not set.
120
     * 
121
     * @return string
122
     */
123 2
    public function getContentTransferEncoding($default = '7bit')
124
    {
125 2
        static $translated = [
126
            'x-uue' => 'x-uuencode',
127
            'uue' => 'x-uuencode',
128
            'uuencode' => 'x-uuencode'
129
        ];
130 2
        $type = strtolower($this->getHeaderValue('Content-Transfer-Encoding', $default));
131 2
        if (isset($translated[$type])) {
132
            return $translated[$type];
133
        }
134 2
        return $type;
135
    }
136
}
137