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