-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
112 lines (87 loc) · 2.36 KB
/
Request.php
File metadata and controls
112 lines (87 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php declare(strict_types=1);
namespace Parable\Http;
use Parable\Http\Traits\HasHeaders;
class Request
{
use HasHeaders;
protected const INPUT_SOURCE = 'php://input';
protected string $method;
protected Uri $uri;
protected string $protocol;
protected ?string $body = null;
public function __construct(
?string $method = null,
null|string|Uri $uri = null,
array $headers = [],
string $protocol = 'HTTP/1.1'
) {
if ($method === null || $uri === null) {
[$method, $uri, $headers, $protocol] = RequestFactory::getValuesFromServer();
}
$this->method = strtoupper($method);
$this->protocol = $protocol;
$this->addHeaders($headers);
if (!($uri instanceof Uri)) {
$uri = new Uri($uri);
}
$this->uri = $uri;
}
public function getMethod(): string
{
return $this->method;
}
public function getUri(): Uri
{
return $this->uri;
}
public function getRequestUri(): ?string
{
return $this->uri->getPath();
}
public function getProtocol(): string
{
return $this->protocol;
}
public function getProtocolVersion(): string
{
return str_replace('HTTP/', '', $this->getProtocol());
}
public function getBody(): ?string
{
if ($this->body === null) {
$body = file_get_contents(static::INPUT_SOURCE);
if (!empty($body)) {
$this->body = trim($body);
}
}
return $this->body;
}
public function getUser(): ?string
{
return $this->uri->getUser();
}
public function getPass(): ?string
{
return $this->uri->getPass();
}
public function isHttps(): bool
{
return $this->uri->isHttps();
}
public function isMethod(string $method): bool
{
return $this->getMethod() === strtoupper($method);
}
protected function addHeaders(array $headers): void
{
foreach ($headers as $header => $value) {
$this->addHeader($header, $value);
}
}
protected function addHeader(string $header, string $value): void
{
$normalized = $this->normalize($header);
$this->originalHeaders[$normalized] = $header;
$this->headers[$normalized] = $value;
}
}