1 | <?php |
||
17 | trait RequestTrait |
||
18 | { |
||
19 | /** |
||
20 | * Message request target |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private $requestTarget = null; |
||
25 | |||
26 | /** |
||
27 | * Method of incoming request - GET, POST, DELETE, PUT, PATCH, HEAD or OPTIONS. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $requestMethod; |
||
32 | |||
33 | /** |
||
34 | * Uri of incoming request |
||
35 | * |
||
36 | * @var Psr\Http\Message\UriInterface; |
||
37 | */ |
||
38 | protected $uri; |
||
39 | |||
40 | /** |
||
41 | * Checks if a header exists by the given case-insensitive name. |
||
42 | * |
||
43 | * @param string $name Case-insensitive header field name. |
||
44 | * |
||
45 | * @return bool Returns true if any header names match the given header name using |
||
46 | * a case-insensitive string comparison. Returns false if no matching |
||
47 | * header name is found in the message. |
||
48 | */ |
||
49 | abstract public function hasHeader($name); |
||
50 | |||
51 | /** |
||
52 | * Retrieves a message header value by the given case-insensitive name. |
||
53 | * |
||
54 | * This method returns an array of all the header values of the given |
||
55 | * case-insensitive header name. |
||
56 | * |
||
57 | * If the header does not appear in the message, this method return an |
||
58 | * empty array. |
||
59 | * |
||
60 | * @param string $name Case-insensitive header field name. |
||
61 | * |
||
62 | * @return string[] An array of string values as provided for the given header. |
||
63 | * If the header does not appear in the message, this method MUST |
||
64 | * return an empty array. |
||
65 | */ |
||
66 | abstract public function getHeader($name); |
||
67 | |||
68 | /** |
||
69 | * Provide message headers |
||
70 | * |
||
71 | * @return Headers Message headers |
||
72 | */ |
||
73 | abstract protected function provideHeaders(); |
||
74 | |||
75 | /** |
||
76 | * Retrieves the message's request target. |
||
77 | * |
||
78 | * Retrieves the message's request-target either as it will appear (for |
||
79 | * clients), as it appeared at request (for servers), or as it was |
||
80 | * specified for the instance (see withRequestTarget()). |
||
81 | * |
||
82 | * In most cases, this will be the origin-form of the composed URI, |
||
83 | * unless a value was provided to the concrete implementation (see |
||
84 | * withRequestTarget() below). |
||
85 | * |
||
86 | * If no URI is available, and no request-target has been specifically |
||
87 | * provided, this method return the string "/". |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 4 | public function getRequestTarget() |
|
105 | |||
106 | /** |
||
107 | * Return an instance with the specific request-target. |
||
108 | * |
||
109 | * If the request needs a non-origin-form request-target — e.g., for |
||
110 | * specifying an absolute-form, authority-form, or asterisk-form — |
||
111 | * this method may be used to create an instance with the specified |
||
112 | * request-target, verbatim. |
||
113 | * |
||
114 | * This method retains the state of the current instance, and return |
||
115 | * an instance that has the changed request target. |
||
116 | * |
||
117 | * @link http://tools.ietf.org/html/rfc7230#section-2.7 (for the various |
||
118 | * request-target forms allowed in request messages) |
||
119 | * |
||
120 | * @param mixed $requestTarget |
||
121 | * |
||
122 | * @return self |
||
123 | */ |
||
124 | 2 | public function withRequestTarget($requestTarget) |
|
131 | |||
132 | /** |
||
133 | * Retrieves the HTTP method of the request. |
||
134 | * |
||
135 | * @return string Returns the request method. |
||
136 | */ |
||
137 | 4 | public function getMethod() |
|
141 | |||
142 | /** |
||
143 | * Return an instance with the provided HTTP method. |
||
144 | * |
||
145 | * While HTTP method names are typically all uppercase characters, HTTP |
||
146 | * method names are case-sensitive and thus implementations not |
||
147 | * modify the given string. |
||
148 | * |
||
149 | * This method retains the state of the current instance, and return |
||
150 | * an instance that changed request method. |
||
151 | * |
||
152 | * @param string $method Case-sensitive method. |
||
153 | * |
||
154 | * @return self |
||
155 | * |
||
156 | * @throws \InvalidArgumentException for invalid HTTP methods. |
||
157 | */ |
||
158 | 4 | public function withMethod($method) |
|
167 | |||
168 | /** |
||
169 | * Retrieves the URI instance. |
||
170 | * |
||
171 | * This method return a UriInterface instance. |
||
172 | * |
||
173 | * @link http://tools.ietf.org/html/rfc3986#section-4.3 |
||
174 | * |
||
175 | * @return UriInterface Returns a UriInterface instance representing the URI of the request. |
||
176 | */ |
||
177 | 7 | public function getUri() |
|
181 | |||
182 | /** |
||
183 | * Returns an instance with the provided URI. |
||
184 | * |
||
185 | * This method update the Host header of the returned request by |
||
186 | * default if the URI contains a host component. If the URI does not |
||
187 | * contain a host component, any pre-existing Host header is carried |
||
188 | * over to the returned request. |
||
189 | * |
||
190 | * You can opt-in to preserving the original state of the Host header by |
||
191 | * setting `$preserveHost` to `true`. When `$preserveHost` is set to |
||
192 | * `true`, this method interacts with the Host header in the following ways: |
||
193 | * |
||
194 | * - If the the Host header is missing or empty, and the new URI contains |
||
195 | * a host component, this method update the Host header in the returned |
||
196 | * request. |
||
197 | * - If the Host header is missing or empty, and the new URI does not contain a |
||
198 | * host component, this method not update the Host header in the returned |
||
199 | * request. |
||
200 | * - If a Host header is present and non-empty, this method not update |
||
201 | * the Host header in the returned request. |
||
202 | * |
||
203 | * This method retains the state of the current instance, and return |
||
204 | * an instance that contains the new UriInterface instance. |
||
205 | * |
||
206 | * @link http://tools.ietf.org/html/rfc3986#section-4.3 |
||
207 | * |
||
208 | * @param UriInterface $uri New request URI to use. |
||
209 | * @param bool $preserveHost Preserve the original state of the Host header. |
||
210 | * |
||
211 | * @return self |
||
212 | */ |
||
213 | 4 | public function withUri(UriInterface $uri, $preserveHost = false) |
|
230 | |||
231 | /** |
||
232 | * Check if the host header should be set. |
||
233 | * |
||
234 | * @return bool true if should be |
||
235 | */ |
||
236 | 11 | protected function shouldSetHost() |
|
240 | |||
241 | /** |
||
242 | * Validate request method. |
||
243 | * |
||
244 | * @param string $method Case-sensitive request method. |
||
245 | * |
||
246 | * @return void |
||
247 | * |
||
248 | * @throws \InvalidArgumentException for invalid HTTP methods. |
||
249 | */ |
||
250 | 11 | protected function validateMethod($method) |
|
268 | } |
||
269 |