Test Failed
Push — master ( e258e4...a626ba )
by Zaahid
15:25
created

NameValuePart::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
8
namespace ZBateson\MailMimeParser\Header\Part;
9
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\LogLevel;
12
use ZBateson\MailMimeParser\ErrorBag;
13
use ZBateson\MbWrapper\MbWrapper;
14
15
/**
16
 * Represents a name/value pair part of a header.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class NameValuePart extends ContainerPart
21
{
22
    /**
23
     * @var string the name of the part
24
     */
25
    protected string $name;
26
27
    public function __construct(
28
        LoggerInterface $logger,
29
        MbWrapper $charsetConverter,
30
        array $nameParts,
31
        array $valueParts
32
    ) {
33
        ErrorBag::__construct($logger);
34
        $this->charsetConverter = $charsetConverter;
35
        $this->name = (!empty($nameParts)) ? $this->getNameFromParts($nameParts) : '';
36
        parent::__construct($logger, $charsetConverter, $valueParts);
37
        \array_unshift($this->children, ...$nameParts);
38
    }
39
40
    /**
41
     * Creates the string 'name' representation of this part constructed from
42
     * the child name parts passed to it.
43
     *
44
     * @param HeaderParts[] $parts
45
     */
46
    protected function getNameFromParts(array $parts) : string
47
    {
48
        return \array_reduce($this->filterIgnoredSpaces($parts), fn ($c, $p) => $c . $p->getValue(), '');
49
    }
50
51
    /**
52
     * Returns the name of the name/value part.
53
     */
54
    public function getName() : string
55
    {
56
        return $this->name;
57
    }
58
59
    protected function validate() : void
60
    {
61
        if ($this->value === '') {
62
            $this->addError('NameValuePart value is empty', LogLevel::NOTICE);
63
        }
64
    }
65
}
66