1 | <?php |
||
30 | |||
31 | class Psr17Factory |
||
32 | 1 | implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, |
|
33 | UploadedFileFactoryInterface, UriFactoryInterface |
||
34 | 1 | { |
|
35 | 1 | ||
36 | /** @var ServerRequestCreator|null */ |
||
37 | protected ?ServerRequestCreator $creatorFromGlobal = null; |
||
|
|||
38 | 1 | ||
39 | public function createRequest(string $method, $uri): RequestInterface |
||
40 | { |
||
41 | 1 | if (is_string($uri)) { |
|
42 | $uri = $this->createUri($uri); |
||
43 | 1 | } |
|
44 | 1 | ||
45 | return new Request($method, $uri); |
||
46 | } |
||
47 | 1 | ||
48 | public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
||
49 | { |
||
50 | 10 | if (2 > func_num_args()) { |
|
51 | $reasonPhrase = null; |
||
52 | 10 | } |
|
53 | |||
54 | return new Response($code, [], null, '1.1', $reasonPhrase); |
||
55 | 3 | } |
|
56 | |||
57 | 3 | public function createStream(string $content = ''): StreamInterface |
|
58 | 3 | { |
|
59 | 2 | return Stream::create($content); |
|
60 | 1 | } |
|
61 | |||
62 | public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
||
63 | 1 | { |
|
64 | $resource = @fopen($filename, $mode); |
||
65 | if (false === $resource) { |
||
66 | 1 | if ('' === $mode || false === in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) { |
|
67 | throw new InvalidArgumentException('The mode ' . $mode . ' is invalid.'); |
||
68 | } |
||
69 | 1 | ||
70 | throw new RuntimeException('The file ' . $filename . ' cannot be opened.'); |
||
71 | 1 | } |
|
72 | |||
73 | return Stream::create($resource); |
||
74 | 1 | } |
|
75 | |||
76 | 1 | public function createStreamFromResource($resource): StreamInterface |
|
77 | 1 | { |
|
78 | return Stream::create($resource); |
||
79 | } |
||
80 | 1 | ||
81 | public function createUploadedFile( |
||
82 | StreamInterface $stream, |
||
83 | 2 | int $size = null, |
|
84 | int $error = \UPLOAD_ERR_OK, |
||
85 | 2 | string $clientFilename = null, |
|
86 | string $clientMediaType = null |
||
87 | ): UploadedFileInterface { |
||
88 | 1 | if (null === $size) { |
|
89 | $size = $stream->getSize(); |
||
90 | 1 | } |
|
91 | 1 | ||
92 | return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); |
||
93 | } |
||
94 | 1 | ||
95 | public function createUri(string $uri = ''): UriInterface |
||
96 | { |
||
97 | return new Uri($uri); |
||
98 | } |
||
99 | |||
100 | public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
||
101 | { |
||
102 | if (is_string($uri)) { |
||
103 | $uri = $this->createUri($uri); |
||
104 | } |
||
118 | } |