1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Http Message. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2019, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Http\Message; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
use Psr\Http\Message\StreamInterface; |
17
|
|
|
use Psr\Http\Message\UriInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PSR-7 Request implementation. |
21
|
|
|
*/ |
22
|
|
|
class Request extends Message implements RequestInterface |
23
|
|
|
{ |
24
|
|
|
protected $method = 'GET'; |
25
|
|
|
|
26
|
|
|
protected $target = ''; |
27
|
|
|
|
28
|
|
|
protected $uri = ''; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function __construct(UriInterface $uri, string $method = 'GET', StreamInterface $body, array $headers = []) |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
//force __toString method |
35
|
|
|
$this->uri = (string) $uri; |
36
|
|
|
$this->method = $method; |
37
|
|
|
|
38
|
|
|
parent::$body = $body; |
39
|
|
|
parent::$headers = $headers; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Retrieves the message's request target. |
44
|
|
|
* |
45
|
|
|
* Retrieves the message's request-target either as it will appear (for |
46
|
|
|
* clients), as it appeared at request (for servers), or as it was |
47
|
|
|
* specified for the instance (see withRequestTarget()). |
48
|
|
|
* |
49
|
|
|
* In most cases, this will be the origin-form of the composed URI, |
50
|
|
|
* unless a value was provided to the concrete implementation (see |
51
|
|
|
* withRequestTarget() below). |
52
|
|
|
* |
53
|
|
|
* If no URI is available, and no request-target has been specifically |
54
|
|
|
* provided, this method MUST return the string "/". |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getRequestTarget(): string |
59
|
|
|
{ |
60
|
|
|
} |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return an instance with the specific request-target. |
64
|
|
|
* |
65
|
|
|
* If the request needs a non-origin-form request-target — e.g., for |
66
|
|
|
* specifying an absolute-form, authority-form, or asterisk-form — |
67
|
|
|
* this method may be used to create an instance with the specified |
68
|
|
|
* request-target, verbatim. |
69
|
|
|
* |
70
|
|
|
* This method MUST be implemented in such a way as to retain the |
71
|
|
|
* immutability of the message, and MUST return an instance that has the |
72
|
|
|
* changed request target. |
73
|
|
|
* |
74
|
|
|
* @link http://tools.ietf.org/html/rfc7230#section-5.3 (for the various |
75
|
|
|
* request-target forms allowed in request messages) |
76
|
|
|
* |
77
|
|
|
* @param mixed $requestTarget |
78
|
|
|
* |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
|
|
public function withRequestTarget(string $requestTarget): RequestInterface |
82
|
|
|
{ |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retrieves the HTTP method of the request. |
87
|
|
|
* |
88
|
|
|
* @return string Returns the request method. |
89
|
|
|
*/ |
90
|
|
|
public function getMethod(): string |
91
|
|
|
{ |
92
|
|
|
} |
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return an instance with the provided HTTP method. |
96
|
|
|
* |
97
|
|
|
* While HTTP method names are typically all uppercase characters, HTTP |
98
|
|
|
* method names are case-sensitive and thus implementations SHOULD NOT |
99
|
|
|
* modify the given string. |
100
|
|
|
* |
101
|
|
|
* This method MUST be implemented in such a way as to retain the |
102
|
|
|
* immutability of the message, and MUST return an instance that has the |
103
|
|
|
* changed request method. |
104
|
|
|
* |
105
|
|
|
* @param string $method Case-sensitive method. |
106
|
|
|
* |
107
|
|
|
* @throws InvalidArgumentException for invalid HTTP methods. |
108
|
|
|
* |
109
|
|
|
* @return static |
110
|
|
|
*/ |
111
|
|
|
public function withMethod(string $method): RequestInterface |
112
|
|
|
{ |
113
|
|
|
} |
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieves the URI instance. |
117
|
|
|
* |
118
|
|
|
* This method MUST return a UriInterface instance. |
119
|
|
|
* |
120
|
|
|
* @link http://tools.ietf.org/html/rfc3986#section-4.3 |
121
|
|
|
* |
122
|
|
|
* @return UriInterface Returns a UriInterface instance |
123
|
|
|
* representing the URI of the request. |
124
|
|
|
*/ |
125
|
|
|
public function getUri(): UriInterface |
126
|
|
|
{ |
127
|
|
|
} |
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns an instance with the provided URI. |
131
|
|
|
* |
132
|
|
|
* This method MUST update the Host header of the returned request by |
133
|
|
|
* default if the URI contains a host component. If the URI does not |
134
|
|
|
* contain a host component, any pre-existing Host header MUST be carried |
135
|
|
|
* over to the returned request. |
136
|
|
|
* |
137
|
|
|
* You can opt-in to preserving the original state of the Host header by |
138
|
|
|
* setting `$preserveHost` to `true`. When `$preserveHost` is set to |
139
|
|
|
* `true`, this method interacts with the Host header in the following ways: |
140
|
|
|
* |
141
|
|
|
* - If the Host header is missing or empty, and the new URI contains |
142
|
|
|
* a host component, this method MUST update the Host header in the returned |
143
|
|
|
* request. |
144
|
|
|
* - If the Host header is missing or empty, and the new URI does not contain a |
145
|
|
|
* host component, this method MUST NOT update the Host header in the returned |
146
|
|
|
* request. |
147
|
|
|
* - If a Host header is present and non-empty, this method MUST NOT update |
148
|
|
|
* the Host header in the returned request. |
149
|
|
|
* |
150
|
|
|
* This method MUST be implemented in such a way as to retain the |
151
|
|
|
* immutability of the message, and MUST return an instance that has the |
152
|
|
|
* new UriInterface instance. |
153
|
|
|
* |
154
|
|
|
* @link http://tools.ietf.org/html/rfc3986#section-4.3 |
155
|
|
|
* |
156
|
|
|
* @param UriInterface $uri New request URI to use. |
157
|
|
|
* @param bool $preserveHost Preserve the original state of the Host header. |
158
|
|
|
* |
159
|
|
|
* @return static |
160
|
|
|
*/ |
161
|
|
|
public function withUri(UriInterface $uri, bool $preserveHost = false): RequestInterface |
162
|
|
|
{ |
163
|
|
|
} |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: