|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * HTTPFactory.php |
| 4 | + * |
| 5 | + * This file is part of HTTPFactory. |
| 6 | + * |
| 7 | + * @author Muhammet ŞAFAK <[email protected]> |
| 8 | + * @copyright Copyright © 2022 Muhammet ŞAFAK |
| 9 | + * @license ./LICENSE MIT |
| 10 | + * @version 1.0 |
| 11 | + * @link https://www.muhammetsafak.com.tr |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace InitPHP\HTTPFactory; |
| 17 | + |
| 18 | +use \InitPHP\HTTP\{Request, |
| 19 | + Response, |
| 20 | + ServerRequest, |
| 21 | + Stream, |
| 22 | + UploadedFile, |
| 23 | + Uri}; |
| 24 | +use Psr\Http\Message\{RequestInterface, |
| 25 | + ResponseInterface, |
| 26 | + ServerRequestInterface, |
| 27 | + StreamInterface, |
| 28 | + UploadedFileInterface, |
| 29 | + UriInterface, |
| 30 | + UriFactoryInterface, |
| 31 | + RequestFactoryInterface, |
| 32 | + ResponseFactoryInterface, |
| 33 | + ServerRequestFactoryInterface, |
| 34 | + StreamFactoryInterface, |
| 35 | + UploadedFileFactoryInterface}; |
| 36 | + |
| 37 | +class HTTPFactory implements UriFactoryInterface, RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface |
| 38 | +{ |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public function createUri(string $uri = ''): UriInterface |
| 44 | + { |
| 45 | + return new Uri($uri); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritDoc |
| 50 | + */ |
| 51 | + public function createRequest(string $method, $uri): RequestInterface |
| 52 | + { |
| 53 | + return new Request($method, $uri, [], new Stream('', null), '1.1'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
| 60 | + { |
| 61 | + return new Response($code, [], new Stream('', null), '1.1', $reasonPhrase); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @inheritDoc |
| 66 | + */ |
| 67 | + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
| 68 | + { |
| 69 | + return new ServerRequest($method, $uri, [], null, '1.1', $serverParams); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritDoc |
| 74 | + */ |
| 75 | + public function createStream(string $content = ''): StreamInterface |
| 76 | + { |
| 77 | + return new Stream($content); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @inheritDoc |
| 82 | + */ |
| 83 | + public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
| 84 | + { |
| 85 | + if(empty($filename) || !is_file($filename)){ |
| 86 | + return new Stream('', null); |
| 87 | + } |
| 88 | + $resource = @fopen($filename, $mode); |
| 89 | + if($resource === FALSE){ |
| 90 | + return new Stream('', null); |
| 91 | + } |
| 92 | + |
| 93 | + return new Stream($resource); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @inheritDoc |
| 98 | + */ |
| 99 | + public function createStreamFromResource($resource): StreamInterface |
| 100 | + { |
| 101 | + return new Stream($resource); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @inheritDoc |
| 106 | + */ |
| 107 | + public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface |
| 108 | + { |
| 109 | + if($size === null){ |
| 110 | + $size = $stream->getSize(); |
| 111 | + } |
| 112 | + return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments