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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Nyholm\Psr7\Factory;
  4. use Nyholm\Psr7\{RequestResponseServerRequestStreamUploadedFileUri};
  5. use Psr\Http\Message\{RequestFactoryInterfaceRequestInterfaceResponseFactoryInterfaceResponseInterfaceServerRequestFactoryInterfaceServerRequestInterfaceStreamFactoryInterfaceStreamInterfaceUploadedFileFactoryInterfaceUploadedFileInterfaceUriFactoryInterfaceUriInterface};
  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 RequestFactoryInterfaceResponseFactoryInterfaceServerRequestFactoryInterfaceStreamFactoryInterfaceUploadedFileFactoryInterfaceUriFactoryInterface
  13. {
  14.     public function createRequest(string $method$uri): RequestInterface
  15.     {
  16.         return new Request($method$uri);
  17.     }
  18.     public function createResponse(int $code 200string $reasonPhrase ''): ResponseInterface
  19.     {
  20.         if (\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 $filenamestring $mode 'r'): StreamInterface
  31.     {
  32.         try {
  33.             $resource = @\fopen($filename$mode);
  34.         } catch (\Throwable $e) {
  35.             throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.'$filename));
  36.         }
  37.         if (false === $resource) {
  38.             if ('' === $mode || false === \in_array($mode[0], ['r''w''a''x''c'], true)) {
  39.                 throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.'$mode));
  40.             }
  41.             throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.'$filename));
  42.         }
  43.         return Stream::create($resource);
  44.     }
  45.     public function createStreamFromResource($resource): StreamInterface
  46.     {
  47.         return Stream::create($resource);
  48.     }
  49.     public function createUploadedFile(StreamInterface $streamint $size nullint $error \UPLOAD_ERR_OKstring $clientFilename nullstring $clientMediaType null): UploadedFileInterface
  50.     {
  51.         if (null === $size) {
  52.             $size $stream->getSize();
  53.         }
  54.         return new UploadedFile($stream$size$error$clientFilename$clientMediaType);
  55.     }
  56.     public function createUri(string $uri ''): UriInterface
  57.     {
  58.         return new Uri($uri);
  59.     }
  60.     public function createServerRequest(string $method$uri, array $serverParams = []): ServerRequestInterface
  61.     {
  62.         return new ServerRequest($method$uri, [], null'1.1'$serverParams);
  63.     }
  64. }