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

QuotedStringConsumerService::isEndToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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\Consumer;
9
10
use ZBateson\MailMimeParser\Header\IHeaderPart;
11
12
/**
13
 * Represents a quoted part of a header value starting at a double quote, and
14
 * ending at the next double quote.
15
 *
16
 * A quoted-pair part in a header is a literal.  There are no sub-consumers for
17
 * it and a Part\LiteralPart is returned.
18
 *
19
 * Newline characters (CR and LF) are stripped entirely from the quoted part.
20
 * This is based on the example at:
21
 *
22
 * https://tools.ietf.org/html/rfc822#section-3.1.1
23
 *
24
 * And https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html in section 7.2.1
25
 * splitting the boundary.
26
 *
27
 * @author Zaahid Bateson
28
 */
29
class QuotedStringConsumerService extends AbstractGenericConsumerService
30
{
31
    /**
32
     * Returns true if the token is a double quote.
33
     */
34 106
    protected function isStartToken(string $token) : bool
35
    {
36 106
        return ($token === '"');
37
    }
38
39
    /**
40
     * Returns true if the token is a double quote.
41
     */
42 100
    protected function isEndToken(string $token) : bool
43
    {
44 100
        return ($token === '"');
45
    }
46
47
    /**
48
     * Returns a single regex pattern for a double quote.
49
     *
50
     * @return string[]
51
     */
52 3
    protected function getTokenSeparators() : array
53
    {
54 3
        return ['\"'];
55
    }
56
57
    /**
58
     * No ignored spaces in a quoted part.  Returns the passed $parts param
59
     * as-is.
60
     *
61
     */
62 99
    protected function filterIgnoredSpaces(array $parts) : array
63
    {
64 99
        return $parts;
65
    }
66
67
    /**
68
     * Constructs a LiteralPart and returns it.
69
     *
70
     * @param bool $isLiteral not used - everything in a quoted string is a
71
     *        literal
72
     */
73 99
    protected function getPartForToken(string $token, bool $isLiteral) : ?IHeaderPart
74
    {
75 99
        return $this->partFactory->newLiteralPart($token);
76
    }
77
78
    /**
79
     * Overridden to combine all part values into a single string and return it
80
     * as an array with a single element.
81
     *
82
     * The returned IHeaderParts are all LiteralParts.
83
     *
84
     * @param IHeaderPart[] $parts
85
     * @return IHeaderPart[]
86
     */
87 99
    protected function processParts(array $parts) : array
88
    {
89 99
        $strValue = '';
90 99
        $filtered = $this->filterIgnoredSpaces($parts);
91 99
        foreach ($filtered as $part) {
92 99
            $strValue .= $part->getValue();
93
        }
94 99
        return [$this->partFactory->newLiteralPart($strValue)];
95
    }
96
}
97