| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Http\Message; |
| 4 |
|
| 5 |
use React\Http\Io\HttpBodyStream; |
| 6 |
use React\Stream\ReadableStreamInterface; |
| 7 |
use RingCentral\Psr7\Response as Psr7Response; |
| 8 |
use Psr\Http\Message\StreamInterface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Represents an outgoing server response message. |
| 12 |
* |
| 13 |
* ```php |
| 14 |
* $response = new React\Http\Message\Response( |
| 15 |
* 200, |
| 16 |
* array( |
| 17 |
* 'Content-Type' => 'text/html' |
| 18 |
* ), |
| 19 |
* "<html>Hello world!</html>\n" |
| 20 |
* ); |
| 21 |
* ``` |
| 22 |
* |
| 23 |
* This class implements the |
| 24 |
* [PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface) |
| 25 |
* which in turn extends the |
| 26 |
* [PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface). |
| 27 |
* |
| 28 |
* > Internally, this implementation builds on top of an existing incoming |
| 29 |
* response message and only adds required streaming support. This base class is |
| 30 |
* considered an implementation detail that may change in the future. |
| 31 |
* |
| 32 |
* @see \Psr\Http\Message\ResponseInterface |
| 33 |
*/ |
| 34 |
final class Response extends Psr7Response |
| 35 |
{ |
| 36 |
/** |
| 37 |
* @param int $status HTTP status code (e.g. 200/404) |
| 38 |
* @param array<string,string|string[]> $headers additional response headers |
| 39 |
* @param string|ReadableStreamInterface|StreamInterface $body response body |
| 40 |
* @param string $version HTTP protocol version (e.g. 1.1/1.0) |
| 41 |
* @param ?string $reason custom HTTP response phrase |
| 42 |
* @throws \InvalidArgumentException for an invalid body |
| 43 |
*/ |
| 44 |
public function __construct( |
| 45 |
$status = 200, |
| 46 |
array $headers = array(), |
| 47 |
$body = '', |
| 48 |
$version = '1.1', |
| 49 |
$reason = null |
| 50 |
) { |
| 51 |
if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { |
| 52 |
$body = new HttpBodyStream($body, null); |
| 53 |
} elseif (!\is_string($body) && !$body instanceof StreamInterface) { |
| 54 |
throw new \InvalidArgumentException('Invalid response body given'); |
| 55 |
} |
| 56 |
|
| 57 |
parent::__construct( |
| 58 |
$status, |
| 59 |
$headers, |
| 60 |
$body, |
| 61 |
$version, |
| 62 |
$reason |
| 63 |
); |
| 64 |
} |
| 65 |
} |