1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lib\Http; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Psr7\Request as BaseRequest; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
|
10
|
|
|
class Request extends BaseRequest |
11
|
|
|
{ |
12
|
|
|
// @codeCoverageIgnoreStart |
13
|
|
|
const METHOD_HEAD = 'HEAD'; |
14
|
|
|
const METHOD_GET = 'GET'; |
15
|
|
|
const METHOD_POST = 'POST'; |
16
|
|
|
|
17
|
|
|
protected $query; |
18
|
|
|
protected $request; |
19
|
|
|
protected $cookie; |
20
|
|
|
protected $server; |
21
|
|
|
protected $files; |
22
|
|
|
|
23
|
|
|
public function __construct(array $query = [], array $request = [], array $cookie = [], array $server = [], array $files = []) |
24
|
|
|
{ |
25
|
|
|
$this->query = new ParamCollection($query); |
26
|
|
|
$this->request = new ParamCollection($request); |
27
|
|
|
$this->cookie = new ParamCollection($cookie); |
28
|
|
|
$this->server = new ServerCollection($server); |
29
|
|
|
$this->files = new ParamCollection($files); |
30
|
|
|
|
31
|
|
|
$method = $this->server->has('REQUEST_METHOD') ? $this->server->get('REQUEST_METHOD') : 'GET'; |
32
|
|
|
|
33
|
|
|
$requestUri = '/'; |
34
|
|
|
if ($this->server->has('REQUEST_URI')) { |
35
|
|
|
$requestUri = $this->server->get('REQUEST_URI'); |
36
|
|
|
} elseif ($this->server->has('ORIG_PATH_INFO')) { |
37
|
|
|
$requestUri = $this->server->get('ORIG_PATH_INFO'); |
38
|
|
|
$this->server->set('REQUEST_URI', $requestUri); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$version = $this->server->has('SERVER_PROTOCOL') ?? mb_substr($this->server->get('SERVER_PROTOCOL'), -3) ?? '1.1'; |
42
|
|
|
|
43
|
|
|
parent::__construct($method, $requestUri, $this->server->getHeaders(), http_build_query($this->request->all()), $version); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function createFromGlobals(): RequestInterface |
47
|
|
|
{ |
48
|
|
|
$datas = [$_GET, $_POST, $_COOKIE]; |
49
|
|
|
foreach ($datas as &$array) { |
50
|
|
|
array_walk($array, function ($value) { |
51
|
|
|
htmlspecialchars($value); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new self( |
56
|
|
|
$_GET, |
57
|
|
|
$_POST, |
58
|
|
|
$_COOKIE, |
59
|
|
|
$_SERVER, |
60
|
|
|
$_FILES |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// @codeCoverageIgnoreEnd |
65
|
|
|
} |
66
|
|
|
|