|
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
|
112 |
|
public function __construct( |
|
28
|
|
|
LoggerInterface $logger, |
|
29
|
|
|
MbWrapper $charsetConverter, |
|
30
|
|
|
array $nameParts, |
|
31
|
|
|
array $valueParts |
|
32
|
|
|
) { |
|
33
|
112 |
|
ErrorBag::__construct($logger); |
|
34
|
112 |
|
$this->charsetConverter = $charsetConverter; |
|
35
|
112 |
|
$this->name = (!empty($nameParts)) ? $this->getNameFromParts($nameParts) : ''; |
|
36
|
112 |
|
parent::__construct($logger, $charsetConverter, $valueParts); |
|
37
|
112 |
|
\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
|
112 |
|
protected function getNameFromParts(array $parts) : string |
|
47
|
|
|
{ |
|
48
|
112 |
|
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
|
112 |
|
public function getName() : string |
|
55
|
|
|
{ |
|
56
|
112 |
|
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
|
|
|
|