index/src/Http/HttpMessageBuilder.php

72 lines
1.8 KiB
PHP

<?php
// HttpMessageBuilder.php
// Created: 2022-02-08
// Updated: 2024-03-28
namespace Index\Http;
use Index\Version;
use Index\IO\Stream;
use Index\Http\Content\IHttpContent;
use Index\Http\Content\StreamContent;
use Index\Http\Content\StringContent;
class HttpMessageBuilder {
private ?Version $version = null;
private HttpHeadersBuilder $headers;
private ?IHttpContent $content = null;
public function __construct() {
$this->headers = new HttpHeadersBuilder;
}
protected function getHttpVersion(): Version {
return $this->version ?? new Version(1, 1);
}
public function setHttpVersion(Version $version): void {
$this->version = $version;
}
protected function getHeaders(): HttpHeaders {
return $this->headers->toHeaders();
}
public function getHeadersBuilder(): HttpHeadersBuilder {
return $this->headers;
}
public function addHeader(string $name, mixed $value): void {
$this->headers->addHeader($name, $value);
}
public function setHeader(string $name, mixed $value): void {
$this->headers->setHeader($name, $value);
}
public function removeHeader(string $name): void {
$this->headers->removeHeader($name);
}
public function hasHeader(string $name): bool {
return $this->headers->hasHeader($name);
}
protected function getContent(): ?IHttpContent {
return $this->content;
}
/** @phpstan-impure */
public function hasContent(): bool {
return $this->content !== null;
}
public function setContent(IHttpContent|Stream|string|null $content): void {
if($content instanceof Stream)
$content = new StreamContent($content);
elseif(is_string($content))
$content = new StringContent($content);
$this->content = $content;
}
}