123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace Symfony\Component\HttpFoundation;
- class HeaderBag implements \IteratorAggregate, \Countable
- {
- protected $headers = [];
- protected $cacheControl = [];
-
- public function __construct(array $headers = [])
- {
- foreach ($headers as $key => $values) {
- $this->set($key, $values);
- }
- }
-
- public function __toString()
- {
- if (!$headers = $this->all()) {
- return '';
- }
- ksort($headers);
- $max = max(array_map('strlen', array_keys($headers))) + 1;
- $content = '';
- foreach ($headers as $name => $values) {
- $name = implode('-', array_map('ucfirst', explode('-', $name)));
- foreach ($values as $value) {
- $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
- }
- }
- return $content;
- }
-
- public function all()
- {
- return $this->headers;
- }
-
- public function keys()
- {
- return array_keys($this->all());
- }
-
- public function replace(array $headers = [])
- {
- $this->headers = [];
- $this->add($headers);
- }
-
- public function add(array $headers)
- {
- foreach ($headers as $key => $values) {
- $this->set($key, $values);
- }
- }
-
- public function get($key, $default = null, $first = true)
- {
- $key = str_replace('_', '-', strtolower($key));
- $headers = $this->all();
- if (!\array_key_exists($key, $headers)) {
- if (null === $default) {
- return $first ? null : [];
- }
- return $first ? $default : [$default];
- }
- if ($first) {
- return \count($headers[$key]) ? $headers[$key][0] : $default;
- }
- return $headers[$key];
- }
-
- public function set($key, $values, $replace = true)
- {
- $key = str_replace('_', '-', strtolower($key));
- if (\is_array($values)) {
- $values = array_values($values);
- if (true === $replace || !isset($this->headers[$key])) {
- $this->headers[$key] = $values;
- } else {
- $this->headers[$key] = array_merge($this->headers[$key], $values);
- }
- } else {
- if (true === $replace || !isset($this->headers[$key])) {
- $this->headers[$key] = [$values];
- } else {
- $this->headers[$key][] = $values;
- }
- }
- if ('cache-control' === $key) {
- $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key]));
- }
- }
-
- public function has($key)
- {
- return \array_key_exists(str_replace('_', '-', strtolower($key)), $this->all());
- }
-
- public function contains($key, $value)
- {
- return \in_array($value, $this->get($key, null, false));
- }
-
- public function remove($key)
- {
- $key = str_replace('_', '-', strtolower($key));
- unset($this->headers[$key]);
- if ('cache-control' === $key) {
- $this->cacheControl = [];
- }
- }
-
- public function getDate($key, \DateTime $default = null)
- {
- if (null === $value = $this->get($key)) {
- return $default;
- }
- if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
- throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
- }
- return $date;
- }
-
- public function addCacheControlDirective($key, $value = true)
- {
- $this->cacheControl[$key] = $value;
- $this->set('Cache-Control', $this->getCacheControlHeader());
- }
-
- public function hasCacheControlDirective($key)
- {
- return \array_key_exists($key, $this->cacheControl);
- }
-
- public function getCacheControlDirective($key)
- {
- return \array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
- }
-
- public function removeCacheControlDirective($key)
- {
- unset($this->cacheControl[$key]);
- $this->set('Cache-Control', $this->getCacheControlHeader());
- }
-
- public function getIterator()
- {
- return new \ArrayIterator($this->headers);
- }
-
- public function count()
- {
- return \count($this->headers);
- }
- protected function getCacheControlHeader()
- {
- $parts = [];
- ksort($this->cacheControl);
- foreach ($this->cacheControl as $key => $value) {
- if (true === $value) {
- $parts[] = $key;
- } else {
- if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
- $value = '"'.$value.'"';
- }
- $parts[] = "$key=$value";
- }
- }
- return implode(', ', $parts);
- }
-
- protected function parseCacheControl($header)
- {
- $cacheControl = [];
- preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
- foreach ($matches as $match) {
- $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
- }
- return $cacheControl;
- }
- }
|