-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
117 lines (93 loc) · 2.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\HttpMessage;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
class Request extends Message implements RequestInterface
{
private ?string $requestTarget = null;
private UriInterface $uri;
public function __construct(
private ?string $method = null,
UriInterface|string $uri = null,
) {
if (null !== $method && !Method::tryFrom(strtoupper($method)) instanceof Method) {
throw new InvalidArgumentException(sprintf('The value of "%s" for $method is invalid', strtoupper($method)));
}
if (is_string($uri)) {
$uri = new Uri($uri);
}
if ($uri instanceof UriInterface) {
$this->uri = $uri;
}
}
/**
* {@inheritdoc}
*/
public function getRequestTarget(): string
{
return $this->requestTarget ?? '';
}
/**
* {@inheritdoc}
*/
public function withRequestTarget(string $requestTarget): RequestInterface
{
if ($requestTarget === $this->requestTarget) {
return $this;
}
$that = clone $this;
$that->requestTarget = $requestTarget;
return $that;
}
/**
* {@inheritdoc}
*/
public function getMethod(): string
{
return $this->method;
}
/**
* {@inheritdoc}
*/
public function withMethod(string $method): RequestInterface
{
if (!Method::tryFrom(strtoupper($method)) instanceof Method) {
throw new InvalidArgumentException(sprintf('The value of "%s" for $method is invalid', strtoupper($method)));
}
if ($method === $this->method) {
return $this;
}
$that = clone $this;
$that->method = $method;
return $that;
}
/**
* {@inheritdoc}
*/
public function getUri(): UriInterface
{
return $this->uri;
}
/**
* {@inheritdoc}
*/
public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface
{
if (isset($this->uri) && (string) $uri === (string) $this->uri) {
return $this;
}
$that = clone $this;
$that->uri = $uri;
if ($preserveHost && $this->hasHeader('host')) {
return $that->withHeader('host', $this->getHeader('host'));
}
return $that;
}
}