1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Header; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class HeaderValueParameter |
8
|
|
|
* @package Genkgo\Mail\Header |
9
|
|
|
*/ |
10
|
|
|
final class HeaderValueParameter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $name; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $value; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
private CONST ANY_ASCII_EXCEPT_SPACE_CTL_T_SPECIAL = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"; |
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $prependNewLine; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* HeaderValue constructor. |
33
|
|
|
* @param string $name |
34
|
|
|
* @param string $value |
35
|
|
|
* @param bool $prependNewLine |
36
|
|
|
*/ |
37
|
28 |
|
public function __construct(string $name, string $value, bool $prependNewLine = false) |
38
|
|
|
{ |
39
|
28 |
|
if (strcspn($name, self::ANY_ASCII_EXCEPT_SPACE_CTL_T_SPECIAL) !== strlen($name)) { |
40
|
|
|
throw new \InvalidArgumentException('Incorrect name'); |
41
|
|
|
} |
42
|
|
|
|
43
|
28 |
|
if (strcspn($value, self::ANY_ASCII_EXCEPT_SPACE_CTL_T_SPECIAL) !== strlen($value)) { |
44
|
|
|
throw new \InvalidArgumentException('Incorrect value'); |
45
|
|
|
} |
46
|
|
|
|
47
|
28 |
|
$this->name = $name; |
48
|
28 |
|
$this->value = $value; |
49
|
28 |
|
$this->prependNewLine = $prependNewLine; |
50
|
28 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
28 |
|
public function getName(): string |
56
|
|
|
{ |
57
|
28 |
|
return $this->name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
7 |
|
public function getValue(): string |
64
|
|
|
{ |
65
|
7 |
|
return $this->value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
28 |
|
public function __toString(): string |
72
|
|
|
{ |
73
|
28 |
|
$prepend = $this->prependNewLine ? "\r\n " : ''; |
74
|
28 |
|
return sprintf('%s%s="%s"', $prepend, $this->name, $this->value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $parameterString |
79
|
|
|
* @return HeaderValueParameter |
80
|
|
|
*/ |
81
|
6 |
|
public static function fromString(string $parameterString): HeaderValueParameter |
82
|
|
|
{ |
83
|
6 |
|
$nameValue = explode('=', $parameterString); |
84
|
6 |
|
if (count($nameValue) !== 2) { |
85
|
1 |
|
throw new \InvalidArgumentException( |
86
|
1 |
|
sprintf('Invalid parameter string value %s', $parameterString) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
5 |
|
[$name, $value] = $nameValue; |
|
|
|
|
91
|
|
|
|
92
|
5 |
|
if ($value[0] === '"' && $value[-1] === '"') { |
|
|
|
|
93
|
3 |
|
$value = substr($value, 1, -1); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
return new self($name, $value); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
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.