0 follower

Final Class Yiisoft\Test\Support\HttpMessage\StringStream

InheritanceYiisoft\Test\Support\HttpMessage\StringStream
ImplementsPsr\Http\Message\StreamInterface

A test-specific implementation of PSR-7 stream.

Allows creating stream instances with configurable behavior for testing HTTP message handling.

Psalm Types

Name Value
MetadataClosure callable

Method Details

Hide inherited methods

__construct() public method

public __construct( string $content '', integer $position 0, boolean $readable true, boolean $writable true, boolean $seekable true, array|Closure|null $metadata null ): mixed
$content string

Initial stream content.

$position integer

Initial position of the stream pointer.

$readable boolean

Whether the stream is readable.

$writable boolean

Whether the stream is writable.

$seekable boolean

Whether the stream is seekable.

$metadata array|Closure|null

Custom metadata as an array or a closure that receives the stream instance and returns an array.

                public function __construct(
    private string $content = '',
    private int $position = 0,
    private bool $readable = true,
    private bool $writable = true,
    private bool $seekable = true,
    private Closure|array|null $metadata = null,
) {
    $size = strlen($this->content);
    if ($this->position < 0 || $this->position > $size) {
        throw new LogicException(
            sprintf('Position %d is out of valid range [0, %d].', $this->position, $size)
        );
    }
}

            
__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->detached = true;
    $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 = match (true) {
        is_array($this->metadata) => $this->metadata,
        $this->metadata instanceof Closure => ($this->metadata)($this),
        default => [
            'eof' => $this->eof(),
            'seekable' => $this->isSeekable(),
        ],
    };
    if ($key === null) {
        return $metadata;
    }
    return $metadata[$key] ?? null;
}

            
getPosition() public method

Returns the current position of the stream pointer.

public getPosition( ): integer

                public function getPosition(): int
{
    return $this->position;
}

            
getSize() public method

public getSize( ): integer|null

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

            
isClosed() public method

Checks whether the stream has been closed.

public isClosed( ): boolean

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

            
isDetached() public method

Checks whether the stream has been detached.

public isDetached( ): boolean

                public function isDetached(): bool
{
    return $this->detached;
}

            
isReadable() public method

public isReadable( ): boolean

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

            
isSeekable() public method

public isSeekable( ): boolean

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

            
isWritable() public method

public isWritable( ): boolean

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

            
read() public method

public read( integer $length ): string
$length integer

                public function read(int $length): string
{
    if (!$this->readable) {
        throw new RuntimeException('Stream is not readable.');
    }
    if ($this->closed) {
        throw new RuntimeException('Stream is closed.');
    }
    if ($length < 0) {
        throw new RuntimeException('Length cannot be negative.');
    }
    if ($this->position >= $this->getContentSize()) {
        return '';
    }
    $result = substr($this->content, $this->position, $length);
    $this->position += strlen($result);
    return $result;
}

            
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->seekable) {
        throw new RuntimeException('Stream is not seekable.');
    }
    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
{
    if (!$this->writable) {
        throw new RuntimeException('Stream is not writable.');
    }
    if ($this->closed) {
        throw new RuntimeException('Stream is closed.');
    }
    $size = strlen($string);
    $this->content = substr($this->content, 0, $this->position)
        . $string
        . substr($this->content, $this->position + $size);
    $this->position = min($this->position + $size, $this->getContentSize());
    return $size;
}