1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message\Header; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueException; |
18
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException; |
19
|
|
|
use Sunrise\Http\Message\Header; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Import functions |
23
|
|
|
*/ |
24
|
|
|
use function sprintf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @link https://tools.ietf.org/html/rfc2616#section-19.5.1 |
28
|
|
|
*/ |
29
|
|
|
class ContentDispositionHeader extends Header |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private string $type; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array<string, string> |
39
|
|
|
*/ |
40
|
|
|
private array $parameters; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor of the class |
44
|
|
|
* |
45
|
|
|
* @param string $type |
46
|
|
|
* @param array<array-key, mixed> $parameters |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @throws InvalidHeaderValueException |
49
|
|
|
* If the type isn't valid. |
50
|
|
|
* |
51
|
|
|
* @throws InvalidHeaderValueParameterException |
52
|
|
|
* If the parameters aren't valid. |
53
|
|
|
*/ |
54
|
16 |
|
public function __construct(string $type, array $parameters = []) |
55
|
|
|
{ |
56
|
16 |
|
$this->validateToken($type); |
57
|
|
|
|
58
|
14 |
|
$parameters = $this->validateParameters($parameters); |
59
|
|
|
|
60
|
10 |
|
$this->type = $type; |
61
|
10 |
|
$this->parameters = $parameters; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
9 |
|
public function getFieldName(): string |
68
|
|
|
{ |
69
|
9 |
|
return 'Content-Disposition'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
8 |
|
public function getFieldValue(): string |
76
|
|
|
{ |
77
|
8 |
|
$v = $this->type; |
78
|
8 |
|
foreach ($this->parameters as $name => $value) { |
79
|
7 |
|
$v .= sprintf('; %s="%s"', $name, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
return $v; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|