|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Buzz\Message; |
|
6
|
|
|
|
|
7
|
|
|
use Buzz\Exception\ClientException; |
|
8
|
|
|
use Buzz\Exception\InvalidArgumentException; |
|
9
|
|
|
use Http\Message\ResponseFactory as HTTPlugResponseFactory; |
|
10
|
|
|
use Interop\Http\Factory\ResponseFactoryInterface as InteropResponseFactory; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class ResponseBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ResponseInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $response; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var null|resource |
|
25
|
|
|
*/ |
|
26
|
|
|
private $stream = null; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var null|string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $body = null; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
private $protocolVersion; |
|
|
|
|
|
|
34
|
|
|
private $statusCode; |
|
|
|
|
|
|
35
|
|
|
private $reasonPhrase; |
|
|
|
|
|
|
36
|
|
|
private $headers = []; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param HTTPlugResponseFactory|InteropResponseFactory $responseFactory |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($responseFactory) |
|
42
|
|
|
{ |
|
43
|
|
|
if (!$responseFactory instanceof HTTPlugResponseFactory && !$responseFactory instanceof InteropResponseFactory) { |
|
|
|
|
|
|
44
|
|
|
throw new InvalidArgumentException('First parameter to ResponseBuilder must be a response factory'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->response = $responseFactory->createResponse(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function setStatus(string $input): void |
|
51
|
|
|
{ |
|
52
|
|
|
$parts = explode(' ', $input, 3); |
|
53
|
|
|
if (count($parts) < 2 || 0 !== strpos(strtolower($parts[0]), 'http/')) { |
|
54
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not a valid HTTP status line', $input)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->response = $this->response->withStatus((int) $parts[1], isset($parts[2]) ? $parts[2] : ''); |
|
58
|
|
|
$this->response = $this->response->withProtocolVersion((string) substr($parts[0], 5)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Add a single HTTP header line. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $input |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addHeader(string $input): void |
|
67
|
|
|
{ |
|
68
|
|
|
list($key, $value) = explode(':', $input, 2); |
|
69
|
|
|
$this->response = $this->response->withAddedHeader(trim($key), trim($value)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add HTTP headers. The input array is all the header lines from the HTTP message. Optionally including the |
|
74
|
|
|
* status line. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $headers |
|
77
|
|
|
*/ |
|
78
|
|
|
public function parseHttpHeaders(array $headers): void |
|
79
|
|
|
{ |
|
80
|
|
|
$statusLine = array_shift($headers); |
|
81
|
|
|
try { |
|
82
|
|
|
$this->setStatus($statusLine); |
|
83
|
|
|
} catch (InvalidArgumentException $e) { |
|
84
|
|
|
array_unshift($headers, $statusLine); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
foreach ($headers as $header) { |
|
88
|
|
|
$this->addHeader($header); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add some content to the body. This function writes the $input to a stream. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $input |
|
96
|
|
|
* |
|
97
|
|
|
* @return int returns the number of bytes written |
|
98
|
|
|
*/ |
|
99
|
|
|
public function writeBody(string $input): int |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->response->getBody()->write($input); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getResponse(): ResponseInterface |
|
105
|
|
|
{ |
|
106
|
|
|
$this->response->getBody()->rewind(); |
|
107
|
|
|
|
|
108
|
|
|
return $this->response; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|