62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
// StringContent.php
|
|
// Created: 2022-02-10
|
|
// Updated: 2024-08-01
|
|
|
|
namespace Index\Http\Content;
|
|
|
|
use Stringable;
|
|
|
|
/**
|
|
* Represents string body content for a HTTP message.
|
|
*/
|
|
class StringContent implements IHttpContent {
|
|
/**
|
|
* @param string $string String that represents this message body.
|
|
*/
|
|
public function __construct(
|
|
private string $string
|
|
) {}
|
|
|
|
/**
|
|
* Retrieves the underlying string.
|
|
*
|
|
* @return string Underlying string.
|
|
*/
|
|
public function getString(): string {
|
|
return $this->string;
|
|
}
|
|
|
|
public function __toString(): string {
|
|
return $this->string;
|
|
}
|
|
|
|
/**
|
|
* Creates an instance an existing object.
|
|
*
|
|
* @param Stringable|string $string Object to cast to a string.
|
|
* @return StringContent Instance representing the provided object.
|
|
*/
|
|
public static function fromObject(Stringable|string $string): StringContent {
|
|
return new StringContent((string)$string);
|
|
}
|
|
|
|
/**
|
|
* Creates an instance from a file.
|
|
*
|
|
* @param string $path Path to the file.
|
|
* @return StringContent Instance representing the provided path.
|
|
*/
|
|
public static function fromFile(string $path): StringContent {
|
|
return new StringContent(file_get_contents($path));
|
|
}
|
|
|
|
/**
|
|
* Creates an instance from the raw request body.
|
|
*
|
|
* @return StringContent Instance representing the request body.
|
|
*/
|
|
public static function fromRequest(): StringContent {
|
|
return self::fromFile('php://input');
|
|
}
|
|
}
|