1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Http\Responses\Psr; |
15
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Http\Message\StreamInterface; |
18
|
|
|
use Valkyrja\Http\Response as ValkyrjaResponse; |
19
|
|
|
use Valkyrja\Http\Streams\Psr\Stream; |
20
|
|
|
use Valkyrja\Http\Streams\Stream as ValkyrjaStream; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Response. |
24
|
|
|
* |
25
|
|
|
* @author Melech Mizrachi |
26
|
|
|
*/ |
27
|
|
|
class Response implements ResponseInterface |
28
|
|
|
{ |
29
|
|
|
public function __construct( |
30
|
|
|
protected ValkyrjaResponse $response, |
31
|
|
|
) { |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
public function getProtocolVersion(): string |
38
|
|
|
{ |
39
|
|
|
return $this->response->getProtocolVersion(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function withProtocolVersion(string $version): ResponseInterface |
46
|
|
|
{ |
47
|
|
|
$new = clone $this; |
48
|
|
|
|
49
|
|
|
$new->response = $this->response->withProtocolVersion($version); |
50
|
|
|
|
51
|
|
|
return $new; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public function getHeaders(): array |
58
|
|
|
{ |
59
|
|
|
return $this->response->getHeaders(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function hasHeader(string $name): bool |
66
|
|
|
{ |
67
|
|
|
return $this->response->hasHeader($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function getHeader(string $name): array |
74
|
|
|
{ |
75
|
|
|
return $this->response->getHeader($name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function getHeaderLine(string $name): string |
82
|
|
|
{ |
83
|
|
|
return $this->response->getHeaderLine($name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function withHeader(string $name, $value): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
$new = clone $this; |
92
|
|
|
|
93
|
|
|
$new->response = $this->response->withHeader($name, $value); |
94
|
|
|
|
95
|
|
|
return $new; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public function withAddedHeader(string $name, $value): ResponseInterface |
102
|
|
|
{ |
103
|
|
|
$new = clone $this; |
104
|
|
|
|
105
|
|
|
$new->response = $this->response->withAddedHeader($name, $value); |
106
|
|
|
|
107
|
|
|
return $new; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
|
|
public function withoutHeader(string $name): ResponseInterface |
114
|
|
|
{ |
115
|
|
|
$new = clone $this; |
116
|
|
|
|
117
|
|
|
$new->response = $this->response->withoutHeader($name); |
118
|
|
|
|
119
|
|
|
return $new; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function getBody(): StreamInterface |
126
|
|
|
{ |
127
|
|
|
$stream = $this->response->getBody(); |
128
|
|
|
|
129
|
|
|
return new Stream($stream); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
|
|
public function withBody(StreamInterface $body): ResponseInterface |
136
|
|
|
{ |
137
|
|
|
$new = clone $this; |
138
|
|
|
|
139
|
|
|
$mode = ''; |
140
|
|
|
if ($body->isReadable()) { |
141
|
|
|
$mode = 'r'; |
142
|
|
|
} |
143
|
|
|
if ($body->isWritable()) { |
144
|
|
|
$mode .= 'w'; |
145
|
|
|
} |
146
|
|
|
$stream = new ValkyrjaStream($body->getContents(), $mode . 'b'); |
147
|
|
|
$new->response = $this->response->withBody($stream); |
148
|
|
|
|
149
|
|
|
return $new; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritDoc |
154
|
|
|
*/ |
155
|
|
|
public function getStatusCode(): int |
156
|
|
|
{ |
157
|
|
|
return $this->response->getStatusCode(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritDoc |
162
|
|
|
*/ |
163
|
|
|
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface |
164
|
|
|
{ |
165
|
|
|
$new = clone $this; |
166
|
|
|
|
167
|
|
|
$new->response = $this->response->withStatus($code, $reasonPhrase); |
168
|
|
|
|
169
|
|
|
return $new; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
|
public function getReasonPhrase(): string |
176
|
|
|
{ |
177
|
|
|
return $this->response->getReasonPhrase(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|