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