| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Buzz\Message; |
| 4 |
|
| 5 |
use Psr\Http\Message\StreamInterface; |
| 6 |
use Psr\Http\Message\UriInterface; |
| 7 |
use RingCentral\Psr7\Request; |
| 8 |
use RingCentral\Psr7\Response; |
| 9 |
use RingCentral\Psr7\Uri; |
| 10 |
use React\Stream\ReadableStreamInterface; |
| 11 |
|
| 12 |
/** |
| 13 |
* @internal |
| 14 |
*/ |
| 15 |
class MessageFactory |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Creates a new instance of RequestInterface for the given request parameters |
| 19 |
* |
| 20 |
* @param string $method |
| 21 |
* @param string|UriInterface $uri |
| 22 |
* @param array $headers |
| 23 |
* @param string|ReadableStreamInterface $content |
| 24 |
* @return Request |
| 25 |
*/ |
| 26 |
public function request($method, $uri, $headers = array(), $content = '') |
| 27 |
{ |
| 28 |
return new Request($method, $uri, $headers, $this->body($content), '1.0'); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates a new instance of ResponseInterface for the given response parameters |
| 33 |
* |
| 34 |
* @param string $version |
| 35 |
* @param int $status |
| 36 |
* @param string $reason |
| 37 |
* @param array $headers |
| 38 |
* @param ReadableStreamInterface|string $body |
| 39 |
* @return Response |
| 40 |
* @uses self::body() |
| 41 |
*/ |
| 42 |
public function response($version, $status, $reason, $headers = array(), $body = '') |
| 43 |
{ |
| 44 |
return new Response($status, $headers, $this->body($body), $version, $reason); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Creates a new instance of StreamInterface for the given body contents |
| 49 |
* |
| 50 |
* @param ReadableStreamInterface|string $body |
| 51 |
* @return StreamInterface |
| 52 |
*/ |
| 53 |
public function body($body) |
| 54 |
{ |
| 55 |
if ($body instanceof ReadableStreamInterface) { |
| 56 |
return new ReadableBodyStream($body); |
| 57 |
} |
| 58 |
|
| 59 |
return \RingCentral\Psr7\stream_for($body); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Creates a new instance of UriInterface for the given URI string |
| 64 |
* |
| 65 |
* @param string $uri |
| 66 |
* @return UriInterface |
| 67 |
*/ |
| 68 |
public function uri($uri) |
| 69 |
{ |
| 70 |
return new Uri($uri); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Creates a new instance of UriInterface for the given URI string relative to the given base URI |
| 75 |
* |
| 76 |
* @param UriInterface $base |
| 77 |
* @param string $uri |
| 78 |
* @return UriInterface |
| 79 |
*/ |
| 80 |
public function uriRelative(UriInterface $base, $uri) |
| 81 |
{ |
| 82 |
return Uri::resolve($base, $uri); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Resolves the given relative or absolute $uri by appending it behind $this base URI |
| 87 |
* |
| 88 |
* The given $uri parameter can be either a relative or absolute URI and |
| 89 |
* as such can not contain any URI template placeholders. |
| 90 |
* |
| 91 |
* As such, the outcome of this method represents a valid, absolute URI |
| 92 |
* which will be returned as an instance implementing `UriInterface`. |
| 93 |
* |
| 94 |
* If the given $uri is a relative URI, it will simply be appended behind $base URI. |
| 95 |
* |
| 96 |
* If the given $uri is an absolute URI, it will simply be verified to |
| 97 |
* be *below* the given $base URI. |
| 98 |
* |
| 99 |
* @param UriInterface $uri |
| 100 |
* @param UriInterface $base |
| 101 |
* @return UriInterface |
| 102 |
* @throws \UnexpectedValueException |
| 103 |
*/ |
| 104 |
public function expandBase(UriInterface $uri, UriInterface $base) |
| 105 |
{ |
| 106 |
if ($uri->getScheme() !== '') { |
| 107 |
if (strpos((string)$uri, (string)$base) !== 0) { |
| 108 |
throw new \UnexpectedValueException('Invalid base, "' . $uri . '" does not appear to be below "' . $base . '"'); |
| 109 |
} |
| 110 |
return $uri; |
| 111 |
} |
| 112 |
|
| 113 |
$uri = (string)$uri; |
| 114 |
$base = (string)$base; |
| 115 |
|
| 116 |
if ($uri !== '' && substr($base, -1) !== '/' && substr($uri, 0, 1) !== '?') { |
| 117 |
$base .= '/'; |
| 118 |
} |
| 119 |
|
| 120 |
if (isset($uri[0]) && $uri[0] === '/') { |
| 121 |
$uri = substr($uri, 1); |
| 122 |
} |
| 123 |
|
| 124 |
return $this->uri($base . $uri); |
| 125 |
} |
| 126 |
} |