-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInternalResponse.php
More file actions
150 lines (126 loc) · 4.06 KB
/
InternalResponse.php
File metadata and controls
150 lines (126 loc) · 4.06 KB
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
<?php
declare(strict_types=1);
namespace TinyBlocks\Http\Internal\Response;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use TinyBlocks\Http\Code;
use TinyBlocks\Http\Headers;
use TinyBlocks\Http\Internal\Exceptions\BadMethodCall;
use TinyBlocks\Http\Internal\Stream\StreamFactory;
final readonly class InternalResponse implements ResponseInterface
{
private function __construct(
private StreamInterface $body,
private Code $code,
private ResponseHeaders $headers,
private ProtocolVersion $protocolVersion
) {
}
public static function createWithBody(mixed $body, Code $code, Headers ...$headers): ResponseInterface
{
return new InternalResponse(
body: StreamFactory::fromBody(body: $body)->write(),
code: $code,
headers: ResponseHeaders::fromOrDefault(...$headers),
protocolVersion: ProtocolVersion::default()
);
}
public static function createWithoutBody(Code $code, Headers ...$headers): ResponseInterface
{
return new InternalResponse(
body: StreamFactory::fromEmptyBody()->write(),
code: $code,
headers: ResponseHeaders::fromOrDefault(...$headers),
protocolVersion: ProtocolVersion::default()
);
}
public function withBody(StreamInterface $body): MessageInterface
{
return new InternalResponse(
body: $body,
code: $this->code,
headers: $this->headers,
protocolVersion: $this->protocolVersion
);
}
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface
{
throw new BadMethodCall(method: __FUNCTION__);
}
public function withHeader(string $name, $value): MessageInterface
{
$headers = ResponseHeaders::fromOrDefault(
$this->headers,
ResponseHeaders::fromNameAndValue(name: $name, value: $value)
);
return new InternalResponse(
body: $this->body,
code: $this->code,
headers: $headers,
protocolVersion: $this->protocolVersion
);
}
public function withoutHeader(string $name): MessageInterface
{
$headers = $this->headers->removeByName(name: $name);
return new InternalResponse(
body: $this->body,
code: $this->code,
headers: $headers,
protocolVersion: $this->protocolVersion
);
}
public function withAddedHeader(string $name, $value): MessageInterface
{
$headers = ResponseHeaders::fromNameAndValue(name: $name, value: $value);
return new InternalResponse(
body: $this->body,
code: $this->code,
headers: $headers,
protocolVersion: $this->protocolVersion
);
}
public function withProtocolVersion(string $version): MessageInterface
{
$protocolVersion = ProtocolVersion::from(version: $version);
return new InternalResponse(
body: $this->body,
code: $this->code,
headers: $this->headers,
protocolVersion: $protocolVersion
);
}
public function hasHeader(string $name): bool
{
return $this->headers->hasHeader(name: $name);
}
public function getBody(): StreamInterface
{
return $this->body;
}
public function getHeader(string $name): array
{
return $this->headers->getByName(name: $name);
}
public function getHeaders(): array
{
return $this->headers->toArray();
}
public function getStatusCode(): int
{
return $this->code->value;
}
public function getHeaderLine(string $name): string
{
return implode(', ', $this->headers->getByName(name: $name));
}
public function getReasonPhrase(): string
{
return $this->code->message();
}
public function getProtocolVersion(): string
{
return $this->protocolVersion->version;
}
}