11
11
use InvalidArgumentException ;
12
12
13
13
/**
14
- * A PSR-7 response
14
+ * @api
15
15
*
16
16
* @extends AbstractMessage<PsrResponseInterface>
17
17
*/
18
18
class Response extends AbstractMessage implements ResponseInterface
19
19
{
20
20
use ImmutableTrait;
21
21
22
+ /**
23
+ * @var array<int,string>
24
+ */
22
25
protected const STATUS_CODE = [
23
26
100 => 'Continue ' ,
24
27
101 => 'Switching Protocols ' ,
@@ -86,6 +89,9 @@ class Response extends AbstractMessage implements ResponseInterface
86
89
protected int $ StatusCode ;
87
90
protected ?string $ ReasonPhrase ;
88
91
92
+ /**
93
+ * @api
94
+ */
89
95
final public function __construct (
90
96
int $ code = 200 ,
91
97
$ body = null ,
@@ -141,6 +147,34 @@ public function withStatus(int $code, string $reasonPhrase = ''): PsrResponseInt
141
147
->with ('ReasonPhrase ' , $ this ->filterReasonPhrase ($ code , $ reasonPhrase ));
142
148
}
143
149
150
+ private function filterStatusCode (int $ code ): int
151
+ {
152
+ if ($ code < 100 || $ code > 599 ) {
153
+ throw new InvalidArgumentException (
154
+ sprintf ('Invalid HTTP status code: %d ' , $ code ),
155
+ );
156
+ }
157
+ return $ code ;
158
+ }
159
+
160
+ private function filterReasonPhrase (int $ code , ?string $ reasonPhrase ): ?string
161
+ {
162
+ return Str::coalesce ($ reasonPhrase , null )
163
+ ?? static ::STATUS_CODE [$ code ]
164
+ ?? null ;
165
+ }
166
+
167
+ /**
168
+ * @inheritDoc
169
+ */
170
+ protected function getStartLine (): string
171
+ {
172
+ return Arr::implode (' ' , [
173
+ sprintf ('HTTP/%s %d ' , $ this ->ProtocolVersion , $ this ->StatusCode ),
174
+ $ this ->ReasonPhrase ,
175
+ ]);
176
+ }
177
+
144
178
/**
145
179
* @return array{status:int,statusText:string,httpVersion:string,cookies:array<array{name:string,value:string,path?:string,domain?:string,expires?:string,httpOnly?:bool,secure?:bool}>,headers:array<array{name:string,value:string}>,content:array{size:int,mimeType:string,text:string},redirectURL:string,headersSize:int,bodySize:int}
146
180
*/
@@ -165,32 +199,4 @@ public function jsonSerialize(): array
165
199
'redirectURL ' => count ($ location ) === 1 ? $ location [0 ] : '' ,
166
200
] + $ response ;
167
201
}
168
-
169
- /**
170
- * @inheritDoc
171
- */
172
- protected function getStartLine (): string
173
- {
174
- return Arr::implode (' ' , [
175
- sprintf ('HTTP/%s %d ' , $ this ->ProtocolVersion , $ this ->StatusCode ),
176
- $ this ->ReasonPhrase ,
177
- ]);
178
- }
179
-
180
- private function filterStatusCode (int $ code ): int
181
- {
182
- if ($ code < 100 || $ code > 599 ) {
183
- throw new InvalidArgumentException (
184
- sprintf ('Invalid HTTP status code: %d ' , $ code )
185
- );
186
- }
187
- return $ code ;
188
- }
189
-
190
- private function filterReasonPhrase (int $ code , ?string $ reasonPhrase ): ?string
191
- {
192
- return Str::coalesce ($ reasonPhrase , null )
193
- ?? static ::STATUS_CODE [$ code ]
194
- ?? null ;
195
- }
196
202
}
0 commit comments