Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation; Chris@0: Chris@0: /** Chris@0: * HeaderBag is a container for HTTP headers. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class HeaderBag implements \IteratorAggregate, \Countable Chris@0: { Chris@17: protected $headers = []; Chris@17: protected $cacheControl = []; Chris@0: Chris@0: /** Chris@0: * @param array $headers An array of HTTP headers Chris@0: */ Chris@17: public function __construct(array $headers = []) Chris@0: { Chris@0: foreach ($headers as $key => $values) { Chris@0: $this->set($key, $values); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the headers as a string. Chris@0: * Chris@0: * @return string The headers Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@14: if (!$headers = $this->all()) { Chris@0: return ''; Chris@0: } Chris@0: Chris@14: ksort($headers); Chris@14: $max = max(array_map('strlen', array_keys($headers))) + 1; Chris@0: $content = ''; Chris@14: foreach ($headers as $name => $values) { Chris@0: $name = implode('-', array_map('ucfirst', explode('-', $name))); Chris@0: foreach ($values as $value) { Chris@0: $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); Chris@0: } Chris@0: } Chris@0: Chris@0: return $content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the headers. Chris@0: * Chris@0: * @return array An array of headers Chris@0: */ Chris@0: public function all() Chris@0: { Chris@0: return $this->headers; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the parameter keys. Chris@0: * Chris@0: * @return array An array of parameter keys Chris@0: */ Chris@0: public function keys() Chris@0: { Chris@14: return array_keys($this->all()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Replaces the current HTTP headers by a new set. Chris@0: * Chris@0: * @param array $headers An array of HTTP headers Chris@0: */ Chris@17: public function replace(array $headers = []) Chris@0: { Chris@17: $this->headers = []; Chris@0: $this->add($headers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds new headers the current HTTP headers set. Chris@0: * Chris@0: * @param array $headers An array of HTTP headers Chris@0: */ Chris@0: public function add(array $headers) Chris@0: { Chris@0: foreach ($headers as $key => $values) { Chris@0: $this->set($key, $values); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a header value by name. Chris@0: * Chris@17: * @param string $key The header name Chris@17: * @param string|null $default The default value Chris@17: * @param bool $first Whether to return the first value or all header values Chris@0: * Chris@17: * @return string|string[]|null The first header value or default value if $first is true, an array of values otherwise Chris@0: */ Chris@0: public function get($key, $default = null, $first = true) Chris@0: { Chris@0: $key = str_replace('_', '-', strtolower($key)); Chris@14: $headers = $this->all(); Chris@0: Chris@18: if (!\array_key_exists($key, $headers)) { Chris@0: if (null === $default) { Chris@17: return $first ? null : []; Chris@0: } Chris@0: Chris@17: return $first ? $default : [$default]; Chris@0: } Chris@0: Chris@0: if ($first) { Chris@14: return \count($headers[$key]) ? $headers[$key][0] : $default; Chris@0: } Chris@0: Chris@14: return $headers[$key]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a header by name. Chris@0: * Chris@14: * @param string $key The key Chris@14: * @param string|string[] $values The value or an array of values Chris@14: * @param bool $replace Whether to replace the actual value or not (true by default) Chris@0: */ Chris@0: public function set($key, $values, $replace = true) Chris@0: { Chris@0: $key = str_replace('_', '-', strtolower($key)); Chris@0: Chris@14: if (\is_array($values)) { Chris@14: $values = array_values($values); Chris@0: Chris@14: if (true === $replace || !isset($this->headers[$key])) { Chris@14: $this->headers[$key] = $values; Chris@14: } else { Chris@14: $this->headers[$key] = array_merge($this->headers[$key], $values); Chris@14: } Chris@0: } else { Chris@14: if (true === $replace || !isset($this->headers[$key])) { Chris@17: $this->headers[$key] = [$values]; Chris@14: } else { Chris@14: $this->headers[$key][] = $values; Chris@14: } Chris@0: } Chris@0: Chris@0: if ('cache-control' === $key) { Chris@14: $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key])); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the HTTP header is defined. Chris@0: * Chris@0: * @param string $key The HTTP header Chris@0: * Chris@0: * @return bool true if the parameter exists, false otherwise Chris@0: */ Chris@0: public function has($key) Chris@0: { Chris@18: return \array_key_exists(str_replace('_', '-', strtolower($key)), $this->all()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the given HTTP header contains the given value. Chris@0: * Chris@0: * @param string $key The HTTP header name Chris@0: * @param string $value The HTTP value Chris@0: * Chris@0: * @return bool true if the value is contained in the header, false otherwise Chris@0: */ Chris@0: public function contains($key, $value) Chris@0: { Chris@17: return \in_array($value, $this->get($key, null, false)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes a header. Chris@0: * Chris@0: * @param string $key The HTTP header name Chris@0: */ Chris@0: public function remove($key) Chris@0: { Chris@0: $key = str_replace('_', '-', strtolower($key)); Chris@0: Chris@0: unset($this->headers[$key]); Chris@0: Chris@0: if ('cache-control' === $key) { Chris@17: $this->cacheControl = []; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the HTTP header value converted to a date. Chris@0: * Chris@0: * @param string $key The parameter key Chris@0: * @param \DateTime $default The default value Chris@0: * Chris@17: * @return \DateTime|null The parsed DateTime or the default value if the header does not exist Chris@0: * Chris@0: * @throws \RuntimeException When the HTTP header is not parseable Chris@0: */ Chris@0: public function getDate($key, \DateTime $default = null) Chris@0: { Chris@0: if (null === $value = $this->get($key)) { Chris@0: return $default; Chris@0: } Chris@0: Chris@0: if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) { Chris@0: throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value)); Chris@0: } Chris@0: Chris@0: return $date; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds a custom Cache-Control directive. Chris@0: * Chris@0: * @param string $key The Cache-Control directive name Chris@0: * @param mixed $value The Cache-Control directive value Chris@0: */ Chris@0: public function addCacheControlDirective($key, $value = true) Chris@0: { Chris@0: $this->cacheControl[$key] = $value; Chris@0: Chris@0: $this->set('Cache-Control', $this->getCacheControlHeader()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the Cache-Control directive is defined. Chris@0: * Chris@0: * @param string $key The Cache-Control directive Chris@0: * Chris@0: * @return bool true if the directive exists, false otherwise Chris@0: */ Chris@0: public function hasCacheControlDirective($key) Chris@0: { Chris@18: return \array_key_exists($key, $this->cacheControl); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a Cache-Control directive value by name. Chris@0: * Chris@0: * @param string $key The directive name Chris@0: * Chris@0: * @return mixed|null The directive value if defined, null otherwise Chris@0: */ Chris@0: public function getCacheControlDirective($key) Chris@0: { Chris@18: return \array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes a Cache-Control directive. Chris@0: * Chris@0: * @param string $key The Cache-Control directive Chris@0: */ Chris@0: public function removeCacheControlDirective($key) Chris@0: { Chris@0: unset($this->cacheControl[$key]); Chris@0: Chris@0: $this->set('Cache-Control', $this->getCacheControlHeader()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an iterator for headers. Chris@0: * Chris@0: * @return \ArrayIterator An \ArrayIterator instance Chris@0: */ Chris@0: public function getIterator() Chris@0: { Chris@0: return new \ArrayIterator($this->headers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the number of headers. Chris@0: * Chris@0: * @return int The number of headers Chris@0: */ Chris@0: public function count() Chris@0: { Chris@17: return \count($this->headers); Chris@0: } Chris@0: Chris@0: protected function getCacheControlHeader() Chris@0: { Chris@17: $parts = []; Chris@0: ksort($this->cacheControl); Chris@0: foreach ($this->cacheControl as $key => $value) { Chris@0: if (true === $value) { Chris@0: $parts[] = $key; Chris@0: } else { Chris@0: if (preg_match('#[^a-zA-Z0-9._-]#', $value)) { Chris@0: $value = '"'.$value.'"'; Chris@0: } Chris@0: Chris@0: $parts[] = "$key=$value"; Chris@0: } Chris@0: } Chris@0: Chris@0: return implode(', ', $parts); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a Cache-Control HTTP header. Chris@0: * Chris@0: * @param string $header The value of the Cache-Control HTTP header Chris@0: * Chris@0: * @return array An array representing the attribute values Chris@0: */ Chris@0: protected function parseCacheControl($header) Chris@0: { Chris@17: $cacheControl = []; Chris@0: preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER); Chris@0: foreach ($matches as $match) { Chris@0: $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true); Chris@0: } Chris@0: Chris@0: return $cacheControl; Chris@0: } Chris@0: }