1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Header; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\HeaderInterface; |
7
|
|
|
|
8
|
|
|
final class HeaderLine |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var HeaderInterface |
12
|
|
|
*/ |
13
|
|
|
private $header; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param HeaderInterface $header |
17
|
|
|
*/ |
18
|
76 |
|
public function __construct(HeaderInterface $header) |
19
|
|
|
{ |
20
|
76 |
|
$this->header = $header; |
21
|
76 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return HeaderInterface |
25
|
|
|
*/ |
26
|
47 |
|
public function getHeader(): HeaderInterface |
27
|
|
|
{ |
28
|
47 |
|
return $this->header; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
62 |
|
public function __toString(): string |
35
|
|
|
{ |
36
|
62 |
|
$headerName = (string)$this->header->getName(); |
37
|
62 |
|
$headerValue = (string)$this->header->getValue(); |
38
|
|
|
|
39
|
62 |
|
$firstFoldingAt = \strpos($headerValue, "\r\n"); |
40
|
62 |
|
if ($firstFoldingAt === false) { |
41
|
62 |
|
$firstFoldingAt = \strlen($headerValue); |
42
|
|
|
} |
43
|
|
|
|
44
|
62 |
|
if (\strlen($headerName) + $firstFoldingAt > 76) { |
45
|
3 |
|
return \sprintf("%s:\r\n %s", $headerName, $headerValue); |
46
|
|
|
} |
47
|
|
|
|
48
|
61 |
|
return \sprintf("%s: %s", $headerName, $headerValue); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $line |
53
|
|
|
* @return HeaderLine |
54
|
|
|
*/ |
55
|
49 |
|
public static function fromString(string $line): HeaderLine |
56
|
|
|
{ |
57
|
49 |
|
$parts = \preg_split('/\s*\:\s*/', $line, 2); |
58
|
|
|
|
59
|
49 |
|
if ($parts === false || \count($parts) !== 2) { |
60
|
1 |
|
throw new \InvalidArgumentException('Invalid header line'); |
61
|
|
|
} |
62
|
|
|
|
63
|
48 |
|
[$name, $value] = $parts; |
|
|
|
|
64
|
|
|
|
65
|
48 |
|
if (\substr($value, 0, 2) === '=?' && \substr($value, -2, 2) === '?=') { |
|
|
|
|
66
|
2 |
|
$value = \iconv_mime_decode($value); |
|
|
|
|
67
|
2 |
|
if ($value === false) { |
68
|
|
|
throw new \UnexpectedValueException('Cannot iconv decode header line'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
48 |
|
return new self( |
73
|
48 |
|
new ParsedHeader( |
74
|
48 |
|
new HeaderName($name), |
75
|
47 |
|
HeaderValue::fromString($value) |
|
|
|
|
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.