PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/System/vendor/slim/slim/Slim/Http/Headers.php

https://gitlab.com/leon0399/damnit-engine
PHP | 198 lines | 84 code | 16 blank | 98 comment | 7 complexity | a8791f5777db00fbcfaf3ef63ffa34c3 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-2015 Josh Lockhart
  7. * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
  8. */
  9. namespace Slim\Http;
  10. use Slim\Collection;
  11. use Slim\Http\Environment;
  12. use Slim\Interfaces\Http\HeadersInterface;
  13. /**
  14. * Headers
  15. *
  16. * This class represents a collection of HTTP headers
  17. * that is used in both the HTTP request and response objects.
  18. * It also enables header name case-insensitivity when
  19. * getting or setting a header value.
  20. *
  21. * Each HTTP header can have multiple values. This class
  22. * stores values into an array for each header name. When
  23. * you request a header value, you receive an array of values
  24. * for that header.
  25. */
  26. class Headers extends Collection implements HeadersInterface
  27. {
  28. /**
  29. * Special HTTP headers that do not have the "HTTP_" prefix
  30. *
  31. * @var array
  32. */
  33. protected static $special = [
  34. 'CONTENT_TYPE' => 1,
  35. 'CONTENT_LENGTH' => 1,
  36. 'PHP_AUTH_USER' => 1,
  37. 'PHP_AUTH_PW' => 1,
  38. 'PHP_AUTH_DIGEST' => 1,
  39. 'AUTH_TYPE' => 1,
  40. ];
  41. /**
  42. * Create new headers collection with data extracted from
  43. * the application Environment object
  44. *
  45. * @param Environment $environment The Slim application Environment
  46. *
  47. * @return self
  48. */
  49. public static function createFromEnvironment(Environment $environment)
  50. {
  51. $data = [];
  52. foreach ($environment as $key => $value) {
  53. $key = strtoupper($key);
  54. if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) {
  55. if ($key !== 'HTTP_CONTENT_LENGTH') {
  56. $data[$key] = $value;
  57. }
  58. }
  59. }
  60. return new static($data);
  61. }
  62. /**
  63. * Return array of HTTP header names and values.
  64. * This method returns the _original_ header name
  65. * as specified by the end user.
  66. *
  67. * @return array
  68. */
  69. public function all()
  70. {
  71. $all = parent::all();
  72. $out = [];
  73. foreach ($all as $key => $props) {
  74. $out[$props['originalKey']] = $props['value'];
  75. }
  76. return $out;
  77. }
  78. /**
  79. * Set HTTP header value
  80. *
  81. * This method sets a header value. It replaces
  82. * any values that may already exist for the header name.
  83. *
  84. * @param string $key The case-insensitive header name
  85. * @param string $value The header value
  86. */
  87. public function set($key, $value)
  88. {
  89. if (!is_array($value)) {
  90. $value = [$value];
  91. }
  92. parent::set($this->normalizeKey($key), [
  93. 'value' => $value,
  94. 'originalKey' => $key
  95. ]);
  96. }
  97. /**
  98. * Get HTTP header value
  99. *
  100. * @param string $key The case-insensitive header name
  101. * @param mixed $default The default value if key does not exist
  102. *
  103. * @return string[]
  104. */
  105. public function get($key, $default = null)
  106. {
  107. if ($this->has($key)) {
  108. return parent::get($this->normalizeKey($key))['value'];
  109. }
  110. return $default;
  111. }
  112. /**
  113. * Get HTTP header key as originally specified
  114. *
  115. * @param string $key The case-insensitive header name
  116. * @param mixed $default The default value if key does not exist
  117. *
  118. * @return string
  119. */
  120. public function getOriginalKey($key, $default = null)
  121. {
  122. if ($this->has($key)) {
  123. return parent::get($this->normalizeKey($key))['originalKey'];
  124. }
  125. return $default;
  126. }
  127. /**
  128. * Add HTTP header value
  129. *
  130. * This method appends a header value. Unlike the set() method,
  131. * this method _appends_ this new value to any values
  132. * that already exist for this header name.
  133. *
  134. * @param string $key The case-insensitive header name
  135. * @param array|string $value The new header value(s)
  136. */
  137. public function add($key, $value)
  138. {
  139. $oldValues = $this->get($key, []);
  140. $newValues = is_array($value) ? $value : [$value];
  141. $this->set($key, array_merge($oldValues, array_values($newValues)));
  142. }
  143. /**
  144. * Does this collection have a given header?
  145. *
  146. * @param string $key The case-insensitive header name
  147. *
  148. * @return bool
  149. */
  150. public function has($key)
  151. {
  152. return parent::has($this->normalizeKey($key));
  153. }
  154. /**
  155. * Remove header from collection
  156. *
  157. * @param string $key The case-insensitive header name
  158. */
  159. public function remove($key)
  160. {
  161. parent::remove($this->normalizeKey($key));
  162. }
  163. /**
  164. * Normalize header name
  165. *
  166. * This method transforms header names into a
  167. * normalized form. This is how we enable case-insensitive
  168. * header names in the other methods in this class.
  169. *
  170. * @param string $key The case-insensitive header name
  171. *
  172. * @return string Normalized header name
  173. */
  174. public function normalizeKey($key)
  175. {
  176. $key = strtr(strtolower($key), '_', '-');
  177. if (strpos($key, 'http-') === 0) {
  178. $key = substr($key, 5);
  179. }
  180. return $key;
  181. }
  182. }