HeaderBag.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. class HeaderBag implements \IteratorAggregate, \Countable
  17. {
  18. protected $headers = array();
  19. protected $cacheControl = array();
  20. /**
  21. * @param array $headers An array of HTTP headers
  22. */
  23. public function __construct(array $headers = array())
  24. {
  25. foreach ($headers as $key => $values) {
  26. $this->set($key, $values);
  27. }
  28. }
  29. /**
  30. * Returns the headers as a string.
  31. *
  32. * @return string The headers
  33. */
  34. public function __toString()
  35. {
  36. if (!$headers = $this->all()) {
  37. return '';
  38. }
  39. ksort($headers);
  40. $max = max(array_map('strlen', array_keys($headers))) + 1;
  41. $content = '';
  42. foreach ($headers as $name => $values) {
  43. $name = ucwords($name, '-');
  44. foreach ($values as $value) {
  45. $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
  46. }
  47. }
  48. return $content;
  49. }
  50. /**
  51. * Returns the headers.
  52. *
  53. * @return array An array of headers
  54. */
  55. public function all()
  56. {
  57. return $this->headers;
  58. }
  59. /**
  60. * Returns the parameter keys.
  61. *
  62. * @return array An array of parameter keys
  63. */
  64. public function keys()
  65. {
  66. return array_keys($this->all());
  67. }
  68. /**
  69. * Replaces the current HTTP headers by a new set.
  70. *
  71. * @param array $headers An array of HTTP headers
  72. */
  73. public function replace(array $headers = array())
  74. {
  75. $this->headers = array();
  76. $this->add($headers);
  77. }
  78. /**
  79. * Adds new headers the current HTTP headers set.
  80. *
  81. * @param array $headers An array of HTTP headers
  82. */
  83. public function add(array $headers)
  84. {
  85. foreach ($headers as $key => $values) {
  86. $this->set($key, $values);
  87. }
  88. }
  89. /**
  90. * Returns a header value by name.
  91. *
  92. * @param string $key The header name
  93. * @param string|string[] $default The default value
  94. * @param bool $first Whether to return the first value or all header values
  95. *
  96. * @return string|string[] The first header value or default value if $first is true, an array of values otherwise
  97. */
  98. public function get($key, $default = null, $first = true)
  99. {
  100. $key = str_replace('_', '-', strtolower($key));
  101. $headers = $this->all();
  102. if (!array_key_exists($key, $headers)) {
  103. if (null === $default) {
  104. return $first ? null : array();
  105. }
  106. return $first ? $default : array($default);
  107. }
  108. if ($first) {
  109. return \count($headers[$key]) ? $headers[$key][0] : $default;
  110. }
  111. return $headers[$key];
  112. }
  113. /**
  114. * Sets a header by name.
  115. *
  116. * @param string $key The key
  117. * @param string|string[] $values The value or an array of values
  118. * @param bool $replace Whether to replace the actual value or not (true by default)
  119. */
  120. public function set($key, $values, $replace = true)
  121. {
  122. $key = str_replace('_', '-', strtolower($key));
  123. if (\is_array($values)) {
  124. $values = array_values($values);
  125. if (true === $replace || !isset($this->headers[$key])) {
  126. $this->headers[$key] = $values;
  127. } else {
  128. $this->headers[$key] = array_merge($this->headers[$key], $values);
  129. }
  130. } else {
  131. if (true === $replace || !isset($this->headers[$key])) {
  132. $this->headers[$key] = array($values);
  133. } else {
  134. $this->headers[$key][] = $values;
  135. }
  136. }
  137. if ('cache-control' === $key) {
  138. $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key]));
  139. }
  140. }
  141. /**
  142. * Returns true if the HTTP header is defined.
  143. *
  144. * @param string $key The HTTP header
  145. *
  146. * @return bool true if the parameter exists, false otherwise
  147. */
  148. public function has($key)
  149. {
  150. return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all());
  151. }
  152. /**
  153. * Returns true if the given HTTP header contains the given value.
  154. *
  155. * @param string $key The HTTP header name
  156. * @param string $value The HTTP value
  157. *
  158. * @return bool true if the value is contained in the header, false otherwise
  159. */
  160. public function contains($key, $value)
  161. {
  162. return in_array($value, $this->get($key, null, false));
  163. }
  164. /**
  165. * Removes a header.
  166. *
  167. * @param string $key The HTTP header name
  168. */
  169. public function remove($key)
  170. {
  171. $key = str_replace('_', '-', strtolower($key));
  172. unset($this->headers[$key]);
  173. if ('cache-control' === $key) {
  174. $this->cacheControl = array();
  175. }
  176. }
  177. /**
  178. * Returns the HTTP header value converted to a date.
  179. *
  180. * @param string $key The parameter key
  181. * @param \DateTime $default The default value
  182. *
  183. * @return null|\DateTime The parsed DateTime or the default value if the header does not exist
  184. *
  185. * @throws \RuntimeException When the HTTP header is not parseable
  186. */
  187. public function getDate($key, \DateTime $default = null)
  188. {
  189. if (null === $value = $this->get($key)) {
  190. return $default;
  191. }
  192. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  193. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  194. }
  195. return $date;
  196. }
  197. /**
  198. * Adds a custom Cache-Control directive.
  199. *
  200. * @param string $key The Cache-Control directive name
  201. * @param mixed $value The Cache-Control directive value
  202. */
  203. public function addCacheControlDirective($key, $value = true)
  204. {
  205. $this->cacheControl[$key] = $value;
  206. $this->set('Cache-Control', $this->getCacheControlHeader());
  207. }
  208. /**
  209. * Returns true if the Cache-Control directive is defined.
  210. *
  211. * @param string $key The Cache-Control directive
  212. *
  213. * @return bool true if the directive exists, false otherwise
  214. */
  215. public function hasCacheControlDirective($key)
  216. {
  217. return array_key_exists($key, $this->cacheControl);
  218. }
  219. /**
  220. * Returns a Cache-Control directive value by name.
  221. *
  222. * @param string $key The directive name
  223. *
  224. * @return mixed|null The directive value if defined, null otherwise
  225. */
  226. public function getCacheControlDirective($key)
  227. {
  228. return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
  229. }
  230. /**
  231. * Removes a Cache-Control directive.
  232. *
  233. * @param string $key The Cache-Control directive
  234. */
  235. public function removeCacheControlDirective($key)
  236. {
  237. unset($this->cacheControl[$key]);
  238. $this->set('Cache-Control', $this->getCacheControlHeader());
  239. }
  240. /**
  241. * Returns an iterator for headers.
  242. *
  243. * @return \ArrayIterator An \ArrayIterator instance
  244. */
  245. public function getIterator()
  246. {
  247. return new \ArrayIterator($this->headers);
  248. }
  249. /**
  250. * Returns the number of headers.
  251. *
  252. * @return int The number of headers
  253. */
  254. public function count()
  255. {
  256. return count($this->headers);
  257. }
  258. protected function getCacheControlHeader()
  259. {
  260. ksort($this->cacheControl);
  261. return HeaderUtils::toString($this->cacheControl, ',');
  262. }
  263. /**
  264. * Parses a Cache-Control HTTP header.
  265. *
  266. * @param string $header The value of the Cache-Control HTTP header
  267. *
  268. * @return array An array representing the attribute values
  269. */
  270. protected function parseCacheControl($header)
  271. {
  272. $parts = HeaderUtils::split($header, ',=');
  273. return HeaderUtils::combine($parts);
  274. }
  275. }