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\InvalidHeaderValueParameterException; |
18
|
|
|
use Sunrise\Http\Message\Header; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Import functions |
22
|
|
|
*/ |
23
|
|
|
use function implode; |
24
|
|
|
use function sprintf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @link https://tools.ietf.org/html/rfc2068#section-19.7.1.1 |
28
|
|
|
*/ |
29
|
|
|
class KeepAliveHeader extends Header |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array<string, string> |
34
|
|
|
*/ |
35
|
|
|
private array $parameters; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor of the class |
39
|
|
|
* |
40
|
|
|
* @param array<array-key, mixed> $parameters |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @throws InvalidHeaderValueParameterException |
43
|
|
|
* If the parameters aren't valid. |
44
|
|
|
*/ |
45
|
14 |
|
public function __construct(array $parameters = []) |
46
|
|
|
{ |
47
|
14 |
|
$parameters = $this->validateParameters($parameters); |
48
|
|
|
|
49
|
10 |
|
$this->parameters = $parameters; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
7 |
|
public function getFieldName(): string |
56
|
|
|
{ |
57
|
7 |
|
return 'Keep-Alive'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
8 |
|
public function getFieldValue(): string |
64
|
|
|
{ |
65
|
8 |
|
$segments = []; |
66
|
8 |
|
foreach ($this->parameters as $name => $value) { |
67
|
|
|
// the construction <foo=> isn't valid... |
68
|
7 |
|
if ($value === '') { |
69
|
2 |
|
$segments[] = $name; |
70
|
2 |
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
$format = $this->isToken($value) ? '%s=%s' : '%s="%s"'; |
74
|
|
|
|
75
|
6 |
|
$segments[] = sprintf($format, $name, $value); |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
return implode(', ', $segments); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|