71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
|
<?php
|
||
|
// HttpMessageBuilder.php
|
||
|
// Created: 2022-02-08
|
||
|
// Updated: 2022-02-27
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|