1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Koded package. |
5
|
|
|
* |
6
|
|
|
* (c) Mihail Binev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Please view the LICENSE distributed with this source code |
9
|
|
|
* for the full copyright and license information. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Koded\Http; |
14
|
|
|
|
15
|
|
|
/* |
16
|
|
|
* |
17
|
|
|
* Implementation of PSR-17 (HTTP Message Factories) |
18
|
|
|
* @see https://www.php-fig.org/psr/psr-17/ |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use Psr\Http\Message\{RequestFactoryInterface, |
23
|
|
|
RequestInterface, |
24
|
|
|
ResponseFactoryInterface, |
25
|
|
|
ResponseInterface, |
26
|
|
|
ServerRequestFactoryInterface, |
27
|
|
|
ServerRequestInterface, |
28
|
|
|
StreamFactoryInterface, |
29
|
|
|
StreamInterface, |
30
|
|
|
UploadedFileFactoryInterface, |
31
|
|
|
UploadedFileInterface, |
32
|
|
|
UriFactoryInterface, |
33
|
|
|
UriInterface}; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class HttpFactory implements RequestFactoryInterface, |
37
|
|
|
ResponseFactoryInterface, |
38
|
|
|
ServerRequestFactoryInterface, |
39
|
|
|
StreamFactoryInterface, |
40
|
|
|
UploadedFileFactoryInterface, |
41
|
|
|
UriFactoryInterface |
42
|
|
|
{ |
43
|
1 |
|
public function createRequest(string $method, $uri): RequestInterface |
44
|
|
|
{ |
45
|
1 |
|
return new ClientRequest($method, $uri); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
49
|
|
|
{ |
50
|
1 |
|
if ($serverParams) { |
|
|
|
|
51
|
1 |
|
$_SERVER = \array_replace($_SERVER, $serverParams); |
52
|
|
|
} |
53
|
|
|
$_SERVER['REQUEST_METHOD'] = $method; |
54
|
1 |
|
$_SERVER['REQUEST_URI'] = (string)$uri; |
55
|
1 |
|
return new ServerRequest; |
56
|
|
|
} |
57
|
1 |
|
|
58
|
|
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
59
|
|
|
{ |
60
|
1 |
|
return (new ServerResponse)->withStatus($code, $reasonPhrase); |
61
|
|
|
} |
62
|
1 |
|
|
63
|
|
|
public function createStream(string $content = ''): StreamInterface |
64
|
|
|
{ |
65
|
3 |
|
return create_stream($content); |
66
|
|
|
} |
67
|
3 |
|
|
68
|
|
|
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
69
|
|
|
{ |
70
|
1 |
|
return new FileStream($filename, $mode); |
71
|
|
|
} |
72
|
1 |
|
|
73
|
|
|
public function createStreamFromResource($resource): StreamInterface |
74
|
|
|
{ |
75
|
1 |
|
return create_stream($resource); |
76
|
|
|
} |
77
|
1 |
|
|
78
|
|
|
public function createUri(string $uri = ''): UriInterface |
79
|
|
|
{ |
80
|
2 |
|
return new Uri($uri); |
81
|
|
|
} |
82
|
2 |
|
|
83
|
|
|
public function createUploadedFile( |
84
|
|
|
StreamInterface $stream, |
85
|
1 |
|
?int $size = null, |
86
|
|
|
?int $error = \UPLOAD_ERR_OK, |
87
|
|
|
?string $clientFilename = null, |
88
|
|
|
?string $clientMediaType = null |
89
|
|
|
): UploadedFileInterface { |
90
|
|
|
return new UploadedFile([ |
91
|
|
|
'tmp_name' => $stream, |
92
|
1 |
|
'name' => $clientFilename, |
93
|
1 |
|
'type' => $clientMediaType, |
94
|
1 |
|
'size' => $size, |
95
|
1 |
|
'error' => $error, |
96
|
1 |
|
]); |
97
|
1 |
|
} |
98
|
|
|
} |
99
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.