Passed
Push — master ( a626ba...b864c0 )
by Zaahid
15:44 queued 12:03
created

AddressPart::getValueFromParts()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.9666
cc 4
nc 1
nop 1
crap 4.016
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 Psr\Log\LogLevel;
11
12
/**
13
 * Holds a single address or name/address pair.
14
 *
15
 * The name part of the address may be mime-encoded, but the email address part
16
 * can't be mime-encoded.  Any whitespace in the email address part is stripped
17
 * out.
18
 *
19
 * A convenience method, getEmail, is provided for clarity -- but getValue
20
 * returns the email address as well.
21
 *
22
 * @author Zaahid Bateson
23
 */
24
class AddressPart extends NameValuePart
25
{
26 97
    protected function getValueFromParts(array $parts) : string
27
    {
28 97
        return \implode(\array_map(
29 97
            function ($p) {
30 97
                if ($p instanceof AddressPart) {
31 95
                    return $p->getValue();
32 97
                } elseif ($p instanceof QuotedLiteralPart && $p->getValue() !== '') {
33
                    return '"' . \preg_replace('/(["\\\])/', '\\\$1', $p->getValue()) . '"';
34
                } else {
35 97
                    return \preg_replace('/\s+/', '', $p->getValue());
36
                }
37 97
            },
38 97
            $parts
39 97
        ));
40
    }
41
42
    /**
43
     * Returns the email address.
44
     *
45
     * @return string The email address.
46
     */
47 1
    public function getEmail() : string
48
    {
49 1
        return $this->value;
50
    }
51
52 1
    protected function validate() : void
53
    {
54 1
        if (empty($this->value)) {
55 1
            $this->addError('Address doesn\'t contain an email address', LogLevel::ERROR);
56
        }
57
    }
58
}
59