49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
// BencodedContent.php
|
|
// Created: 2022-02-10
|
|
// Updated: 2022-02-27
|
|
|
|
namespace Index\Http\Content;
|
|
|
|
use Stringable;
|
|
use Index\IO\Stream;
|
|
use Index\IO\FileStream;
|
|
use Index\Serialisation\Serialiser;
|
|
use Index\Serialisation\IBencodeSerialisable;
|
|
|
|
class BencodedContent implements Stringable, IHttpContent, IBencodeSerialisable {
|
|
private mixed $content;
|
|
|
|
public function __construct(mixed $content) {
|
|
$this->content = $content;
|
|
}
|
|
|
|
public function getContent(): mixed {
|
|
return $this->content;
|
|
}
|
|
|
|
public function bencodeSerialise(): mixed {
|
|
return $this->content;
|
|
}
|
|
|
|
public function encode(): string {
|
|
return Serialiser::bencode()->serialise($this->content);
|
|
}
|
|
|
|
public function __toString(): string {
|
|
return $this->encode();
|
|
}
|
|
|
|
public static function fromEncoded(Stream|string $encoded): BencodedContent {
|
|
return new BencodedContent(Serialiser::bencode()->deserialise($encoded));
|
|
}
|
|
|
|
public static function fromFile(string $path): BencodedContent {
|
|
return self::fromEncoded(FileStream::openRead($path));
|
|
}
|
|
|
|
public static function fromRequest(): BencodedContent {
|
|
return self::fromFile('php://input');
|
|
}
|
|
}
|