Repository navigation
Expand file tree
/
Copy pathResponse.php
More file actions
169 lines (149 loc) · 4.22 KB
/
Copy pathResponse.php
File metadata and controls
169 lines (149 loc) · 4.22 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace Arcanum\Hyper;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
class Response implements ResponseInterface, HasCharacterSet
{
/**
* Create a new response.
*/
public function __construct(
protected MessageInterface $message,
protected StatusCode $statusCode = StatusCode::OK,
protected Phrase|null $reasonPhrase = null,
protected string $charset = self::UTF8,
) {
}
/**
* Get the character set.
*/
public function getCharacterSet(): string
{
return $this->charset;
}
/**
* Get the status code.
*/
public function getStatusCode(): int
{
return $this->statusCode->value;
}
/**
* Get the reason phrase.
*/
public function getReasonPhrase(): string
{
return $this->reasonPhrase !== null
? $this->reasonPhrase->value
: $this->statusCode->reason()->value;
}
/**
* Return an instance with the specified status code and, optionally, reason phrase.
*/
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface
{
$response = clone $this;
$response->statusCode = StatusCode::from($code);
$response->reasonPhrase = Phrase::tryFrom($reasonPhrase);
return $response;
}
/**
* Message methods
*/
/**
* Get the protocol version.
*/
public function getProtocolVersion(): string
{
return $this->message->getProtocolVersion();
}
/**
* Return an instance with the specified protocol version.
*/
public function withProtocolVersion(string $version): MessageInterface
{
$response = clone $this;
$response->message = $this->message->withProtocolVersion($version);
return $response;
}
/**
* Retrieve all message header values.
*
* The keys represent the header name as it will be sent over the wire, and
* each value is an array of strings associated with the header.
*/
public function getHeaders(): array
{
return $this->message->getHeaders();
}
/**
* Get a header by the given case-insensitive name.
*/
public function getHeader(string $name): array
{
return $this->message->getHeader($name);
}
/**
* Get a comma-separated string of the values for a single header.
*/
public function getHeaderLine(string $name): string
{
return $this->message->getHeaderLine($name);
}
/**
* Checks if a header exists by the given case-insensitive name.
*/
public function hasHeader(string $name): bool
{
return $this->message->hasHeader($name);
}
/**
* Return an instance with the provided value replacing the specified header.
*
* @param string|string[] $value header value(s)
*/
public function withHeader(string $name, $value): MessageInterface
{
$response = clone $this;
$response->message = $this->message->withHeader($name, $value);
return $response;
}
/**
* Return an instance with the specified header appended with the given value.
*
* @param string|string[] $value header value(s)
*/
public function withAddedHeader(string $name, $value): MessageInterface
{
$response = clone $this;
$response->message = $this->message->withAddedHeader($name, $value);
return $response;
}
/**
* Return an instance without the specified header.
*/
public function withoutHeader(string $name): MessageInterface
{
$response = clone $this;
$response->message = $this->message->withoutHeader($name);
return $response;
}
/**
* Get the body of the message.
*/
public function getBody(): StreamInterface
{
return $this->message->getBody();
}
/**
* Return an instance with the specified message body.
*/
public function withBody(StreamInterface $body): MessageInterface
{
$response = clone $this;
$response->message = $this->message->withBody($body);
return $response;
}
}