-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathResponse.php
More file actions
66 lines (49 loc) · 1.38 KB
/
Response.php
File metadata and controls
66 lines (49 loc) · 1.38 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
<?php
namespace App\HttpClient;
class Response
{
private const STATUS_LINE_PATTERN = '~^HTTP/(?P<protocolVersion>\d+\.\d+) (?P<statusCode>\d{3})(?: (?P<reasonPhrase>.+))?~';
private $protocolVersion;
private $statusCode;
private $reasonPhrase;
private $headers = [];
private $body;
public function __construct(string $body, string ...$headers)
{
$this->body = $body;
$this->parseStatusLine(array_shift($headers));
foreach ($headers as $header) {
$this->headers[] = Header::createFromString($header);
}
}
private function parseStatusLine(string $statusLine):void
{
preg_match(self::STATUS_LINE_PATTERN, $statusLine, $matches);
$this->protocolVersion = $matches['protocolVersion'];
$this->statusCode = $matches['statusCode'];
$this->reasonPhrase = $matches['reasonPhrase'] ?? null;
}
public function getProtocolVersion(): string
{
return $this->protocolVersion;
}
public function getStatusCode(): int
{
return $this->statusCode;
}
public function getReasonPhrase(): ?string
{
return $this->reasonPhrase;
}
/**
* @return Header[]
*/
public function getHeaders(): array
{
return $this->headers;
}
public function getBody(): string
{
return $this->body;
}
}