Passed
Push — master ( 46ed75...ca2387 )
by Zaahid
03:33
created

HeaderPart   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 10
eloc 14
c 0
b 0
f 0
dl 0
loc 92
ccs 16
cts 18
cp 0.8889
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A ignoreSpacesAfter() 0 3 1
A ignoreSpacesBefore() 0 3 1
A getErrorBagChildren() 0 3 1
A getValue() 0 3 1
A __construct() 0 4 1
A convertEncoding() 0 10 4
A __toString() 0 3 1
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
8
namespace ZBateson\MailMimeParser\Header\Part;
9
10
use ZBateson\MbWrapper\MbWrapper;
11
use ZBateson\MailMimeParser\Header\IHeaderPart;
12
use ZBateson\MailMimeParser\ErrorBag;
13
14
/**
15
 * Abstract base class representing a single part of a parsed header.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
abstract class HeaderPart extends ErrorBag implements IHeaderPart
20
{
21
    /**
22
     * @var ?string the value of the part
23
     */
24
    protected ?string $value;
25
26
    /**
27
     * @var MbWrapper $charsetConverter the charset converter used for
28
     *      converting strings in HeaderPart::convertEncoding
29
     */
30
    protected MbWrapper $charsetConverter;
31
32
    /**
33
     * Sets up dependencies.
34
     *
35
     */
36 151
    public function __construct(MbWrapper $charsetConverter)
37
    {
38 151
        parent::__construct();
39 151
        $this->charsetConverter = $charsetConverter;
40
    }
41
42
    /**
43
     * Returns the part's value.
44
     *
45
     * @return ?string the value of the part
46
     */
47 125
    public function getValue() : ?string
48
    {
49 125
        return $this->value;
50
    }
51
52
    /**
53
     * Returns the value of the part (which is a string).
54
     *
55
     * @return string the value
56
     */
57 1
    public function __toString() : string
58
    {
59 1
        return $this->value;
60
    }
61
62
    /**
63
     * Returns true if spaces before this part should be ignored.  True is only
64
     * returned for MimeLiterals if the part begins with a mime-encoded string,
65
     * Tokens if the Token's value is a single space, and for CommentParts.
66
     *
67
     */
68 1
    public function ignoreSpacesBefore() : bool
69
    {
70 1
        return false;
71
    }
72
73
    /**
74
     * Returns true if spaces after this part should be ignored.  True is only
75
     * returned for MimeLiterals if the part ends with a mime-encoded string
76
     * Tokens if the Token's value is a single space, and for CommentParts.
77
     *
78
     */
79 1
    public function ignoreSpacesAfter() : bool
80
    {
81 1
        return false;
82
    }
83
84
    /**
85
     * Ensures the encoding of the passed string is set to UTF-8.
86
     *
87
     * The method does nothing if the passed $from charset is UTF-8 already, or
88
     * if $force is set to false and mb_check_encoding for $str returns true
89
     * for 'UTF-8'.
90
     *
91
     * @return string utf-8 string
92
     */
93 138
    protected function convertEncoding(string $str, string $from = 'ISO-8859-1', bool $force = false) : string
94
    {
95 138
        if ($from !== 'UTF-8') {
96
            // mime header part decoding will force it.  This is necessary for
97
            // UTF-7 because mb_check_encoding will return true
98 138
            if ($force || !($this->charsetConverter->checkEncoding($str, 'UTF-8'))) {
99 72
                return $this->charsetConverter->convert($str, $from, 'UTF-8');
100
            }
101
        }
102 128
        return $str;
103
    }
104
105
    /**
106
     * Default implementation returns an empty array.
107
     */
108
    protected function getErrorBagChildren() : array
109
    {
110
        return [];
111
    }
112
}
113