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