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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<?php
declare(strict_types=1);
namespace Nsfisis\TinyPhpHttpd\Http;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
final class Response implements ResponseInterface
{
private int $statusCode;
private string $reasonPhrase;
private array $headers = [];
private StreamInterface $body;
private string $protocolVersion = '1.1';
private static array $phrases = [
200 => 'OK',
201 => 'Created',
204 => 'No Content',
301 => 'Moved Permanently',
302 => 'Found',
304 => 'Not Modified',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
];
public function __construct(int $statusCode = 200, array $headers = [], string $body = '', string $reasonPhrase = '')
{
$this->statusCode = $statusCode;
$this->reasonPhrase = $reasonPhrase !== '' ? $reasonPhrase : (self::$phrases[$statusCode] ?? '');
$this->body = new Stream($body);
foreach ($headers as $name => $value) {
$this->headers[strtolower($name)] = [
'name' => $name,
'values' => is_array($value) ? $value : [$value],
];
}
}
public function getProtocolVersion(): string
{
return $this->protocolVersion;
}
public function withProtocolVersion(string $version): static
{
$clone = clone $this;
$clone->protocolVersion = $version;
return $clone;
}
public function getHeaders(): array
{
$result = [];
foreach ($this->headers as $header) {
$result[$header['name']] = $header['values'];
}
return $result;
}
public function hasHeader(string $name): bool
{
return isset($this->headers[strtolower($name)]);
}
public function getHeader(string $name): array
{
$lower = strtolower($name);
return $this->headers[$lower]['values'] ?? [];
}
public function getHeaderLine(string $name): string
{
return implode(', ', $this->getHeader($name));
}
public function withHeader(string $name, $value): static
{
$clone = clone $this;
$clone->headers[strtolower($name)] = [
'name' => $name,
'values' => is_array($value) ? $value : [$value],
];
return $clone;
}
public function withAddedHeader(string $name, $value): static
{
$clone = clone $this;
$lower = strtolower($name);
$values = is_array($value) ? $value : [$value];
if (isset($clone->headers[$lower])) {
$clone->headers[$lower]['values'] = array_merge($clone->headers[$lower]['values'], $values);
} else {
$clone->headers[$lower] = [
'name' => $name,
'values' => $values,
];
}
return $clone;
}
public function withoutHeader(string $name): static
{
$clone = clone $this;
unset($clone->headers[strtolower($name)]);
return $clone;
}
public function getBody(): StreamInterface
{
return $this->body;
}
public function withBody(StreamInterface $body): static
{
$clone = clone $this;
$clone->body = $body;
return $clone;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
public function withStatus(int $code, string $reasonPhrase = ''): static
{
$clone = clone $this;
$clone->statusCode = $code;
$clone->reasonPhrase = $reasonPhrase !== '' ? $reasonPhrase : (self::$phrases[$code] ?? '');
return $clone;
}
public function getReasonPhrase(): string
{
return $this->reasonPhrase;
}
}
|