/vendor/slim/slim/Slim/Http/Message.php

https://gitlab.com/ramos.lauty/softlord · PHP · 304 lines · 79 code · 26 blank · 199 comment · 1 complexity · 0074be5301de84606132c9606621bef5 MD5 · raw file

  1. <?php
  2. /**
  3. * Slim Framework (http://slimframework.com)
  4. *
  5. * @link https://github.com/slimphp/Slim
  6. * @copyright Copyright (c) 2011-2016 Josh Lockhart
  7. * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
  8. */
  9. namespace Slim\Http;
  10. use InvalidArgumentException;
  11. use Psr\Http\Message\MessageInterface;
  12. use Psr\Http\Message\StreamInterface;
  13. /**
  14. * Abstract message (base class for Request and Response)
  15. *
  16. * This class represents a general HTTP message. It provides common properties and methods for
  17. * the HTTP request and response, as defined in the PSR-7 MessageInterface.
  18. *
  19. * @link https://github.com/php-fig/http-message/blob/master/src/MessageInterface.php
  20. * @see Slim\Http\Request
  21. * @see Slim\Http\Response
  22. */
  23. abstract class Message implements MessageInterface
  24. {
  25. /**
  26. * Protocol version
  27. *
  28. * @var string
  29. */
  30. protected $protocolVersion = '1.1';
  31. /**
  32. * A map of valid protocol versions
  33. *
  34. * @var array
  35. */
  36. protected static $validProtocolVersions = [
  37. '1.0' => true,
  38. '1.1' => true,
  39. '2.0' => true,
  40. ];
  41. /**
  42. * Headers
  43. *
  44. * @var \Slim\Interfaces\Http\HeadersInterface
  45. */
  46. protected $headers;
  47. /**
  48. * Body object
  49. *
  50. * @var \Psr\Http\Message\StreamInterface
  51. */
  52. protected $body;
  53. /**
  54. * Disable magic setter to ensure immutability
  55. */
  56. public function __set($name, $value)
  57. {
  58. // Do nothing
  59. }
  60. /*******************************************************************************
  61. * Protocol
  62. ******************************************************************************/
  63. /**
  64. * Retrieves the HTTP protocol version as a string.
  65. *
  66. * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
  67. *
  68. * @return string HTTP protocol version.
  69. */
  70. public function getProtocolVersion()
  71. {
  72. return $this->protocolVersion;
  73. }
  74. /**
  75. * Return an instance with the specified HTTP protocol version.
  76. *
  77. * The version string MUST contain only the HTTP version number (e.g.,
  78. * "1.1", "1.0").
  79. *
  80. * This method MUST be implemented in such a way as to retain the
  81. * immutability of the message, and MUST return an instance that has the
  82. * new protocol version.
  83. *
  84. * @param string $version HTTP protocol version
  85. * @return static
  86. * @throws InvalidArgumentException if the http version is an invalid number
  87. */
  88. public function withProtocolVersion($version)
  89. {
  90. if (!isset(self::$validProtocolVersions[$version])) {
  91. throw new InvalidArgumentException(
  92. 'Invalid HTTP version. Must be one of: '
  93. . implode(', ', array_keys(self::$validProtocolVersions))
  94. );
  95. }
  96. $clone = clone $this;
  97. $clone->protocolVersion = $version;
  98. return $clone;
  99. }
  100. /*******************************************************************************
  101. * Headers
  102. ******************************************************************************/
  103. /**
  104. * Retrieves all message header values.
  105. *
  106. * The keys represent the header name as it will be sent over the wire, and
  107. * each value is an array of strings associated with the header.
  108. *
  109. * // Represent the headers as a string
  110. * foreach ($message->getHeaders() as $name => $values) {
  111. * echo $name . ": " . implode(", ", $values);
  112. * }
  113. *
  114. * // Emit headers iteratively:
  115. * foreach ($message->getHeaders() as $name => $values) {
  116. * foreach ($values as $value) {
  117. * header(sprintf('%s: %s', $name, $value), false);
  118. * }
  119. * }
  120. *
  121. * While header names are not case-sensitive, getHeaders() will preserve the
  122. * exact case in which headers were originally specified.
  123. *
  124. * @return array Returns an associative array of the message's headers. Each
  125. * key MUST be a header name, and each value MUST be an array of strings
  126. * for that header.
  127. */
  128. public function getHeaders()
  129. {
  130. return $this->headers->all();
  131. }
  132. /**
  133. * Checks if a header exists by the given case-insensitive name.
  134. *
  135. * @param string $name Case-insensitive header field name.
  136. * @return bool Returns true if any header names match the given header
  137. * name using a case-insensitive string comparison. Returns false if
  138. * no matching header name is found in the message.
  139. */
  140. public function hasHeader($name)
  141. {
  142. return $this->headers->has($name);
  143. }
  144. /**
  145. * Retrieves a message header value by the given case-insensitive name.
  146. *
  147. * This method returns an array of all the header values of the given
  148. * case-insensitive header name.
  149. *
  150. * If the header does not appear in the message, this method MUST return an
  151. * empty array.
  152. *
  153. * @param string $name Case-insensitive header field name.
  154. * @return string[] An array of string values as provided for the given
  155. * header. If the header does not appear in the message, this method MUST
  156. * return an empty array.
  157. */
  158. public function getHeader($name)
  159. {
  160. return $this->headers->get($name, []);
  161. }
  162. /**
  163. * Retrieves a comma-separated string of the values for a single header.
  164. *
  165. * This method returns all of the header values of the given
  166. * case-insensitive header name as a string concatenated together using
  167. * a comma.
  168. *
  169. * NOTE: Not all header values may be appropriately represented using
  170. * comma concatenation. For such headers, use getHeader() instead
  171. * and supply your own delimiter when concatenating.
  172. *
  173. * If the header does not appear in the message, this method MUST return
  174. * an empty string.
  175. *
  176. * @param string $name Case-insensitive header field name.
  177. * @return string A string of values as provided for the given header
  178. * concatenated together using a comma. If the header does not appear in
  179. * the message, this method MUST return an empty string.
  180. */
  181. public function getHeaderLine($name)
  182. {
  183. return implode(',', $this->headers->get($name, []));
  184. }
  185. /**
  186. * Return an instance with the provided value replacing the specified header.
  187. *
  188. * While header names are case-insensitive, the casing of the header will
  189. * be preserved by this function, and returned from getHeaders().
  190. *
  191. * This method MUST be implemented in such a way as to retain the
  192. * immutability of the message, and MUST return an instance that has the
  193. * new and/or updated header and value.
  194. *
  195. * @param string $name Case-insensitive header field name.
  196. * @param string|string[] $value Header value(s).
  197. * @return static
  198. * @throws \InvalidArgumentException for invalid header names or values.
  199. */
  200. public function withHeader($name, $value)
  201. {
  202. $clone = clone $this;
  203. $clone->headers->set($name, $value);
  204. return $clone;
  205. }
  206. /**
  207. * Return an instance with the specified header appended with the given value.
  208. *
  209. * Existing values for the specified header will be maintained. The new
  210. * value(s) will be appended to the existing list. If the header did not
  211. * exist previously, it will be added.
  212. *
  213. * This method MUST be implemented in such a way as to retain the
  214. * immutability of the message, and MUST return an instance that has the
  215. * new header and/or value.
  216. *
  217. * @param string $name Case-insensitive header field name to add.
  218. * @param string|string[] $value Header value(s).
  219. * @return static
  220. * @throws \InvalidArgumentException for invalid header names or values.
  221. */
  222. public function withAddedHeader($name, $value)
  223. {
  224. $clone = clone $this;
  225. $clone->headers->add($name, $value);
  226. return $clone;
  227. }
  228. /**
  229. * Return an instance without the specified header.
  230. *
  231. * Header resolution MUST be done without case-sensitivity.
  232. *
  233. * This method MUST be implemented in such a way as to retain the
  234. * immutability of the message, and MUST return an instance that removes
  235. * the named header.
  236. *
  237. * @param string $name Case-insensitive header field name to remove.
  238. * @return static
  239. */
  240. public function withoutHeader($name)
  241. {
  242. $clone = clone $this;
  243. $clone->headers->remove($name);
  244. return $clone;
  245. }
  246. /*******************************************************************************
  247. * Body
  248. ******************************************************************************/
  249. /**
  250. * Gets the body of the message.
  251. *
  252. * @return StreamInterface Returns the body as a stream.
  253. */
  254. public function getBody()
  255. {
  256. return $this->body;
  257. }
  258. /**
  259. * Return an instance with the specified message body.
  260. *
  261. * The body MUST be a StreamInterface object.
  262. *
  263. * This method MUST be implemented in such a way as to retain the
  264. * immutability of the message, and MUST return a new instance that has the
  265. * new body stream.
  266. *
  267. * @param StreamInterface $body Body.
  268. * @return static
  269. * @throws \InvalidArgumentException When the body is not valid.
  270. */
  271. public function withBody(StreamInterface $body)
  272. {
  273. // TODO: Test for invalid body?
  274. $clone = clone $this;
  275. $clone->body = $body;
  276. return $clone;
  277. }
  278. }