1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\Http\Header\Value\MediaType; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stadly\Http\Utilities\Rfc7230; |
9
|
|
|
use Stadly\Http\Utilities\Rfc7231; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class for handling media type parameters. |
13
|
|
|
* |
14
|
|
|
* Specification: https://tools.ietf.org/html/rfc7231#section-3.1.1.1 |
15
|
|
|
*/ |
16
|
|
|
final class Parameter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string Name. |
20
|
|
|
*/ |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string Value. |
25
|
|
|
*/ |
26
|
|
|
private $value; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $name Name. |
32
|
|
|
* @param string $value Value. |
33
|
|
|
*/ |
34
|
6 |
|
public function __construct(string $name, string $value) |
35
|
|
|
{ |
36
|
|
|
// Not possible to change name, since it may be used as key in arrays. |
37
|
6 |
|
$plainName = mb_convert_encoding($name, 'ISO-8859-1', 'UTF-8'); |
38
|
6 |
|
if ($plainName !== $name || preg_match('{^' . Rfc7230::TOKEN . '$}', $name) !== 1) { |
39
|
2 |
|
throw new InvalidArgumentException('Invalid name: ' . $name); |
40
|
|
|
} |
41
|
4 |
|
$this->name = $name; |
42
|
|
|
|
43
|
4 |
|
$this->setValue($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Construct parameter from string. |
48
|
|
|
* |
49
|
|
|
* @param string $parameter Parameter string. |
50
|
|
|
* @return self Parameter generated based on the string. |
51
|
|
|
*/ |
52
|
10 |
|
public static function fromString(string $parameter): self |
53
|
|
|
{ |
54
|
10 |
|
$regEx = '{^' . Rfc7231::PARAMETER_CAPTURE . '$}'; |
55
|
10 |
|
$plainParameter = mb_convert_encoding($parameter, 'ISO-8859-1', 'UTF-8'); |
56
|
10 |
|
if ($plainParameter !== $parameter || preg_match($regEx, $parameter, $matches) !== 1) { |
57
|
6 |
|
throw new InvalidArgumentException('Invalid parameter: ' . $parameter); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
$value = $matches['VALUE']; |
61
|
|
|
// Strip slashes if value is quoted. |
62
|
4 |
|
if (mb_strlen($value) >= 2 && mb_substr($value, 0, 1) === '"' && mb_substr($value, -1) === '"') { |
63
|
3 |
|
$value = stripslashes(mb_substr($value, 1, -1)); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return new self($matches['NAME'], $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string String representation of the parameter. |
71
|
|
|
*/ |
72
|
2 |
|
public function __toString(): string |
73
|
|
|
{ |
74
|
2 |
|
return $this->name . '=' . self::encodeValue($this->value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string Name. |
79
|
|
|
*/ |
80
|
1 |
|
public function getName(): string |
81
|
|
|
{ |
82
|
1 |
|
return $this->name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string Value. |
87
|
|
|
*/ |
88
|
1 |
|
public function getValue(): string |
89
|
|
|
{ |
90
|
1 |
|
return $this->value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $value Value. |
95
|
|
|
*/ |
96
|
3 |
|
public function setValue(string $value): void |
97
|
|
|
{ |
98
|
3 |
|
$encodedValue = self::encodeValue($value); |
99
|
|
|
|
100
|
3 |
|
$regEx = '{^(?:' . Rfc7230::TOKEN . '|' . Rfc7230::QUOTED_STRING . ')$}'; |
101
|
3 |
|
$plainValue = mb_convert_encoding($encodedValue, 'ISO-8859-1', 'UTF-8'); |
102
|
3 |
|
if ($plainValue !== $encodedValue || preg_match($regEx, $encodedValue) !== 1) { |
103
|
1 |
|
throw new InvalidArgumentException('Invalid value: ' . $value); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
$this->value = $value; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $value Value. |
111
|
|
|
* @return string Encoded value. |
112
|
|
|
*/ |
113
|
15 |
|
private static function encodeValue(string $value): string |
114
|
|
|
{ |
115
|
15 |
|
if (preg_match('{^' . Rfc7230::TOKEN . '$}', $value) === 1) { |
116
|
8 |
|
return $value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// If value is not a valid token, turn it into a quoted token. |
120
|
9 |
|
return '"' . addslashes($value) . '"'; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|