PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/HeaderBag.php

https://gitlab.com/oytunistrator/92five
PHP | 350 lines | 154 code | 41 blank | 155 comment | 14 complexity | 78f35fa946450583edc0ebfa182fbe60 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * HeaderBag is a container for HTTP headers.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class HeaderBag implements \IteratorAggregate, \Countable
  19. {
  20. protected $headers;
  21. protected $cacheControl;
  22. /**
  23. * Constructor.
  24. *
  25. * @param array $headers An array of HTTP headers
  26. *
  27. * @api
  28. */
  29. public function __construct(array $headers = array())
  30. {
  31. $this->cacheControl = array();
  32. $this->headers = array();
  33. foreach ($headers as $key => $values) {
  34. $this->set($key, $values);
  35. }
  36. }
  37. /**
  38. * Returns the headers as a string.
  39. *
  40. * @return string The headers
  41. */
  42. public function __toString()
  43. {
  44. if (!$this->headers) {
  45. return '';
  46. }
  47. $max = max(array_map('strlen', array_keys($this->headers))) + 1;
  48. $content = '';
  49. ksort($this->headers);
  50. foreach ($this->headers as $name => $values) {
  51. $name = implode('-', array_map('ucfirst', explode('-', $name)));
  52. foreach ($values as $value) {
  53. $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
  54. }
  55. }
  56. return $content;
  57. }
  58. /**
  59. * Returns the headers.
  60. *
  61. * @return array An array of headers
  62. *
  63. * @api
  64. */
  65. public function all()
  66. {
  67. return $this->headers;
  68. }
  69. /**
  70. * Returns the parameter keys.
  71. *
  72. * @return array An array of parameter keys
  73. *
  74. * @api
  75. */
  76. public function keys()
  77. {
  78. return array_keys($this->headers);
  79. }
  80. /**
  81. * Replaces the current HTTP headers by a new set.
  82. *
  83. * @param array $headers An array of HTTP headers
  84. *
  85. * @api
  86. */
  87. public function replace(array $headers = array())
  88. {
  89. $this->headers = array();
  90. $this->add($headers);
  91. }
  92. /**
  93. * Adds new headers the current HTTP headers set.
  94. *
  95. * @param array $headers An array of HTTP headers
  96. *
  97. * @api
  98. */
  99. public function add(array $headers)
  100. {
  101. foreach ($headers as $key => $values) {
  102. $this->set($key, $values);
  103. }
  104. }
  105. /**
  106. * Returns a header value by name.
  107. *
  108. * @param string $key The header name
  109. * @param mixed $default The default value
  110. * @param bool $first Whether to return the first value or all header values
  111. *
  112. * @return string|array The first header value if $first is true, an array of values otherwise
  113. *
  114. * @api
  115. */
  116. public function get($key, $default = null, $first = true)
  117. {
  118. $key = strtr(strtolower($key), '_', '-');
  119. if (!array_key_exists($key, $this->headers)) {
  120. if (null === $default) {
  121. return $first ? null : array();
  122. }
  123. return $first ? $default : array($default);
  124. }
  125. if ($first) {
  126. return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
  127. }
  128. return $this->headers[$key];
  129. }
  130. /**
  131. * Sets a header by name.
  132. *
  133. * @param string $key The key
  134. * @param string|array $values The value or an array of values
  135. * @param bool $replace Whether to replace the actual value or not (true by default)
  136. *
  137. * @api
  138. */
  139. public function set($key, $values, $replace = true)
  140. {
  141. $key = strtr(strtolower($key), '_', '-');
  142. $values = array_values((array) $values);
  143. if (true === $replace || !isset($this->headers[$key])) {
  144. $this->headers[$key] = $values;
  145. } else {
  146. $this->headers[$key] = array_merge($this->headers[$key], $values);
  147. }
  148. if ('cache-control' === $key) {
  149. $this->cacheControl = $this->parseCacheControl($values[0]);
  150. }
  151. }
  152. /**
  153. * Returns true if the HTTP header is defined.
  154. *
  155. * @param string $key The HTTP header
  156. *
  157. * @return bool true if the parameter exists, false otherwise
  158. *
  159. * @api
  160. */
  161. public function has($key)
  162. {
  163. return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
  164. }
  165. /**
  166. * Returns true if the given HTTP header contains the given value.
  167. *
  168. * @param string $key The HTTP header name
  169. * @param string $value The HTTP value
  170. *
  171. * @return bool true if the value is contained in the header, false otherwise
  172. *
  173. * @api
  174. */
  175. public function contains($key, $value)
  176. {
  177. return in_array($value, $this->get($key, null, false));
  178. }
  179. /**
  180. * Removes a header.
  181. *
  182. * @param string $key The HTTP header name
  183. *
  184. * @api
  185. */
  186. public function remove($key)
  187. {
  188. $key = strtr(strtolower($key), '_', '-');
  189. unset($this->headers[$key]);
  190. if ('cache-control' === $key) {
  191. $this->cacheControl = array();
  192. }
  193. }
  194. /**
  195. * Returns the HTTP header value converted to a date.
  196. *
  197. * @param string $key The parameter key
  198. * @param \DateTime $default The default value
  199. *
  200. * @return null|\DateTime The parsed DateTime or the default value if the header does not exist
  201. *
  202. * @throws \RuntimeException When the HTTP header is not parseable
  203. *
  204. * @api
  205. */
  206. public function getDate($key, \DateTime $default = null)
  207. {
  208. if (null === $value = $this->get($key)) {
  209. return $default;
  210. }
  211. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  212. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  213. }
  214. return $date;
  215. }
  216. /**
  217. * Adds a custom Cache-Control directive.
  218. *
  219. * @param string $key The Cache-Control directive name
  220. * @param mixed $value The Cache-Control directive value
  221. */
  222. public function addCacheControlDirective($key, $value = true)
  223. {
  224. $this->cacheControl[$key] = $value;
  225. $this->set('Cache-Control', $this->getCacheControlHeader());
  226. }
  227. /**
  228. * Returns true if the Cache-Control directive is defined.
  229. *
  230. * @param string $key The Cache-Control directive
  231. *
  232. * @return bool true if the directive exists, false otherwise
  233. */
  234. public function hasCacheControlDirective($key)
  235. {
  236. return array_key_exists($key, $this->cacheControl);
  237. }
  238. /**
  239. * Returns a Cache-Control directive value by name.
  240. *
  241. * @param string $key The directive name
  242. *
  243. * @return mixed|null The directive value if defined, null otherwise
  244. */
  245. public function getCacheControlDirective($key)
  246. {
  247. return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
  248. }
  249. /**
  250. * Removes a Cache-Control directive.
  251. *
  252. * @param string $key The Cache-Control directive
  253. */
  254. public function removeCacheControlDirective($key)
  255. {
  256. unset($this->cacheControl[$key]);
  257. $this->set('Cache-Control', $this->getCacheControlHeader());
  258. }
  259. /**
  260. * Returns an iterator for headers.
  261. *
  262. * @return \ArrayIterator An \ArrayIterator instance
  263. */
  264. public function getIterator()
  265. {
  266. return new \ArrayIterator($this->headers);
  267. }
  268. /**
  269. * Returns the number of headers.
  270. *
  271. * @return int The number of headers
  272. */
  273. public function count()
  274. {
  275. return count($this->headers);
  276. }
  277. protected function getCacheControlHeader()
  278. {
  279. $parts = array();
  280. ksort($this->cacheControl);
  281. foreach ($this->cacheControl as $key => $value) {
  282. if (true === $value) {
  283. $parts[] = $key;
  284. } else {
  285. if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
  286. $value = '"'.$value.'"';
  287. }
  288. $parts[] = "$key=$value";
  289. }
  290. }
  291. return implode(', ', $parts);
  292. }
  293. /**
  294. * Parses a Cache-Control HTTP header.
  295. *
  296. * @param string $header The value of the Cache-Control HTTP header
  297. *
  298. * @return array An array representing the attribute values
  299. */
  300. protected function parseCacheControl($header)
  301. {
  302. $cacheControl = array();
  303. preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
  304. foreach ($matches as $match) {
  305. $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
  306. }
  307. return $cacheControl;
  308. }
  309. }