Passed
Push — master ( ed72e0...56792b )
by Zaahid
03:13
created

SubjectConsumer::getPartForToken()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 4.0312
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\Header\Consumer;
8
9
use ZBateson\MailMimeParser\Header\Part\HeaderPart;
10
use ZBateson\MailMimeParser\Header\Part\Token;
11
use Iterator;
12
13
/**
14
 * Extends GenericConsumer to remove its sub consumers.
15
 *
16
 * Prior to this, subject headers were parsed using the GenericConsumer which
17
 * meant if the subject contained text within parentheses, it would not be
18
 * included as part of the returned value in a getHeaderValue.  Mime-encoded
19
 * parts within quotes would be ignored, and backslash characters denoted an
20
 * escaped character.
21
 *
22
 * From testing in ThunderBird and Outlook web mail it seems quoting parts
23
 * doesn't have an effect (e.g. quoting a "mime-literal" encoded part still
24
 * comes out decoded), and parts in parentheses (comments) are displayed
25
 * normally.
26
 * 
27
 * @author Zaahid Bateson
28
 */
29
class SubjectConsumer extends GenericConsumer
30
{
31
    /**
32
     * Returns an empty array
33
     * 
34
     * @return AbstractConsumer[] the sub-consumers
35
     */
36 2
    protected function getSubConsumers()
37
    {
38 2
        return [];
39
    }
40
41
    /**
42
     * Overridden to preserve whitespace.
43
     *
44
     * Whitespace between two words is preserved unless the whitespace begins
45
     * with a newline (\n or \r\n), in which case the entire string of
46
     * whitespace is discarded, and a single space ' ' character is used in its
47
     * place.
48
     *
49
     * @param string $token the token
50
     * @param bool $isLiteral set to true if the token represents a literal -
51
     *        e.g. an escaped token
52
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPart|null the
53
     *         constructed header part or null if the token should be ignored
54
     */
55 2
    protected function getPartForToken($token, $isLiteral)
56
    {
57 2
        if ($isLiteral) {
58
            return $this->partFactory->newLiteralPart($token);
59 2
        } elseif (preg_match('/^\s+$/', $token)) {
60 2
            if (preg_match('/^[\r\n]/', $token)) {
61 2
                return $this->partFactory->newToken(' ');
62
            }
63 2
            return $this->partFactory->newToken($token);
64
        }
65 2
        return $this->partFactory->newInstance($token);
66
    }
67
68
    /**
69
     * Returns an array of \ZBateson\MailMimeParser\Header\Part\HeaderPart for
70
     * the current token on the iterator.
71
     * 
72
     * Overridden from AbstractConsumer to remove special filtering for
73
     * backslash escaping, which also seems to not apply to Subject headers at
74
     * least in ThunderBird's implementation.
75
     * 
76
     * @param Iterator $tokens
77
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPart[]|array
78
     */
79 2
    protected function getTokenParts(Iterator $tokens)
80
    {
81 2
        return $this->getConsumerTokenParts($tokens);
82
    }
83
84
    /**
85
     * Overridden to not split out backslash characters and its next character
86
     * as a special case defined in AbastractConsumer
87
     * 
88
     * @return string the regex pattern
89
     */
90 2
    protected function getTokenSplitPattern()
91
    {
92 2
        $sChars = implode('|', $this->getAllTokenSeparators());
93 2
        return '~(' . $sChars . ')~';
94
    }
95
}
96