-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRequest.php
97 lines (81 loc) · 1.94 KB
/
Request.php
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
<?php
namespace Gt\Http;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Gt\Http\Header\RequestHeaders;
class Request implements RequestInterface {
use Message;
protected string $method;
protected UriInterface $uri;
protected string $requestTarget;
/** @SuppressWarnings("StaticAccess") */
public function __construct(
string $method,
UriInterface $uri,
RequestHeaders $headers
) {
$this->method = RequestMethod::filterMethodName($method);
$this->uri = $uri;
$this->headers = $headers;
$firstLine = $this->headers->getFirst();
$this->protocol = substr(
$firstLine,
0,
strpos($firstLine, " ") ?: 0
);
}
/** @inheritDoc */
public function getRequestTarget():string {
if(isset($this->requestTarget)) {
return $this->requestTarget;
}
$uri = $this->getUri();
$requestTarget = $uri->getPath();
if(empty($requestTarget)) {
$requestTarget = "/";
}
$query = $uri->getQuery();
if(!empty($query)) {
$requestTarget .= "?";
}
$requestTarget .= $query;
return $requestTarget;
}
/** @inheritDoc */
public function withRequestTarget($requestTarget):self {
$clone = clone $this;
$clone->requestTarget = $requestTarget;
return $clone;
}
/** @inheritDoc */
public function getMethod():string {
return $this->method;
}
/**
* @inheritDoc
* @SuppressWarnings("StaticAccess")
*/
public function withMethod(string $method):self {
$method = RequestMethod::filterMethodName($method);
$clone = clone $this;
$clone->method = $method;
return $clone;
}
/** @inheritDoc */
public function getUri():UriInterface {
return $this->uri;
}
/** @inheritDoc */
public function withUri(UriInterface $uri, bool$preserveHost = false):self {
$clone = clone $this;
$host = $uri->getHost();
if(!empty($host)) {
if(!$preserveHost
|| !$this->headers->contains("Host")) {
$this->headers->add("Host", $host);
}
}
$clone->uri = $uri;
return $clone;
}
}