composer package updates
[openemr.git] / vendor / zendframework / zend-diactoros / src / MessageTrait.php
blobd249d86ac7dde294d2fde99ee3c9093120db34b8
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Diactoros;
10 use InvalidArgumentException;
11 use Psr\Http\Message\StreamInterface;
13 use function array_map;
14 use function array_merge;
15 use function get_class;
16 use function gettype;
17 use function implode;
18 use function is_array;
19 use function is_object;
20 use function is_resource;
21 use function is_string;
22 use function preg_match;
23 use function sprintf;
24 use function strtolower;
26 /**
27 * Trait implementing the various methods defined in MessageInterface.
29 * @see https://github.com/php-fig/http-message/tree/master/src/MessageInterface.php
31 trait MessageTrait
33 /**
34 * List of all registered headers, as key => array of values.
36 * @var array
38 protected $headers = [];
40 /**
41 * Map of normalized header name to original name used to register header.
43 * @var array
45 protected $headerNames = [];
47 /**
48 * @var string
50 private $protocol = '1.1';
52 /**
53 * @var StreamInterface
55 private $stream;
57 /**
58 * Retrieves the HTTP protocol version as a string.
60 * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
62 * @return string HTTP protocol version.
64 public function getProtocolVersion()
66 return $this->protocol;
69 /**
70 * Return an instance with the specified HTTP protocol version.
72 * The version string MUST contain only the HTTP version number (e.g.,
73 * "1.1", "1.0").
75 * This method MUST be implemented in such a way as to retain the
76 * immutability of the message, and MUST return an instance that has the
77 * new protocol version.
79 * @param string $version HTTP protocol version
80 * @return static
82 public function withProtocolVersion($version)
84 $this->validateProtocolVersion($version);
85 $new = clone $this;
86 $new->protocol = $version;
87 return $new;
90 /**
91 * Retrieves all message headers.
93 * The keys represent the header name as it will be sent over the wire, and
94 * each value is an array of strings associated with the header.
96 * // Represent the headers as a string
97 * foreach ($message->getHeaders() as $name => $values) {
98 * echo $name . ": " . implode(", ", $values);
99 * }
101 * // Emit headers iteratively:
102 * foreach ($message->getHeaders() as $name => $values) {
103 * foreach ($values as $value) {
104 * header(sprintf('%s: %s', $name, $value), false);
108 * @return array Returns an associative array of the message's headers. Each
109 * key MUST be a header name, and each value MUST be an array of strings.
111 public function getHeaders()
113 return $this->headers;
117 * Checks if a header exists by the given case-insensitive name.
119 * @param string $header Case-insensitive header name.
120 * @return bool Returns true if any header names match the given header
121 * name using a case-insensitive string comparison. Returns false if
122 * no matching header name is found in the message.
124 public function hasHeader($header)
126 return isset($this->headerNames[strtolower($header)]);
130 * Retrieves a message header value by the given case-insensitive name.
132 * This method returns an array of all the header values of the given
133 * case-insensitive header name.
135 * If the header does not appear in the message, this method MUST return an
136 * empty array.
138 * @param string $header Case-insensitive header field name.
139 * @return string[] An array of string values as provided for the given
140 * header. If the header does not appear in the message, this method MUST
141 * return an empty array.
143 public function getHeader($header)
145 if (! $this->hasHeader($header)) {
146 return [];
149 $header = $this->headerNames[strtolower($header)];
151 return $this->headers[$header];
155 * Retrieves a comma-separated string of the values for a single header.
157 * This method returns all of the header values of the given
158 * case-insensitive header name as a string concatenated together using
159 * a comma.
161 * NOTE: Not all header values may be appropriately represented using
162 * comma concatenation. For such headers, use getHeader() instead
163 * and supply your own delimiter when concatenating.
165 * If the header does not appear in the message, this method MUST return
166 * an empty string.
168 * @param string $name Case-insensitive header field name.
169 * @return string A string of values as provided for the given header
170 * concatenated together using a comma. If the header does not appear in
171 * the message, this method MUST return an empty string.
173 public function getHeaderLine($name)
175 $value = $this->getHeader($name);
176 if (empty($value)) {
177 return '';
180 return implode(',', $value);
184 * Return an instance with the provided header, replacing any existing
185 * values of any headers with the same case-insensitive name.
187 * While header names are case-insensitive, the casing of the header will
188 * be preserved by this function, and returned from getHeaders().
190 * This method MUST be implemented in such a way as to retain the
191 * immutability of the message, and MUST return an instance that has the
192 * new and/or updated header and value.
194 * @param string $header Case-insensitive header field name.
195 * @param string|string[] $value Header value(s).
196 * @return static
197 * @throws \InvalidArgumentException for invalid header names or values.
199 public function withHeader($header, $value)
201 $this->assertHeader($header);
203 $normalized = strtolower($header);
205 $new = clone $this;
206 if ($new->hasHeader($header)) {
207 unset($new->headers[$new->headerNames[$normalized]]);
210 $value = $this->filterHeaderValue($value);
212 $new->headerNames[$normalized] = $header;
213 $new->headers[$header] = $value;
215 return $new;
219 * Return an instance with the specified header appended with the
220 * given value.
222 * Existing values for the specified header will be maintained. The new
223 * value(s) will be appended to the existing list. If the header did not
224 * exist previously, it will be added.
226 * This method MUST be implemented in such a way as to retain the
227 * immutability of the message, and MUST return an instance that has the
228 * new header and/or value.
230 * @param string $header Case-insensitive header field name to add.
231 * @param string|string[] $value Header value(s).
232 * @return static
233 * @throws \InvalidArgumentException for invalid header names or values.
235 public function withAddedHeader($header, $value)
237 $this->assertHeader($header);
239 if (! $this->hasHeader($header)) {
240 return $this->withHeader($header, $value);
243 $header = $this->headerNames[strtolower($header)];
245 $new = clone $this;
246 $value = $this->filterHeaderValue($value);
247 $new->headers[$header] = array_merge($this->headers[$header], $value);
248 return $new;
252 * Return an instance without the specified header.
254 * Header resolution MUST be done without case-sensitivity.
256 * This method MUST be implemented in such a way as to retain the
257 * immutability of the message, and MUST return an instance that removes
258 * the named header.
260 * @param string $header Case-insensitive header field name to remove.
261 * @return static
263 public function withoutHeader($header)
265 if (! $this->hasHeader($header)) {
266 return clone $this;
269 $normalized = strtolower($header);
270 $original = $this->headerNames[$normalized];
272 $new = clone $this;
273 unset($new->headers[$original], $new->headerNames[$normalized]);
274 return $new;
278 * Gets the body of the message.
280 * @return StreamInterface Returns the body as a stream.
282 public function getBody()
284 return $this->stream;
288 * Return an instance with the specified message body.
290 * The body MUST be a StreamInterface object.
292 * This method MUST be implemented in such a way as to retain the
293 * immutability of the message, and MUST return a new instance that has the
294 * new body stream.
296 * @param StreamInterface $body Body.
297 * @return static
298 * @throws \InvalidArgumentException When the body is not valid.
300 public function withBody(StreamInterface $body)
302 $new = clone $this;
303 $new->stream = $body;
304 return $new;
307 private function getStream($stream, $modeIfNotInstance)
309 if ($stream instanceof StreamInterface) {
310 return $stream;
313 if (! is_string($stream) && ! is_resource($stream)) {
314 throw new InvalidArgumentException(
315 'Stream must be a string stream resource identifier, '
316 . 'an actual stream resource, '
317 . 'or a Psr\Http\Message\StreamInterface implementation'
321 return new Stream($stream, $modeIfNotInstance);
325 * Filter a set of headers to ensure they are in the correct internal format.
327 * Used by message constructors to allow setting all initial headers at once.
329 * @param array $originalHeaders Headers to filter.
331 private function setHeaders(array $originalHeaders)
333 $headerNames = $headers = [];
335 foreach ($originalHeaders as $header => $value) {
336 $value = $this->filterHeaderValue($value);
338 $this->assertHeader($header);
340 $headerNames[strtolower($header)] = $header;
341 $headers[$header] = $value;
344 $this->headerNames = $headerNames;
345 $this->headers = $headers;
349 * Validate the HTTP protocol version
351 * @param string $version
352 * @throws InvalidArgumentException on invalid HTTP protocol version
354 private function validateProtocolVersion($version)
356 if (empty($version)) {
357 throw new InvalidArgumentException(
358 'HTTP protocol version can not be empty'
361 if (! is_string($version)) {
362 throw new InvalidArgumentException(sprintf(
363 'Unsupported HTTP protocol version; must be a string, received %s',
364 (is_object($version) ? get_class($version) : gettype($version))
368 // HTTP/1 uses a "<major>.<minor>" numbering scheme to indicate
369 // versions of the protocol, while HTTP/2 does not.
370 if (! preg_match('#^(1\.[01]|2)$#', $version)) {
371 throw new InvalidArgumentException(sprintf(
372 'Unsupported HTTP protocol version "%s" provided',
373 $version
379 * @param mixed $values
380 * @return string[]
382 private function filterHeaderValue($values)
384 if (! is_array($values)) {
385 $values = [$values];
388 return array_map(function ($value) {
389 HeaderSecurity::assertValid($value);
391 return (string) $value;
392 }, $values);
396 * Ensure header name and values are valid.
398 * @param string $name
400 * @throws InvalidArgumentException
402 private function assertHeader($name)
404 HeaderSecurity::assertValidName($name);