1
|
|
|
<?php |
2
|
|
|
namespace Fyuze\Http\Message; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\RequestInterface; |
5
|
|
|
use Psr\Http\Message\UriInterface; |
6
|
|
|
|
7
|
|
|
class Request extends Message implements RequestInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var |
11
|
|
|
*/ |
12
|
|
|
protected $method; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $requestTarget; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Uri |
21
|
|
|
*/ |
22
|
|
|
protected $uri; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
4 |
|
public function getRequestTarget() |
30
|
|
|
{ |
31
|
4 |
|
if ($this->requestTarget !== null) { |
32
|
1 |
|
return $this->requestTarget; |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
if (!$this->uri) { |
36
|
2 |
|
return '/'; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$requestTarget = $this->getUri()->getPath(); |
40
|
1 |
|
if ($query = $this->getUri()->getQuery()) { |
41
|
1 |
|
$requestTarget .= sprintf('?%s', $query); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
1 |
|
return empty($requestTarget) ? '/' : $requestTarget; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
* |
50
|
|
|
* @link http://tools.ietf.org/html/rfc3986#section-4.3 |
51
|
|
|
* @return UriInterface Returns a UriInterface instance |
52
|
|
|
* representing the URI of the request. |
53
|
|
|
*/ |
54
|
16 |
|
public function getUri() |
55
|
|
|
{ |
56
|
16 |
|
return $this->uri; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @link http://tools.ietf.org/html/rfc7230#section-2.7 (for the various |
63
|
|
|
* request-target forms allowed in request messages) |
64
|
|
|
* @param mixed $requestTarget |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
17 |
|
public function withRequestTarget($requestTarget) |
68
|
|
|
{ |
69
|
17 |
|
return $this->_clone('requestTarget', $requestTarget); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
* |
75
|
|
|
* @return string Returns the request method. |
76
|
|
|
*/ |
77
|
7 |
|
public function getMethod() |
78
|
|
|
{ |
79
|
7 |
|
return $this->method; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
* |
85
|
|
|
* @param string $method Case-sensitive method. |
86
|
|
|
* @return self |
87
|
|
|
* @throws \InvalidArgumentException for invalid HTTP methods. |
88
|
|
|
*/ |
89
|
23 |
|
public function withMethod($method) |
90
|
|
|
{ |
91
|
23 |
|
$method = strtoupper($method); |
92
|
|
|
|
93
|
23 |
|
if (false === in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'OPTIONS', 'DELETE'])) { |
94
|
1 |
|
throw new \InvalidArgumentException( |
95
|
1 |
|
sprintf('Invalid HTTP method: %s', $method) |
96
|
1 |
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
22 |
|
return $this->_clone('method', $method); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
* |
105
|
|
|
* @link http://tools.ietf.org/html/rfc3986#section-4.3 |
106
|
|
|
* @param UriInterface $uri New request URI to use. |
107
|
|
|
* @param bool $preserveHost Preserve the original state of the Host header. |
108
|
|
|
* @return self |
109
|
|
|
*/ |
110
|
4 |
|
public function withUri(UriInterface $uri, $preserveHost = false) |
111
|
|
|
{ |
112
|
4 |
|
$clone = $this->_clone('uri', $uri); |
113
|
|
|
|
114
|
4 |
|
if ($preserveHost === true || $uri->getHost() === null) { |
115
|
|
|
|
116
|
2 |
|
return $clone; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
$clone->headers['host'] = $uri->getHost(); |
120
|
|
|
|
121
|
3 |
|
return $clone; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|