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