]; } else { $this->headers[$key][] = $values; } } if ('cache-control' === $key) { $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key])); } } /** * Returns true if the HTTP header is defined. * * @param string $key The HTTP header * * @return bool true if the parameter exists, false otherwise */ public function has($key) { return \array_key_exists(strtr($key, self::UPPER, self::LOWER), $this->all()); } /** * Returns true if the given HTTP header contains the given value. * * @param string $key The HTTP header name * @param string $value The HTTP value * * @return bool true if the value is contained in the header, false otherwise */ public function contains($key, $value) { return \in_array($value, $this->all((string) $key)); } /** * Removes a header. * * @param string $key The HTTP header name */ public function remove($key) { $key = strtr($key, self::UPPER, self::LOWER); unset($this->headers[$key]); if ('cache-control' === $key) { $this->cacheControl = []; } } /** * Returns the HTTP header value converted to a date. * * @param string $key The parameter key * * @return \DateTimeInterface|null The parsed DateTime or the default value if the header does not exist * * @throws \RuntimeException When the HTTP header is not parseable */ 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; } /** * Adds a custom Cache-Control directive. * * @param string $key The Cache-Control directive name * @param bool|string $value The Cache-Control directive value */ public function addCacheControlDirective($key, $value = true) { $this->cacheControl[$key] = $value; $this->set('Cache-Control', $this->getCacheControlHeader()); } /** * Returns true if the Cache-Control directive is defined. * * @param string $key The Cache-Control directive * * @return bool true if the directive exists, false otherwise */ public function hasCacheControlDirective($key) { return \array_key_exists($key, $this->cacheControl); } /** * Returns a Cache-Control directive value by name. * * @param string $key The directive name * * @return bool|string|null The directive value if defined, null otherwise */ public function getCacheControlDirective($key) { return $this->cacheControl[$key] ?? null; } /** * Removes a Cache-Control directive. * * @param string $key The Cache-Control directive */ public function removeCacheControlDirective($key) { unset($this->cacheControl[$key]); $this->set('Cache-Control', $this->getCacheControlHeader()); } /** * Returns an iterator for headers. * * @return \ArrayIterator An \ArrayIterator instance */ #[\ReturnTypeWillChange] public function getIterator() { return new \ArrayIterator($this->headers); } /** * Returns the number of headers. * * @return int The number of headers */ #[\ReturnTypeWillChange] public function count() { return \count($this->headers); } protected function getCacheControlHeader() { ksort($this->cacheControl); return HeaderUtils::toString($this->cacheControl, ','); } /** * Parses a Cache-Control HTTP header. * * @param string $header The value of the Cache-Control HTTP header * * @return array An array representing the attribute values */ protected function parseCacheControl($header) { $parts = HeaderUtils::split($header, ',='); return HeaderUtils::combine($parts); } } ]; } else { $this->headers[$key][] = $values; } } if ('cache-control' === $key) { $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key])); } } /** * Returns true if the HTTP header is defined. * * @param string $key The HTTP header * * @return bool true if the parameter exists, false otherwise */ public function has($key) { return \array_key_exists(strtr($key, self::UPPER, self::LOWER), $this->all()); } /** * Returns true if the given HTTP header contains the given value. * * @param string $key The HTTP header name * @param string $value The HTTP value * * @return bool true if the value is contained in the header, false otherwise */ public function contains($key, $value) { return \in_array($value, $this->all((string) $key)); } /** * Removes a header. * * @param string $key The HTTP header name */ public function remove($key) { $key = strtr($key, self::UPPER, self::LOWER); unset($this->headers[$key]); if ('cache-control' === $key) { $this->cacheControl = []; } } /** * Returns the HTTP header value converted to a date. * * @param string $key The parameter key * * @return \DateTimeInterface|null The parsed DateTime or the default value if the header does not exist * * @throws \RuntimeException When the HTTP header is not parseable */ 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; } /** * Adds a custom Cache-Control directive. * * @param string $key The Cache-Control directive name * @param bool|string $value The Cache-Control directive value */ public function addCacheControlDirective($key, $value = true) { $this->cacheControl[$key] = $value; $this->set('Cache-Control', $this->getCacheControlHeader()); } /** * Returns true if the Cache-Control directive is defined. * * @param string $key The Cache-Control directive * * @return bool true if the directive exists, false otherwise */ public function hasCacheControlDirective($key) { return \array_key_exists($key, $this->cacheControl); } /** * Returns a Cache-Control directive value by name. * * @param string $key The directive name * * @return bool|string|null The directive value if defined, null otherwise */ public function getCacheControlDirective($key) { return $this->cacheControl[$key] ?? null; } /** * Removes a Cache-Control directive. * * @param string $key The Cache-Control directive */ public function removeCacheControlDirective($key) { unset($this->cacheControl[$key]); $this->set('Cache-Control', $this->getCacheControlHeader()); } /** * Returns an iterator for headers. * * @return \ArrayIterator An \ArrayIterator instance */ #[\ReturnTypeWillChange] public function getIterator() { return new \ArrayIterator($this->headers); } /** * Returns the number of headers. * * @return int The number of headers */ #[\ReturnTypeWillChange] public function count() { return \count($this->headers); } protected function getCacheControlHeader() { ksort($this->cacheControl); return HeaderUtils::toString($this->cacheControl, ','); } /** * Parses a Cache-Control HTTP header. * * @param string $header The value of the Cache-Control HTTP header * * @return array An array representing the attribute values */ protected function parseCacheControl($header) { $parts = HeaderUtils::split($header, ',='); return HeaderUtils::combine($parts); } }