0 follower

Final Class Yiisoft\DataResponse\DataStream\StringStream

InheritanceYiisoft\DataResponse\DataStream\StringStream
ImplementsPsr\Http\Message\StreamInterface

A read-only stream implementation for string content.

Method Details

Hide inherited methods

__construct() public method

public __construct( string $content ): mixed
$content string

                public function __construct(
    private readonly string $content,
) {}

            
__toString() public method

public __toString( ): string

                public function __toString(): string
{
    return $this->content;
}

            
close() public method

public close( ): void

                public function close(): void
{
    $this->closed = true;
}

            
detach() public method

public detach( ): mixed

                public function detach()
{
    $this->close();
    return null;
}

            
eof() public method

public eof( ): boolean

                public function eof(): bool
{
    return $this->closed || $this->position >= $this->getContentSize();
}

            
getContents() public method

public getContents( ): string

                public function getContents(): string
{
    return $this->read(
        $this->getContentSize() - $this->position,
    );
}

            
getMetadata() public method

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;
}

            
getSize() public method

public getSize( ): integer

                public function getSize(): int
{
    return $this->getContentSize();
}

            
isReadable() public method

public isReadable( ): boolean

                public function isReadable(): bool
{
    return !$this->closed;
}

            
isSeekable() public method

public isSeekable( ): boolean

                public function isSeekable(): bool
{
    return !$this->closed;
}

            
isWritable() public method

public isWritable( ): boolean

                public function isWritable(): bool
{
    return false;
}

            
read() public method

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;
}

            
rewind() public method

public rewind( ): void

                public function rewind(): void
{
    $this->seek(0);
}

            
seek() public method

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;
}

            
tell() public method

public tell( ): integer

                public function tell(): int
{
    if ($this->closed) {
        throw new RuntimeException('Stream is closed.');
    }
    return $this->position;
}

            
write() public method

public write( string $string ): integer
$string string

                public function write(string $string): int
{
    throw new RuntimeException('Stream is not writable.');
}