|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Http\Message; |
| 4 | + |
| 5 | +use Psr\Http\Message\RequestInterface; |
| 6 | +use Psr\Http\Message\StreamInterface; |
| 7 | +use Psr\Http\Message\UriInterface; |
| 8 | +use React\Http\Io\BufferedBody; |
| 9 | +use React\Http\Io\ReadableBodyStream; |
| 10 | +use React\Stream\ReadableStreamInterface; |
| 11 | +use RingCentral\Psr7\Request as BaseRequest; |
| 12 | + |
| 13 | +/** |
| 14 | + * Respresents an outgoing HTTP request message. |
| 15 | + * |
| 16 | + * This class implements the |
| 17 | + * [PSR-7 `RequestInterface`](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface) |
| 18 | + * which extends the |
| 19 | + * [PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface). |
| 20 | + * |
| 21 | + * This is mostly used internally to represent each outgoing HTTP request |
| 22 | + * message for the HTTP client implementation. Likewise, you can also use this |
| 23 | + * class with other HTTP client implementations and for tests. |
| 24 | + * |
| 25 | + * > Internally, this implementation builds on top of an existing outgoing |
| 26 | + * request message and only adds support for streaming. This base class is |
| 27 | + * considered an implementation detail that may change in the future. |
| 28 | + * |
| 29 | + * @see RequestInterface |
| 30 | + */ |
| 31 | +final class Request extends BaseRequest implements RequestInterface |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @param string $method HTTP method for the request. |
| 35 | + * @param string|UriInterface $url URL for the request. |
| 36 | + * @param array<string,string|string[]> $headers Headers for the message. |
| 37 | + * @param string|ReadableStreamInterface|StreamInterface $body Message body. |
| 38 | + * @param string $version HTTP protocol version. |
| 39 | + * @throws \InvalidArgumentException for an invalid URL or body |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + $method, |
| 43 | + $url, |
| 44 | + array $headers = array(), |
| 45 | + $body = '', |
| 46 | + $version = '1.1' |
| 47 | + ) { |
| 48 | + if (\is_string($body)) { |
| 49 | + $body = new BufferedBody($body); |
| 50 | + } elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { |
| 51 | + $body = new ReadableBodyStream($body); |
| 52 | + } elseif (!$body instanceof StreamInterface) { |
| 53 | + throw new \InvalidArgumentException('Invalid request body given'); |
| 54 | + } |
| 55 | + |
| 56 | + parent::__construct($method, $url, $headers, $body, $version); |
| 57 | + } |
| 58 | +} |
0 commit comments