|
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-14.17 |
|
28
|
|
|
*/ |
|
29
|
|
|
class ContentTypeHeader extends Header |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Regular Expression for a content type validation |
|
34
|
|
|
* |
|
35
|
|
|
* @link https://tools.ietf.org/html/rfc6838#section-4.2 |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
public const RFC6838_CONTENT_TYPE = '/^[\dA-Za-z][\d\w\!#\$&\+\-\.\^]*(?:\/[\dA-Za-z][\d\w\!#\$&\+\-\.\^]*)?$/'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private string $type; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array<string, string> |
|
48
|
|
|
*/ |
|
49
|
|
|
private array $parameters; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructor of the class |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $type |
|
55
|
|
|
* @param array<array-key, mixed> $parameters |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @throws InvalidHeaderValueException |
|
58
|
|
|
* If the type isn't valid. |
|
59
|
|
|
* |
|
60
|
|
|
* @throws InvalidHeaderValueParameterException |
|
61
|
|
|
* If the parameters aren't valid. |
|
62
|
|
|
*/ |
|
63
|
16 |
|
public function __construct(string $type, array $parameters = []) |
|
64
|
|
|
{ |
|
65
|
16 |
|
$this->validateValueByRegex(self::RFC6838_CONTENT_TYPE, $type); |
|
66
|
|
|
|
|
67
|
14 |
|
$parameters = $this->validateParameters($parameters); |
|
68
|
|
|
|
|
69
|
10 |
|
$this->type = $type; |
|
70
|
10 |
|
$this->parameters = $parameters; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
9 |
|
public function getFieldName(): string |
|
77
|
|
|
{ |
|
78
|
9 |
|
return 'Content-Type'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
8 |
|
public function getFieldValue(): string |
|
85
|
|
|
{ |
|
86
|
8 |
|
$v = $this->type; |
|
87
|
8 |
|
foreach ($this->parameters as $name => $value) { |
|
88
|
7 |
|
$v .= sprintf('; %s="%s"', $name, $value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
8 |
|
return $v; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|