Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Diactoros;
|
Chris@0
|
11
|
Chris@0
|
12 use InvalidArgumentException;
|
Chris@0
|
13 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Trait implementing the various methods defined in MessageInterface.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @see https://github.com/php-fig/http-message/tree/master/src/MessageInterface.php
|
Chris@0
|
19 */
|
Chris@0
|
20 trait MessageTrait
|
Chris@0
|
21 {
|
Chris@0
|
22 /**
|
Chris@0
|
23 * List of all registered headers, as key => array of values.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var array
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $headers = [];
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Map of normalized header name to original name used to register header.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var array
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $headerNames = [];
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 private $protocol = '1.1';
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * @var StreamInterface
|
Chris@0
|
43 */
|
Chris@0
|
44 private $stream;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Retrieves the HTTP protocol version as a string.
|
Chris@0
|
48 *
|
Chris@0
|
49 * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
|
Chris@0
|
50 *
|
Chris@0
|
51 * @return string HTTP protocol version.
|
Chris@0
|
52 */
|
Chris@0
|
53 public function getProtocolVersion()
|
Chris@0
|
54 {
|
Chris@0
|
55 return $this->protocol;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Return an instance with the specified HTTP protocol version.
|
Chris@0
|
60 *
|
Chris@0
|
61 * The version string MUST contain only the HTTP version number (e.g.,
|
Chris@0
|
62 * "1.1", "1.0").
|
Chris@0
|
63 *
|
Chris@0
|
64 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
65 * immutability of the message, and MUST return an instance that has the
|
Chris@0
|
66 * new protocol version.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @param string $version HTTP protocol version
|
Chris@0
|
69 * @return static
|
Chris@0
|
70 */
|
Chris@0
|
71 public function withProtocolVersion($version)
|
Chris@0
|
72 {
|
Chris@0
|
73 $this->validateProtocolVersion($version);
|
Chris@0
|
74 $new = clone $this;
|
Chris@0
|
75 $new->protocol = $version;
|
Chris@0
|
76 return $new;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Retrieves all message headers.
|
Chris@0
|
81 *
|
Chris@0
|
82 * The keys represent the header name as it will be sent over the wire, and
|
Chris@0
|
83 * each value is an array of strings associated with the header.
|
Chris@0
|
84 *
|
Chris@0
|
85 * // Represent the headers as a string
|
Chris@0
|
86 * foreach ($message->getHeaders() as $name => $values) {
|
Chris@0
|
87 * echo $name . ": " . implode(", ", $values);
|
Chris@0
|
88 * }
|
Chris@0
|
89 *
|
Chris@0
|
90 * // Emit headers iteratively:
|
Chris@0
|
91 * foreach ($message->getHeaders() as $name => $values) {
|
Chris@0
|
92 * foreach ($values as $value) {
|
Chris@0
|
93 * header(sprintf('%s: %s', $name, $value), false);
|
Chris@0
|
94 * }
|
Chris@0
|
95 * }
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return array Returns an associative array of the message's headers. Each
|
Chris@0
|
98 * key MUST be a header name, and each value MUST be an array of strings.
|
Chris@0
|
99 */
|
Chris@0
|
100 public function getHeaders()
|
Chris@0
|
101 {
|
Chris@0
|
102 return $this->headers;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Checks if a header exists by the given case-insensitive name.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @param string $header Case-insensitive header name.
|
Chris@0
|
109 * @return bool Returns true if any header names match the given header
|
Chris@0
|
110 * name using a case-insensitive string comparison. Returns false if
|
Chris@0
|
111 * no matching header name is found in the message.
|
Chris@0
|
112 */
|
Chris@0
|
113 public function hasHeader($header)
|
Chris@0
|
114 {
|
Chris@0
|
115 return isset($this->headerNames[strtolower($header)]);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Retrieves a message header value by the given case-insensitive name.
|
Chris@0
|
120 *
|
Chris@0
|
121 * This method returns an array of all the header values of the given
|
Chris@0
|
122 * case-insensitive header name.
|
Chris@0
|
123 *
|
Chris@0
|
124 * If the header does not appear in the message, this method MUST return an
|
Chris@0
|
125 * empty array.
|
Chris@0
|
126 *
|
Chris@0
|
127 * @param string $header Case-insensitive header field name.
|
Chris@0
|
128 * @return string[] An array of string values as provided for the given
|
Chris@0
|
129 * header. If the header does not appear in the message, this method MUST
|
Chris@0
|
130 * return an empty array.
|
Chris@0
|
131 */
|
Chris@0
|
132 public function getHeader($header)
|
Chris@0
|
133 {
|
Chris@0
|
134 if (! $this->hasHeader($header)) {
|
Chris@0
|
135 return [];
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 $header = $this->headerNames[strtolower($header)];
|
Chris@0
|
139
|
Chris@0
|
140 return $this->headers[$header];
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * Retrieves a comma-separated string of the values for a single header.
|
Chris@0
|
145 *
|
Chris@0
|
146 * This method returns all of the header values of the given
|
Chris@0
|
147 * case-insensitive header name as a string concatenated together using
|
Chris@0
|
148 * a comma.
|
Chris@0
|
149 *
|
Chris@0
|
150 * NOTE: Not all header values may be appropriately represented using
|
Chris@0
|
151 * comma concatenation. For such headers, use getHeader() instead
|
Chris@0
|
152 * and supply your own delimiter when concatenating.
|
Chris@0
|
153 *
|
Chris@0
|
154 * If the header does not appear in the message, this method MUST return
|
Chris@0
|
155 * an empty string.
|
Chris@0
|
156 *
|
Chris@0
|
157 * @param string $name Case-insensitive header field name.
|
Chris@0
|
158 * @return string A string of values as provided for the given header
|
Chris@0
|
159 * concatenated together using a comma. If the header does not appear in
|
Chris@0
|
160 * the message, this method MUST return an empty string.
|
Chris@0
|
161 */
|
Chris@0
|
162 public function getHeaderLine($name)
|
Chris@0
|
163 {
|
Chris@0
|
164 $value = $this->getHeader($name);
|
Chris@0
|
165 if (empty($value)) {
|
Chris@0
|
166 return '';
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 return implode(',', $value);
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Return an instance with the provided header, replacing any existing
|
Chris@0
|
174 * values of any headers with the same case-insensitive name.
|
Chris@0
|
175 *
|
Chris@0
|
176 * While header names are case-insensitive, the casing of the header will
|
Chris@0
|
177 * be preserved by this function, and returned from getHeaders().
|
Chris@0
|
178 *
|
Chris@0
|
179 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
180 * immutability of the message, and MUST return an instance that has the
|
Chris@0
|
181 * new and/or updated header and value.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @param string $header Case-insensitive header field name.
|
Chris@0
|
184 * @param string|string[] $value Header value(s).
|
Chris@0
|
185 * @return static
|
Chris@0
|
186 * @throws \InvalidArgumentException for invalid header names or values.
|
Chris@0
|
187 */
|
Chris@0
|
188 public function withHeader($header, $value)
|
Chris@0
|
189 {
|
Chris@0
|
190 $this->assertHeader($header);
|
Chris@0
|
191
|
Chris@0
|
192 $normalized = strtolower($header);
|
Chris@0
|
193
|
Chris@0
|
194 $new = clone $this;
|
Chris@0
|
195 if ($new->hasHeader($header)) {
|
Chris@0
|
196 unset($new->headers[$new->headerNames[$normalized]]);
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 $value = $this->filterHeaderValue($value);
|
Chris@0
|
200
|
Chris@0
|
201 $new->headerNames[$normalized] = $header;
|
Chris@0
|
202 $new->headers[$header] = $value;
|
Chris@0
|
203
|
Chris@0
|
204 return $new;
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 /**
|
Chris@0
|
208 * Return an instance with the specified header appended with the
|
Chris@0
|
209 * given value.
|
Chris@0
|
210 *
|
Chris@0
|
211 * Existing values for the specified header will be maintained. The new
|
Chris@0
|
212 * value(s) will be appended to the existing list. If the header did not
|
Chris@0
|
213 * exist previously, it will be added.
|
Chris@0
|
214 *
|
Chris@0
|
215 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
216 * immutability of the message, and MUST return an instance that has the
|
Chris@0
|
217 * new header and/or value.
|
Chris@0
|
218 *
|
Chris@0
|
219 * @param string $header Case-insensitive header field name to add.
|
Chris@0
|
220 * @param string|string[] $value Header value(s).
|
Chris@0
|
221 * @return static
|
Chris@0
|
222 * @throws \InvalidArgumentException for invalid header names or values.
|
Chris@0
|
223 */
|
Chris@0
|
224 public function withAddedHeader($header, $value)
|
Chris@0
|
225 {
|
Chris@0
|
226 $this->assertHeader($header);
|
Chris@0
|
227
|
Chris@0
|
228 if (! $this->hasHeader($header)) {
|
Chris@0
|
229 return $this->withHeader($header, $value);
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 $header = $this->headerNames[strtolower($header)];
|
Chris@0
|
233
|
Chris@0
|
234 $new = clone $this;
|
Chris@0
|
235 $value = $this->filterHeaderValue($value);
|
Chris@0
|
236 $new->headers[$header] = array_merge($this->headers[$header], $value);
|
Chris@0
|
237 return $new;
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 /**
|
Chris@0
|
241 * Return an instance without the specified header.
|
Chris@0
|
242 *
|
Chris@0
|
243 * Header resolution MUST be done without case-sensitivity.
|
Chris@0
|
244 *
|
Chris@0
|
245 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
246 * immutability of the message, and MUST return an instance that removes
|
Chris@0
|
247 * the named header.
|
Chris@0
|
248 *
|
Chris@0
|
249 * @param string $header Case-insensitive header field name to remove.
|
Chris@0
|
250 * @return static
|
Chris@0
|
251 */
|
Chris@0
|
252 public function withoutHeader($header)
|
Chris@0
|
253 {
|
Chris@0
|
254 if (! $this->hasHeader($header)) {
|
Chris@0
|
255 return clone $this;
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 $normalized = strtolower($header);
|
Chris@0
|
259 $original = $this->headerNames[$normalized];
|
Chris@0
|
260
|
Chris@0
|
261 $new = clone $this;
|
Chris@0
|
262 unset($new->headers[$original], $new->headerNames[$normalized]);
|
Chris@0
|
263 return $new;
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * Gets the body of the message.
|
Chris@0
|
268 *
|
Chris@0
|
269 * @return StreamInterface Returns the body as a stream.
|
Chris@0
|
270 */
|
Chris@0
|
271 public function getBody()
|
Chris@0
|
272 {
|
Chris@0
|
273 return $this->stream;
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 /**
|
Chris@0
|
277 * Return an instance with the specified message body.
|
Chris@0
|
278 *
|
Chris@0
|
279 * The body MUST be a StreamInterface object.
|
Chris@0
|
280 *
|
Chris@0
|
281 * This method MUST be implemented in such a way as to retain the
|
Chris@0
|
282 * immutability of the message, and MUST return a new instance that has the
|
Chris@0
|
283 * new body stream.
|
Chris@0
|
284 *
|
Chris@0
|
285 * @param StreamInterface $body Body.
|
Chris@0
|
286 * @return static
|
Chris@0
|
287 * @throws \InvalidArgumentException When the body is not valid.
|
Chris@0
|
288 */
|
Chris@0
|
289 public function withBody(StreamInterface $body)
|
Chris@0
|
290 {
|
Chris@0
|
291 $new = clone $this;
|
Chris@0
|
292 $new->stream = $body;
|
Chris@0
|
293 return $new;
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 private function getStream($stream, $modeIfNotInstance)
|
Chris@0
|
297 {
|
Chris@0
|
298 if ($stream instanceof StreamInterface) {
|
Chris@0
|
299 return $stream;
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 if (! is_string($stream) && ! is_resource($stream)) {
|
Chris@0
|
303 throw new InvalidArgumentException(
|
Chris@0
|
304 'Stream must be a string stream resource identifier, '
|
Chris@0
|
305 . 'an actual stream resource, '
|
Chris@0
|
306 . 'or a Psr\Http\Message\StreamInterface implementation'
|
Chris@0
|
307 );
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 return new Stream($stream, $modeIfNotInstance);
|
Chris@0
|
311 }
|
Chris@0
|
312
|
Chris@0
|
313 /**
|
Chris@0
|
314 * Filter a set of headers to ensure they are in the correct internal format.
|
Chris@0
|
315 *
|
Chris@0
|
316 * Used by message constructors to allow setting all initial headers at once.
|
Chris@0
|
317 *
|
Chris@0
|
318 * @param array $originalHeaders Headers to filter.
|
Chris@0
|
319 */
|
Chris@0
|
320 private function setHeaders(array $originalHeaders)
|
Chris@0
|
321 {
|
Chris@0
|
322 $headerNames = $headers = [];
|
Chris@0
|
323
|
Chris@0
|
324 foreach ($originalHeaders as $header => $value) {
|
Chris@0
|
325 $value = $this->filterHeaderValue($value);
|
Chris@0
|
326
|
Chris@0
|
327 $this->assertHeader($header);
|
Chris@0
|
328
|
Chris@0
|
329 $headerNames[strtolower($header)] = $header;
|
Chris@0
|
330 $headers[$header] = $value;
|
Chris@0
|
331 }
|
Chris@0
|
332
|
Chris@0
|
333 $this->headerNames = $headerNames;
|
Chris@0
|
334 $this->headers = $headers;
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 /**
|
Chris@0
|
338 * Validate the HTTP protocol version
|
Chris@0
|
339 *
|
Chris@0
|
340 * @param string $version
|
Chris@0
|
341 * @throws InvalidArgumentException on invalid HTTP protocol version
|
Chris@0
|
342 */
|
Chris@0
|
343 private function validateProtocolVersion($version)
|
Chris@0
|
344 {
|
Chris@0
|
345 if (empty($version)) {
|
Chris@0
|
346 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
347 'HTTP protocol version can not be empty'
|
Chris@0
|
348 ));
|
Chris@0
|
349 }
|
Chris@0
|
350 if (! is_string($version)) {
|
Chris@0
|
351 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
352 'Unsupported HTTP protocol version; must be a string, received %s',
|
Chris@0
|
353 (is_object($version) ? get_class($version) : gettype($version))
|
Chris@0
|
354 ));
|
Chris@0
|
355 }
|
Chris@0
|
356
|
Chris@0
|
357 // HTTP/1 uses a "<major>.<minor>" numbering scheme to indicate
|
Chris@0
|
358 // versions of the protocol, while HTTP/2 does not.
|
Chris@0
|
359 if (! preg_match('#^(1\.[01]|2)$#', $version)) {
|
Chris@0
|
360 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
361 'Unsupported HTTP protocol version "%s" provided',
|
Chris@0
|
362 $version
|
Chris@0
|
363 ));
|
Chris@0
|
364 }
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 /**
|
Chris@0
|
368 * @param mixed $values
|
Chris@0
|
369 * @return string[]
|
Chris@0
|
370 */
|
Chris@0
|
371 private function filterHeaderValue($values)
|
Chris@0
|
372 {
|
Chris@0
|
373 if (! is_array($values)) {
|
Chris@0
|
374 $values = [$values];
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 return array_map(function ($value) {
|
Chris@0
|
378 HeaderSecurity::assertValid($value);
|
Chris@0
|
379
|
Chris@0
|
380 return (string) $value;
|
Chris@0
|
381 }, $values);
|
Chris@0
|
382 }
|
Chris@0
|
383
|
Chris@0
|
384 /**
|
Chris@0
|
385 * Ensure header name and values are valid.
|
Chris@0
|
386 *
|
Chris@0
|
387 * @param string $name
|
Chris@0
|
388 *
|
Chris@0
|
389 * @throws InvalidArgumentException
|
Chris@0
|
390 */
|
Chris@0
|
391 private function assertHeader($name)
|
Chris@0
|
392 {
|
Chris@0
|
393 HeaderSecurity::assertValidName($name);
|
Chris@0
|
394 }
|
Chris@0
|
395 }
|