index/src/Http/HttpHeaders.php

57 lines
1.4 KiB
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
// HttpHeaders.php
// Created: 2022-02-08
// Updated: 2022-02-27
namespace Index\Http;
use RuntimeException;
class HttpHeaders {
private array $headers;
public function __construct(array $headers) {
$real = [];
foreach($headers as $header)
if($header instanceof HttpHeader)
$real[strtolower((string)$header->getName())] = $header;
$this->headers = $real;
}
public function hasHeader(string $name): bool {
return isset($this->headers[strtolower($name)]);
}
public function getHeaders(): array {
return array_values($this->headers);
}
public function getHeader(string $name): HttpHeader {
$name = strtolower($name);
if(!isset($this->headers[$name]))
throw new RuntimeException('No header with that name is present.');
return $this->headers[$name];
}
public function getHeaderLine(string $name): string {
if(!$this->hasHeader($name))
return '';
return (string)$this->getHeader($name);
}
public function getHeaderLines(string $name): array {
if(!$this->hasHeader($name))
return [];
return $this->getHeader($name)->getLines();
}
public function getHeaderFirstLine(string $name): string {
if(!$this->hasHeader($name))
return '';
return $this->getHeader($name)->getFirstLine();
}
}