|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Lkrms\Http; |
| 4 | + |
| 5 | +use Lkrms\Exception\MethodNotImplementedException; |
| 6 | +use Lkrms\Utility\File; |
| 7 | +use Psr\Http\Message\RequestFactoryInterface; |
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | +use Psr\Http\Message\ResponseFactoryInterface; |
| 10 | +use Psr\Http\Message\ResponseInterface; |
| 11 | +use Psr\Http\Message\ServerRequestFactoryInterface; |
| 12 | +use Psr\Http\Message\ServerRequestInterface; |
| 13 | +use Psr\Http\Message\StreamFactoryInterface; |
| 14 | +use Psr\Http\Message\StreamInterface; |
| 15 | +use Psr\Http\Message\UploadedFileFactoryInterface; |
| 16 | +use Psr\Http\Message\UploadedFileInterface; |
| 17 | +use Psr\Http\Message\UriFactoryInterface; |
| 18 | +use Psr\Http\Message\UriInterface; |
| 19 | + |
| 20 | +class HttpFactory implements |
| 21 | + RequestFactoryInterface, |
| 22 | + ResponseFactoryInterface, |
| 23 | + ServerRequestFactoryInterface, |
| 24 | + StreamFactoryInterface, |
| 25 | + UploadedFileFactoryInterface, |
| 26 | + UriFactoryInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @inheritDoc |
| 30 | + */ |
| 31 | + public function createRequest(string $method, $uri): RequestInterface |
| 32 | + { |
| 33 | + return new HttpRequest($uri, $method); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @inheritDoc |
| 38 | + */ |
| 39 | + public function createResponse( |
| 40 | + int $code = 200, |
| 41 | + string $reasonPhrase = '' |
| 42 | + ): ResponseInterface { |
| 43 | + throw new MethodNotImplementedException( |
| 44 | + static::class, |
| 45 | + __FUNCTION__, |
| 46 | + ResponseFactoryInterface::class |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @inheritDoc |
| 52 | + * |
| 53 | + * @param array<string,mixed> $serverParams |
| 54 | + */ |
| 55 | + public function createServerRequest( |
| 56 | + string $method, |
| 57 | + $uri, |
| 58 | + array $serverParams = [] |
| 59 | + ): ServerRequestInterface { |
| 60 | + throw new MethodNotImplementedException( |
| 61 | + static::class, |
| 62 | + __FUNCTION__, |
| 63 | + ServerRequestFactoryInterface::class |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritDoc |
| 69 | + */ |
| 70 | + public function createStream(string $content = ''): StreamInterface |
| 71 | + { |
| 72 | + return Stream::fromContents($content); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @inheritDoc |
| 77 | + */ |
| 78 | + public function createStreamFromFile( |
| 79 | + string $filename, |
| 80 | + string $mode = 'r' |
| 81 | + ): StreamInterface { |
| 82 | + return new Stream(File::open($filename, $mode)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @inheritDoc |
| 87 | + */ |
| 88 | + public function createStreamFromResource($resource): StreamInterface |
| 89 | + { |
| 90 | + return new Stream($resource); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @inheritDoc |
| 95 | + */ |
| 96 | + public function createUploadedFile( |
| 97 | + StreamInterface $stream, |
| 98 | + ?int $size = null, |
| 99 | + int $error = \UPLOAD_ERR_OK, |
| 100 | + ?string $clientFilename = null, |
| 101 | + ?string $clientMediaType = null |
| 102 | + ): UploadedFileInterface { |
| 103 | + throw new MethodNotImplementedException( |
| 104 | + static::class, |
| 105 | + __FUNCTION__, |
| 106 | + UploadedFileFactoryInterface::class |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @inheritDoc |
| 112 | + */ |
| 113 | + public function createUri(string $uri = ''): UriInterface |
| 114 | + { |
| 115 | + return (new Uri($uri, false))->normalise(); |
| 116 | + } |
| 117 | +} |
0 commit comments