1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Header; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Stream\OptimalTransferEncodedPhraseStream; |
7
|
|
|
use Genkgo\Mail\Stream\OptimalTransferEncodedTextStream; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class OptimalEncodedHeaderValue |
11
|
|
|
* @package Genkgo\Mail\Header |
12
|
|
|
*/ |
13
|
|
|
final class OptimalEncodedHeaderValue |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
private const FOLDING = "\r\n "; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $value; |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $phrase = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* OptimalEncodedHeaderValue constructor. |
30
|
|
|
* @param string $value |
31
|
|
|
*/ |
32
|
95 |
|
public function __construct(string $value) |
33
|
|
|
{ |
34
|
95 |
|
$this->value = $value; |
35
|
95 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
95 |
|
public function __toString(): string |
41
|
|
|
{ |
42
|
95 |
|
if ($this->phrase === true) { |
43
|
28 |
|
$encoded = new OptimalTransferEncodedPhraseStream($this->value, 68, self::FOLDING); |
44
|
|
|
|
45
|
28 |
|
$encoding = $encoded->getMetadata(['transfer-encoding'])['transfer-encoding']; |
46
|
|
|
} else { |
47
|
78 |
|
$encoded = new OptimalTransferEncodedTextStream($this->value, 68, self::FOLDING); |
48
|
|
|
|
49
|
78 |
|
$encoding = $encoded->getMetadata(['transfer-encoding'])['transfer-encoding']; |
50
|
|
|
} |
51
|
|
|
|
52
|
95 |
|
if ($encoding === '7bit' || $encoding === '8bit') { |
53
|
91 |
|
return (string) $encoded; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
if ($encoding === 'base64') { |
57
|
5 |
|
return sprintf('=?%s?B?%s?=', 'UTF-8', (string) $encoded); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return sprintf('=?%s?Q?%s?=', 'UTF-8', (string) $encoded); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $value |
65
|
|
|
* @return OptimalEncodedHeaderValue |
66
|
|
|
*/ |
67
|
28 |
|
public static function forPhrase(string $value): self |
68
|
|
|
{ |
69
|
28 |
|
$encoded = new self($value); |
70
|
28 |
|
$encoded->value = $value; |
71
|
28 |
|
$encoded->phrase = true; |
|
|
|
|
72
|
28 |
|
return $encoded; |
73
|
|
|
} |
74
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..