| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Nyholm\Psr7\Factory; |
| 6 |
|
| 7 |
use Nyholm\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri}; |
| 8 |
use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface, ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface, UriFactoryInterface, UriInterface}; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface |
| 17 |
{ |
| 18 |
public function createRequest(string $method, $uri): RequestInterface |
| 19 |
{ |
| 20 |
return new Request($method, $uri); |
| 21 |
} |
| 22 |
|
| 23 |
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
| 24 |
{ |
| 25 |
if (2 > \func_num_args()) { |
| 26 |
|
| 27 |
$reasonPhrase = null; |
| 28 |
} |
| 29 |
|
| 30 |
return new Response($code, [], null, '1.1', $reasonPhrase); |
| 31 |
} |
| 32 |
|
| 33 |
public function createStream(string $content = ''): StreamInterface |
| 34 |
{ |
| 35 |
return Stream::create($content); |
| 36 |
} |
| 37 |
|
| 38 |
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 39 |
{ |
| 40 |
if ('' === $filename) { |
| 41 |
throw new \RuntimeException('Path cannot be empty'); |
| 42 |
} |
| 43 |
|
| 44 |
if (false === $resource = @\fopen($filename, $mode)) { |
| 45 |
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { |
| 46 |
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode)); |
| 47 |
} |
| 48 |
|
| 49 |
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? '')); |
| 50 |
} |
| 51 |
|
| 52 |
return Stream::create($resource); |
| 53 |
} |
| 54 |
|
| 55 |
public function createStreamFromResource($resource): StreamInterface |
| 56 |
{ |
| 57 |
return Stream::create($resource); |
| 58 |
} |
| 59 |
|
| 60 |
public function createUploadedFile(StreamInterface $stream, ?int $size = null, int $error = \UPLOAD_ERR_OK, ?string $clientFilename = null, ?string $clientMediaType = null): UploadedFileInterface |
| 61 |
{ |
| 62 |
if (null === $size) { |
| 63 |
$size = $stream->getSize(); |
| 64 |
} |
| 65 |
|
| 66 |
return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
| 67 |
} |
| 68 |
|
| 69 |
public function createUri(string $uri = ''): UriInterface |
| 70 |
{ |
| 71 |
return new Uri($uri); |
| 72 |
} |
| 73 |
|
| 74 |
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
| 75 |
{ |
| 76 |
return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
| 77 |
} |
| 78 |
} |