Passed
Push — 2.0 ( df1b1e...20b3e4 )
by Zaahid
08:44 queued 05:27
created

AddressEmailConsumer::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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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\Part\CommentPart;
11
use ZBateson\MailMimeParser\Header\Part\LiteralPart;
12
13
/**
14
 * Parses the Address portion of an email address header, for an address part
15
 * that contains both a name and an email address, e.g. "name" <[email protected]>.
16
 *
17
 * The address portion found within the '<' and '>' chars may contain comments
18
 * and quoted portions.
19
 *
20
 * @author Zaahid Bateson
21
 */
22
class AddressEmailConsumer extends AbstractConsumer
23
{
24
    /**
25
     * Returns the following as sub-consumers:
26
     *  - {@see AddressGroupConsumer}
27
     *  - {@see CommentConsumer}
28
     *  - {@see QuotedStringConsumer}
29
     *
30
     * @return AbstractConsumer[] the sub-consumers
31
     */
32 99
    protected function getSubConsumers() : array
33
    {
34 99
        return [
35 99
            $this->consumerService->getCommentConsumer(),
36 99
            $this->consumerService->getQuotedStringConsumer(),
37 99
        ];
38
    }
39
40
    /**
41
     * Overridden to return patterns matching the beginning/end part of an
42
     * address in a name/address part ("<" and ">" chars).
43
     *
44
     * @return string[] the patterns
45
     */
46 99
    public function getTokenSeparators() : array
47
    {
48 99
        return ['<', '>'];
49
    }
50
51
    /**
52
     * Returns true for the '>' char.
53
     */
54 98
    protected function isEndToken(string $token) : bool
55
    {
56 98
        return ($token === '>');
57
    }
58
59
    /**
60
     * Returns true for the '<' char.
61
     */
62 95
    protected function isStartToken(string $token) : bool
63
    {
64 95
        return ($token === '<');
65
    }
66
67
    /**
68
     * Returns a single AddressPart with its 'email' portion set, so an
69
     * AddressConsumer can identify it and create an AddressPart with both a
70
     * name and email set.
71
     *
72
     * @param \ZBateson\MailMimeParser\Header\IHeaderPart[] $parts
73
     * @return \ZBateson\MailMimeParser\Header\IHeaderPart[]|array
74
     */
75 98
    protected function processParts(array $parts) : array
76
    {
77 98
        $strEmail = '';
78 98
        foreach ($parts as $p) {
79 98
            $val = $p->getValue();
80 98
            if ((($p instanceof LiteralPart) && !($p instanceof CommentPart)) && $val !== '') {
81 1
                $val = '"' . \preg_replace('/(["\\\])/', '\\\$1', $val) . '"';
82
            } else {
83 98
                $val = \preg_replace('/\s+/', '', $val);
84
            }
85 98
            $strEmail .= $val;
86
        }
87 98
        return [$this->partFactory->newAddressPart('', $strEmail)];
88
    }
89
}
90