vendor/nyholm/psr7/src/Factory/Psr17Factory.php line 71

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Nyholm\Psr7\Factory;
  4. use Nyholm\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri};
  5. use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface, ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface, UriFactoryInterface, UriInterface};
  6. /**
  7.  * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  8.  * @author Martijn van der Ven <martijn@vanderven.se>
  9.  *
  10.  * @final This class should never be extended. See https://github.com/Nyholm/psr7/blob/master/doc/final.md
  11.  */
  12. class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
  13. {
  14.     public function createRequest(string $method, $uri): RequestInterface
  15.     {
  16.         return new Request($method, $uri);
  17.     }
  18.     public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
  19.     {
  20.         if (2 > \func_num_args()) {
  21.             // This will make the Response class to use a custom reasonPhrase
  22.             $reasonPhrase = null;
  23.         }
  24.         return new Response($code, [], null, '1.1', $reasonPhrase);
  25.     }
  26.     public function createStream(string $content = ''): StreamInterface
  27.     {
  28.         return Stream::create($content);
  29.     }
  30.     public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
  31.     {
  32.         $resource = @\fopen($filename, $mode);
  33.         if (false === $resource) {
  34.             if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
  35.                 throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
  36.             }
  37.             throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
  38.         }
  39.         return Stream::create($resource);
  40.     }
  41.     public function createStreamFromResource($resource): StreamInterface
  42.     {
  43.         return Stream::create($resource);
  44.     }
  45.     public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
  46.     {
  47.         if (null === $size) {
  48.             $size = $stream->getSize();
  49.         }
  50.         return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
  51.     }
  52.     public function createUri(string $uri = ''): UriInterface
  53.     {
  54.         return new Uri($uri);
  55.     }
  56.     public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
  57.     {
  58.         return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
  59.     }
  60. }