4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
15 * HeaderBag is a container for HTTP headers.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class HeaderBag implements \IteratorAggregate, \Countable
21 protected $headers = array();
22 protected $cacheControl = array();
27 * @param array $headers An array of HTTP headers
29 public function __construct(array $headers = array())
31 foreach ($headers as $key => $values) {
32 $this->set($key, $values);
37 * Returns the headers as a string.
39 * @return string The headers
41 public function __toString()
43 if (!$this->headers) {
47 $max = max(array_map('strlen', array_keys($this->headers))) + 1;
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);
61 * Returns the headers.
63 * @return array An array of headers
67 return $this->headers;
71 * Returns the parameter keys.
73 * @return array An array of parameter keys
75 public function keys()
77 return array_keys($this->headers);
81 * Replaces the current HTTP headers by a new set.
83 * @param array $headers An array of HTTP headers
85 public function replace(array $headers = array())
87 $this->headers = array();
92 * Adds new headers the current HTTP headers set.
94 * @param array $headers An array of HTTP headers
96 public function add(array $headers)
98 foreach ($headers as $key => $values) {
99 $this->set($key, $values);
104 * Returns a header value by name.
106 * @param string $key The header name
107 * @param mixed $default The default value
108 * @param bool $first Whether to return the first value or all header values
110 * @return string|array The first header value if $first is true, an array of values otherwise
112 public function get($key, $default = null, $first = true)
114 $key = str_replace('_', '-', strtolower($key));
116 if (!array_key_exists($key, $this->headers)) {
117 if (null === $default) {
118 return $first ? null : array();
121 return $first ? $default : array($default);
125 return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
128 return $this->headers[$key];
132 * Sets a header by name.
134 * @param string $key The key
135 * @param string|array $values The value or an array of values
136 * @param bool $replace Whether to replace the actual value or not (true by default)
138 public function set($key, $values, $replace = true)
140 $key = str_replace('_', '-', strtolower($key));
142 $values = array_values((array) $values);
144 if (true === $replace || !isset($this->headers[$key])) {
145 $this->headers[$key] = $values;
147 $this->headers[$key] = array_merge($this->headers[$key], $values);
150 if ('cache-control' === $key) {
151 $this->cacheControl = $this->parseCacheControl($values[0]);
156 * Returns true if the HTTP header is defined.
158 * @param string $key The HTTP header
160 * @return bool true if the parameter exists, false otherwise
162 public function has($key)
164 return array_key_exists(str_replace('_', '-', strtolower($key)), $this->headers);
168 * Returns true if the given HTTP header contains the given value.
170 * @param string $key The HTTP header name
171 * @param string $value The HTTP value
173 * @return bool true if the value is contained in the header, false otherwise
175 public function contains($key, $value)
177 return in_array($value, $this->get($key, null, false));
183 * @param string $key The HTTP header name
185 public function remove($key)
187 $key = str_replace('_', '-', strtolower($key));
189 unset($this->headers[$key]);
191 if ('cache-control' === $key) {
192 $this->cacheControl = array();
197 * Returns the HTTP header value converted to a date.
199 * @param string $key The parameter key
200 * @param \DateTime $default The default value
202 * @return null|\DateTime The parsed DateTime or the default value if the header does not exist
204 * @throws \RuntimeException When the HTTP header is not parseable
206 public function getDate($key, \DateTime $default = null)
208 if (null === $value = $this->get($key)) {
212 if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
213 throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
220 * Adds a custom Cache-Control directive.
222 * @param string $key The Cache-Control directive name
223 * @param mixed $value The Cache-Control directive value
225 public function addCacheControlDirective($key, $value = true)
227 $this->cacheControl[$key] = $value;
229 $this->set('Cache-Control', $this->getCacheControlHeader());
233 * Returns true if the Cache-Control directive is defined.
235 * @param string $key The Cache-Control directive
237 * @return bool true if the directive exists, false otherwise
239 public function hasCacheControlDirective($key)
241 return array_key_exists($key, $this->cacheControl);
245 * Returns a Cache-Control directive value by name.
247 * @param string $key The directive name
249 * @return mixed|null The directive value if defined, null otherwise
251 public function getCacheControlDirective($key)
253 return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
257 * Removes a Cache-Control directive.
259 * @param string $key The Cache-Control directive
261 public function removeCacheControlDirective($key)
263 unset($this->cacheControl[$key]);
265 $this->set('Cache-Control', $this->getCacheControlHeader());
269 * Returns an iterator for headers.
271 * @return \ArrayIterator An \ArrayIterator instance
273 public function getIterator()
275 return new \ArrayIterator($this->headers);
279 * Returns the number of headers.
281 * @return int The number of headers
283 public function count()
285 return count($this->headers);
288 protected function getCacheControlHeader()
291 ksort($this->cacheControl);
292 foreach ($this->cacheControl as $key => $value) {
293 if (true === $value) {
296 if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
297 $value = '"'.$value.'"';
300 $parts[] = "$key=$value";
304 return implode(', ', $parts);
308 * Parses a Cache-Control HTTP header.
310 * @param string $header The value of the Cache-Control HTTP header
312 * @return array An array representing the attribute values
314 protected function parseCacheControl($header)
316 $cacheControl = array();
317 preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
318 foreach ($matches as $match) {
319 $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
322 return $cacheControl;