DiactorosMessageFactory.php
2 years ago
GuzzleMessageFactory.php
6 months ago
SlimMessageFactory.php
2 years ago
SlimMessageFactory.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Message\MessageFactory; |
| 4 | |
| 5 | use AmeliaHttp\Message\StreamFactory\SlimStreamFactory; |
| 6 | use AmeliaHttp\Message\UriFactory\SlimUriFactory; |
| 7 | use AmeliaHttp\Message\MessageFactory; |
| 8 | use Slim\Http\Request; |
| 9 | use Slim\Http\Response; |
| 10 | use Slim\Http\Headers; |
| 11 | |
| 12 | /** |
| 13 | * Creates Slim 3 messages. |
| 14 | * |
| 15 | * @author Mika Tuupola <tuupola@appelsiini.net> |
| 16 | */ |
| 17 | final class SlimMessageFactory implements MessageFactory |
| 18 | { |
| 19 | /** |
| 20 | * @var SlimStreamFactory |
| 21 | */ |
| 22 | private $streamFactory; |
| 23 | |
| 24 | /** |
| 25 | * @var SlimUriFactory |
| 26 | */ |
| 27 | private $uriFactory; |
| 28 | |
| 29 | public function __construct() |
| 30 | { |
| 31 | $this->streamFactory = new SlimStreamFactory(); |
| 32 | $this->uriFactory = new SlimUriFactory(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * {@inheritdoc} |
| 37 | */ |
| 38 | public function createRequest( |
| 39 | $method, |
| 40 | $uri, |
| 41 | array $headers = [], |
| 42 | $body = null, |
| 43 | $protocolVersion = '1.1' |
| 44 | ) { |
| 45 | return (new Request( |
| 46 | $method, |
| 47 | $this->uriFactory->createUri($uri), |
| 48 | new Headers($headers), |
| 49 | [], |
| 50 | [], |
| 51 | $this->streamFactory->createStream($body), |
| 52 | [] |
| 53 | ))->withProtocolVersion($protocolVersion); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * {@inheritdoc} |
| 58 | */ |
| 59 | public function createResponse( |
| 60 | $statusCode = 200, |
| 61 | $reasonPhrase = null, |
| 62 | array $headers = [], |
| 63 | $body = null, |
| 64 | $protocolVersion = '1.1' |
| 65 | ) { |
| 66 | return (new Response( |
| 67 | $statusCode, |
| 68 | new Headers($headers), |
| 69 | $this->streamFactory->createStream($body) |
| 70 | ))->withProtocolVersion($protocolVersion); |
| 71 | } |
| 72 | } |
| 73 |