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