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 Psr\Http\Message\UriInterface; |
18
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException; |
19
|
|
|
use Sunrise\Http\Message\Exception\InvalidUriException; |
20
|
|
|
use Sunrise\Http\Message\Header; |
21
|
|
|
use Sunrise\Http\Message\Uri; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Import functions |
25
|
|
|
*/ |
26
|
|
|
use function sprintf; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @link https://tools.ietf.org/html/rfc5988 |
30
|
|
|
*/ |
31
|
|
|
class LinkHeader extends Header |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var UriInterface |
36
|
|
|
*/ |
37
|
|
|
private UriInterface $uri; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array<string, string> |
41
|
|
|
*/ |
42
|
|
|
private array $parameters; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor of the class |
46
|
|
|
* |
47
|
|
|
* @param mixed $uri |
48
|
|
|
* @param array<array-key, mixed> $parameters |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @throws InvalidUriException |
51
|
|
|
* If the URI isn't valid. |
52
|
|
|
* |
53
|
|
|
* @throws InvalidHeaderValueParameterException |
54
|
|
|
* If the parameters aren't valid. |
55
|
|
|
*/ |
56
|
14 |
|
public function __construct($uri, array $parameters = []) |
57
|
|
|
{ |
58
|
14 |
|
$uri = Uri::create($uri); |
59
|
|
|
|
60
|
14 |
|
$parameters = $this->validateParameters($parameters); |
61
|
|
|
|
62
|
10 |
|
$this->uri = $uri; |
63
|
10 |
|
$this->parameters = $parameters; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
7 |
|
public function getFieldName(): string |
70
|
|
|
{ |
71
|
7 |
|
return 'Link'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
8 |
|
public function getFieldValue(): string |
78
|
|
|
{ |
79
|
8 |
|
$v = sprintf('<%s>', $this->uri->__toString()); |
80
|
8 |
|
foreach ($this->parameters as $name => $value) { |
81
|
7 |
|
$v .= sprintf('; %s="%s"', $name, $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
return $v; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|