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 | * Retrieves the message's request target. |
||
46 | * |
||
47 | * Retrieves the message's request-target either as it will appear (for |
||
48 | * clients), as it appeared at request (for servers), or as it was |
||
49 | * specified for the instance (see withRequestTarget()). |
||
50 | * |
||
51 | * In most cases, this will be the origin-form of the composed URI, |
||
52 | * unless a value was provided to the concrete implementation (see |
||
53 | * withRequestTarget() below). |
||
54 | * |
||
55 | * If no URI is available, and no request-target has been specifically |
||
56 | * provided, this method return the string "/". |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 4 | public function getRequestTarget() |
|
74 | |||
75 | /** |
||
76 | * Return an instance with the specific request-target. |
||
77 | * |
||
78 | * If the request needs a non-origin-form request-target — e.g., for |
||
79 | * specifying an absolute-form, authority-form, or asterisk-form — |
||
80 | * this method may be used to create an instance with the specified |
||
81 | * request-target, verbatim. |
||
82 | * |
||
83 | * This method retains the state of the current instance, and return |
||
84 | * an instance that has the changed request target. |
||
85 | * |
||
86 | * @link http://tools.ietf.org/html/rfc7230#section-2.7 (for the various |
||
87 | * request-target forms allowed in request messages) |
||
88 | * |
||
89 | * @param mixed $requestTarget |
||
90 | * |
||
91 | * @return self |
||
92 | */ |
||
93 | 2 | public function withRequestTarget($requestTarget) |
|
100 | |||
101 | /** |
||
102 | * Retrieves the HTTP method of the request. |
||
103 | * |
||
104 | * @return string Returns the request method. |
||
105 | */ |
||
106 | 4 | public function getMethod() |
|
110 | |||
111 | /** |
||
112 | * Return an instance with the provided HTTP method. |
||
113 | * |
||
114 | * While HTTP method names are typically all uppercase characters, HTTP |
||
115 | * method names are case-sensitive and thus implementations not |
||
116 | * modify the given string. |
||
117 | * |
||
118 | * This method retains the state of the current instance, and return |
||
119 | * an instance that changed request method. |
||
120 | * |
||
121 | * @param string $method Case-sensitive method. |
||
122 | * |
||
123 | * @return self |
||
124 | * |
||
125 | * @throws \InvalidArgumentException for invalid HTTP methods. |
||
126 | */ |
||
127 | 4 | public function withMethod($method) |
|
135 | |||
136 | /** |
||
137 | * Retrieves the URI instance. |
||
138 | * |
||
139 | * This method return a UriInterface instance. |
||
140 | * |
||
141 | * @link http://tools.ietf.org/html/rfc3986#section-4.3 |
||
142 | * |
||
143 | * @return UriInterface Returns a UriInterface instance representing the URI of the request. |
||
144 | */ |
||
145 | 6 | public function getUri() |
|
149 | |||
150 | /** |
||
151 | * Returns an instance with the provided URI. |
||
152 | * |
||
153 | * This method update the Host header of the returned request by |
||
154 | * default if the URI contains a host component. If the URI does not |
||
155 | * contain a host component, any pre-existing Host header is carried |
||
156 | * over to the returned request. |
||
157 | * |
||
158 | * You can opt-in to preserving the original state of the Host header by |
||
159 | * setting `$preserveHost` to `true`. When `$preserveHost` is set to |
||
160 | * `true`, this method interacts with the Host header in the following ways: |
||
161 | * |
||
162 | * - If the the Host header is missing or empty, and the new URI contains |
||
163 | * a host component, this method update the Host header in the returned |
||
164 | * request. |
||
165 | * - If the Host header is missing or empty, and the new URI does not contain a |
||
166 | * host component, this method not update the Host header in the returned |
||
167 | * request. |
||
168 | * - If a Host header is present and non-empty, this method not update |
||
169 | * the Host header in the returned request. |
||
170 | * |
||
171 | * This method retains the state of the current instance, and return |
||
172 | * an instance that contains the new UriInterface instance. |
||
173 | * |
||
174 | * @link http://tools.ietf.org/html/rfc3986#section-4.3 |
||
175 | * |
||
176 | * @param UriInterface $uri New request URI to use. |
||
177 | * @param bool $preserveHost Preserve the original state of the Host header. |
||
178 | * |
||
179 | * @return self |
||
180 | */ |
||
181 | 4 | public function withUri(UriInterface $uri, $preserveHost = false) |
|
182 | { |
||
183 | 4 | $clone = clone $this; |
|
184 | 4 | $clone->uri = $uri; |
|
185 | |||
186 | 4 | if (!$preserveHost) { |
|
187 | 2 | if ($uri->getHost() !== '') { |
|
188 | 2 | $clone->headers->set('Host', $uri->getHost()); |
|
|
|||
189 | 2 | } |
|
190 | 2 | } else { |
|
191 | 2 | if ($this->uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeader('Host') === null)) { |
|
192 | 1 | $clone->headers->set('Host', $uri->getHost()); |
|
193 | 1 | } |
|
194 | } |
||
195 | |||
196 | 4 | return $clone; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Checks if a header exists by the given case-insensitive name. |
||
201 | * |
||
202 | * @param string $name Case-insensitive header field name. |
||
203 | * |
||
204 | * @return bool Returns true if any header names match the given header name using |
||
205 | * a case-insensitive string comparison. Returns false if no matching |
||
206 | * header name is found in the message. |
||
207 | */ |
||
208 | abstract public function hasHeader($name); |
||
209 | |||
210 | /** |
||
211 | * Retrieves a message header value by the given case-insensitive name. |
||
212 | * |
||
213 | * This method returns an array of all the header values of the given |
||
214 | * case-insensitive header name. |
||
215 | * |
||
216 | * If the header does not appear in the message, this method return an |
||
217 | * empty array. |
||
218 | * |
||
219 | * @param string $name Case-insensitive header field name. |
||
220 | * |
||
221 | * @return string[] An array of string values as provided for the given header. |
||
222 | * If the header does not appear in the message, this method MUST |
||
223 | * return an empty array. |
||
224 | */ |
||
225 | abstract public function getHeader($name); |
||
226 | |||
227 | /** |
||
228 | * Validate request method. |
||
229 | * |
||
230 | * @param string $method Case-sensitive request method. |
||
231 | * |
||
232 | * @return void |
||
233 | * |
||
234 | * @throws \InvalidArgumentException for invalid HTTP methods. |
||
235 | */ |
||
236 | 10 | protected function validateMethod($method) |
|
254 | } |
||
255 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: