Final Class Yiisoft\DataResponse\DataStream\StringStream
| Inheritance | Yiisoft\DataResponse\DataStream\StringStream |
|---|---|
| Implements | Psr\Http\Message\StreamInterface |
A read-only stream implementation for string content.
Public Methods
Method Details
| public __construct( string $content ): mixed | ||
| $content | string | |
public function __construct(
private readonly string $content,
) {}
| public eof( ): boolean |
public function eof(): bool
{
return $this->closed || $this->position >= $this->getContentSize();
}
| public getContents( ): string |
public function getContents(): string
{
return $this->read(
$this->getContentSize() - $this->position,
);
}
| public getMetadata( string|null $key = null ): mixed | ||
| $key | string|null | |
public function getMetadata(?string $key = null)
{
if ($this->closed) {
return $key === null ? [] : null;
}
$metadata = [
'eof' => $this->eof(),
'seekable' => $this->isSeekable(),
];
if ($key === null) {
return $metadata;
}
return $metadata[$key] ?? null;
}
| public read( integer $length ): string | ||
| $length | integer | |
public function read(int $length): string
{
if ($this->closed) {
throw new RuntimeException('Stream is closed.');
}
if ($length < 0) {
throw new RuntimeException('Length must be non-negative.');
}
if ($this->position >= $this->getContentSize()) {
return '';
}
$data = substr($this->content, $this->position, $length);
$this->position += strlen($data);
return $data;
}
| public seek( integer $offset, integer $whence = SEEK_SET ): void | ||
| $offset | integer | |
| $whence | integer | |
public function seek(int $offset, int $whence = SEEK_SET): void
{
if ($this->closed) {
throw new RuntimeException('Stream is closed.');
}
$size = $this->getContentSize();
$newPosition = match ($whence) {
SEEK_SET => $offset,
SEEK_CUR => $this->position + $offset,
SEEK_END => $size + $offset,
default => throw new RuntimeException('Invalid whence value.'),
};
if ($newPosition < 0 || $newPosition > $size) {
throw new RuntimeException('Invalid seek position.');
}
$this->position = $newPosition;
}
| public tell( ): integer |
public function tell(): int
{
if ($this->closed) {
throw new RuntimeException('Stream is closed.');
}
return $this->position;
}
Signup or Login in order to comment.