Final Class Yiisoft\Test\Support\HttpMessage\StringStream
| Inheritance | Yiisoft\Test\Support\HttpMessage\StringStream |
|---|---|
| Implements | Psr\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 |
Public Methods
Method Details
| 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)
);
}
}
| public detach( ): mixed |
public function detach()
{
$this->detached = true;
$this->close();
return null;
}
| 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 = 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;
}
Returns the current position of the stream pointer.
| public getPosition( ): integer |
public function getPosition(): int
{
return $this->position;
}
Checks whether the stream has been closed.
| public isClosed( ): boolean |
public function isClosed(): bool
{
return $this->closed;
}
Checks whether the stream has been detached.
| public isDetached( ): boolean |
public function isDetached(): bool
{
return $this->detached;
}
| public isReadable( ): boolean |
public function isReadable(): bool
{
return $this->readable && !$this->closed;
}
| public isSeekable( ): boolean |
public function isSeekable(): bool
{
return $this->seekable && !$this->closed;
}
| public isWritable( ): boolean |
public function isWritable(): bool
{
return $this->writable && !$this->closed;
}
| 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;
}
| 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;
}
| public tell( ): integer |
public function tell(): int
{
if ($this->closed) {
throw new RuntimeException('Stream is closed.');
}
return $this->position;
}
| 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;
}
Signup or Login in order to comment.