|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\Http\Header\Common; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Stadly\Http\Exception\InvalidHeader; |
|
9
|
|
|
use Stadly\Http\Utilities\Rfc7230; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class for handling general HTTP header fields. |
|
13
|
|
|
* |
|
14
|
|
|
* Specification: https://tools.ietf.org/html/rfc7230#section-3.2 |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ArbitraryHeader implements Header |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string Header field name. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $name; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string Header field value. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $value; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $name Header field name. |
|
32
|
|
|
* @param string $value Header field value. |
|
33
|
|
|
*/ |
|
34
|
5 |
|
public function __construct(string $name, string $value) |
|
35
|
|
|
{ |
|
36
|
5 |
|
$this->setName($name); |
|
37
|
3 |
|
$this->setValue($value); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Construct header from string. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $header Header field string. |
|
44
|
|
|
* @return self Header generated based on the string. |
|
45
|
|
|
* @throws InvalidHeader If the header is invalid. |
|
46
|
|
|
*/ |
|
47
|
7 |
|
public static function fromString(string $header): self |
|
48
|
|
|
{ |
|
49
|
7 |
|
$regEx = '{^' . Rfc7230::HEADER_FIELD_CAPTURE . '$}'; |
|
50
|
7 |
|
$plainHeader = mb_convert_encoding($header, 'ISO-8859-1', 'UTF-8'); |
|
51
|
7 |
|
if ($plainHeader !== $header || preg_match($regEx, $header, $matches) !== 1) { |
|
52
|
4 |
|
throw new InvalidHeader('Invalid header field: ' . $header); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return new self($matches['FIELD_NAME'], $matches['FIELD_VALUE']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
* @throws void The header is always valid. |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function __toString(): string |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->getName() . ': ' . $this->getValue(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return true The header is always valid. |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function isValid(): bool |
|
71
|
|
|
{ |
|
72
|
1 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritDoc |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getName(): string |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->name; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @inheritDoc |
|
85
|
|
|
* @throws void The header is always valid. |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getValue(): string |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set header field name. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $name Header field name. |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function setName(string $name): void |
|
98
|
|
|
{ |
|
99
|
3 |
|
$plainName = mb_convert_encoding($name, 'ISO-8859-1', 'UTF-8'); |
|
100
|
3 |
|
if ($plainName !== $name || preg_match('{^' . Rfc7230::FIELD_NAME . '$}', $name) !== 1) { |
|
101
|
2 |
|
throw new InvalidArgumentException('Invalid header field name: ' . $name); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
$this->name = $name; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Set header field value. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $value Header field value. |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function setValue(string $value): void |
|
113
|
|
|
{ |
|
114
|
3 |
|
$plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8'); |
|
115
|
3 |
|
if ($plainValue !== $value || preg_match('{^' . Rfc7230::FIELD_VALUE . '$}', $value) !== 1) { |
|
116
|
1 |
|
throw new InvalidArgumentException('Invalid header field value: ' . $value); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
3 |
|
$this->value = $value; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|