Passed
Push — master ( 7ee1dd...fe1f98 )
by Zaahid
03:17
created

IdConsumer::getSubConsumers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
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\Header\Consumer;
8
9
/**
10
 * Parses a single ID from an ID header.
11
 *
12
 * Begins consuming greedily on anything except a space, to allow for
13
 * incorrectly-formatted ID headers.  Ends consuming only when a '>' character
14
 * is found.  This means an incorrectly-formatted header can't have multiple
15
 * IDs, but one surrounded by '<>' chars may contain spaces.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
class IdConsumer extends AbstractConsumer
20
{
21
    /**
22
     * Returns the following as sub-consumers:
23
     *  - \ZBateson\MailMimeParser\Header\Consumer\CommentConsumer
24
     *  - \ZBateson\MailMimeParser\Header\Consumer\QuotedStringConsumer
25
     * 
26
     * @return AbstractConsumer[] the sub-consumers
27
     */
28 3
    protected function getSubConsumers()
29
    {
30
        return [
31 3
            $this->consumerService->getCommentConsumer(),
32 3
            $this->consumerService->getQuotedStringConsumer(),
33
        ];
34
    }
35
    
36
    /**
37
     * Overridden to return patterns matching the beginning part of an ID ("<"
38
     * and ">\s*" chars).
39
     * 
40
     * @return string[] the patterns
41
     */
42 3
    public function getTokenSeparators()
43
    {
44 3
        return ['<', '>'];
45
    }
46
    
47
    /**
48
     * Returns true for '>'.
49
     */
50 3
    protected function isEndToken($token)
51
    {
52 3
        return ($token === '>');
53
    }
54
    
55
    /**
56
     * AddressConsumer is "greedy", so this always returns true unless the token
57
     * consists only of whitespace (as it would between '>' and '<' chars).
58
     * 
59
     * @param string $token
60
     * @return boolean false
61
     */
62
    protected function isStartToken($token)
63
    {
64
        return (preg_match('/^\s+$/', $token) !== 1);
65
    }
66
    
67
    /**
68
     * Concatenates the passed parts into a single part and returns it.
69
     *
70
     * @param \ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts
71
     * @return \ZBateson\MailMimeParser\Header\Part\HeaderPart[]
72
     */
73 3
    protected function processParts(array $parts)
74
    {
75 3
        $strValue = '';
76 3
        foreach ($parts as $part) {
77 3
            $strValue .= $part->getValue();
78
        }
79 3
        return [$this->partFactory->newLiteralPart($strValue)];
80
    }
81
}
82