/
Headers.php
89 lines (65 loc) · 1.6 KB
/
Headers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Centum\Http;
use Centum\Interfaces\Http\HeaderInterface;
use Centum\Interfaces\Http\HeadersInterface;
use OutOfRangeException;
class Headers implements HeadersInterface
{
/**
* @var array<non-empty-string, HeaderInterface>
*/
protected array $headers = [];
/**
* @param array<HeaderInterface> $headers
*/
public function __construct(array $headers = [])
{
foreach ($headers as $header) {
$name = $header->getName();
$this->headers[$name] = $header;
}
}
/**
* @throws OutOfRangeException
*/
public function get(string $name): HeaderInterface
{
if (!isset($this->headers[$name])) {
throw new OutOfRangeException();
}
return $this->headers[$name];
}
public function has(string $name): bool
{
return isset($this->headers[$name]);
}
/**
* @throws OutOfRangeException
*/
public function matches(string $name, string $value): bool
{
if (!$this->has($name)) {
return false;
}
$header = $this->get($name);
return $header->getValue() === $value;
}
public function send(): void
{
foreach ($this->headers as $header) {
$header->send();
}
}
public function all(): array
{
return $this->headers;
}
public function toArray(): array
{
$headers = [];
foreach ($this->headers as $name => $header) {
$headers[$name] = $header->getValue();
}
return $headers;
}
}