kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Koded package. |
||
| 5 | * |
||
| 6 | * (c) Mihail Binev <[email protected]> |
||
| 7 | * |
||
| 8 | * Please view the LICENSE distributed with this source code |
||
| 9 | * for the full copyright and license information. |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Koded\Http; |
||
| 14 | |||
| 15 | /* |
||
| 16 | * |
||
| 17 | * Implementation of PSR-17 (HTTP Message Factories) |
||
| 18 | * @see https://www.php-fig.org/psr/psr-17/ |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | |||
| 22 | use Koded\Http\Interfaces\HttpMethod; |
||
| 23 | use Psr\Http\Message\{RequestFactoryInterface, |
||
| 24 | RequestInterface, |
||
| 25 | ResponseFactoryInterface, |
||
| 26 | ResponseInterface, |
||
| 27 | ServerRequestFactoryInterface, |
||
| 28 | ServerRequestInterface, |
||
| 29 | StreamFactoryInterface, |
||
| 30 | StreamInterface, |
||
| 31 | UploadedFileFactoryInterface, |
||
| 32 | UploadedFileInterface, |
||
| 33 | UriFactoryInterface, |
||
| 34 | UriInterface}; |
||
| 35 | use function array_replace; |
||
| 36 | use function strtoupper; |
||
| 37 | use const UPLOAD_ERR_OK; |
||
| 38 | |||
| 39 | |||
| 40 | class HttpFactory implements RequestFactoryInterface, |
||
| 41 | ResponseFactoryInterface, |
||
| 42 | ServerRequestFactoryInterface, |
||
| 43 | 1 | StreamFactoryInterface, |
|
| 44 | UploadedFileFactoryInterface, |
||
| 45 | 1 | UriFactoryInterface |
|
| 46 | { |
||
| 47 | public function createRequest(string $method, $uri): RequestInterface |
||
| 48 | 1 | { |
|
| 49 | return new ClientRequest(HttpMethod::tryFrom(strtoupper($method)), $uri); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 50 | 1 | } |
|
| 51 | 1 | ||
| 52 | public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
||
| 53 | { |
||
| 54 | 1 | if ($serverParams) { |
|
| 55 | 1 | $_SERVER = array_replace($_SERVER, $serverParams); |
|
| 56 | } |
||
| 57 | 1 | $_SERVER['REQUEST_METHOD'] = strtoupper($method); |
|
| 58 | $_SERVER['REQUEST_URI'] = (string)$uri; |
||
| 59 | return new ServerRequest; |
||
| 60 | 1 | } |
|
| 61 | |||
| 62 | 1 | public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
| 63 | { |
||
| 64 | return (new ServerResponse)->withStatus($code, $reasonPhrase); |
||
| 65 | 3 | } |
|
| 66 | |||
| 67 | 3 | public function createStream(string $content = ''): StreamInterface |
|
| 68 | { |
||
| 69 | return create_stream($content); |
||
| 70 | 1 | } |
|
| 71 | |||
| 72 | 1 | public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
|
| 73 | { |
||
| 74 | return new FileStream($filename, $mode); |
||
| 75 | 1 | } |
|
| 76 | |||
| 77 | 1 | public function createStreamFromResource($resource): StreamInterface |
|
| 78 | { |
||
| 79 | return create_stream($resource); |
||
| 80 | 2 | } |
|
| 81 | |||
| 82 | 2 | public function createUri(string $uri = ''): UriInterface |
|
| 83 | { |
||
| 84 | return new Uri($uri); |
||
| 85 | 1 | } |
|
| 86 | |||
| 87 | public function createUploadedFile( |
||
| 88 | StreamInterface $stream, |
||
| 89 | ?int $size = null, |
||
| 90 | ?int $error = UPLOAD_ERR_OK, |
||
| 91 | ?string $clientFilename = null, |
||
| 92 | 1 | ?string $clientMediaType = null |
|
| 93 | 1 | ): UploadedFileInterface { |
|
| 94 | 1 | return new UploadedFile([ |
|
| 95 | 1 | 'tmp_name' => $stream, |
|
| 96 | 1 | 'name' => $clientFilename, |
|
| 97 | 1 | 'type' => $clientMediaType, |
|
| 98 | 'size' => $size, |
||
| 99 | 'error' => $error, |
||
| 100 | ]); |
||
| 101 | } |
||
| 102 | } |
||
| 103 |