# Class HttpSoft\ServerRequest\PhpInputStream

Class, that implements Psr\Http\Message\StreamInterface for read-only the stream (by default: php://input) or file.

Source code on GitHub.

use HttpSoft\ServerRequest\PhpInputStream;

$stream = new PhpInputStream();

$stream->getMetadata('uri'); // 'php://input'
$stream->getMetadata('mode'); // 'rb'
$stream->getContents(); // ''

$stream->isReadable(); // true
$stream->isSeekable(); // true
$stream->isWritable(); // false

$stream->close();
$stream->isReadable(); // false
$stream->isSeekable(); // false
$stream->isWritable(); // false

// Create instance from existing file.

$stream = new PhpInputStream('path/to/file');

$stream->getMetadata('uri'); // 'path/to/file'
$stream->getMetadata('mode'); // 'r'

$stream->read(5); // 'File '
$stream->getContents(); // 'content'
$stream->__toString(); // 'File content'
// equivalently to:
(string) $stream; // echo $stream;

// Create instance from existing resource.

$resource = fopen('path/to/file', 'r');
$stream = new PhpInputStream($resource);

$stream->getMetadata('uri'); // 'path/to/file'
$stream->getMetadata('mode'); // 'r'

$stream->read(5); // 'File '
$stream->getContents(); // 'content'
$stream->__toString(); // 'File content'
// equivalently to:
(string) $stream; // echo $stream;

# Public methods

See the original detailed description of the methods in the Psr\Http\Message\StreamInterface.

/**
 * @param string|resource $stream
 * @param string $mode
 */
public function __construct($stream = 'php://temp', string $mode = 'r');

The methods that have been modified for this class, are shown below. All other methods with descriptions are in the used trait (HttpSoft\Message\StreamTrait).

# isWritable

Always returns false, since the stream is read-only.

public function isWritable(): bool;

# read

Reads and returns data from the stream, adding it to the cache for subsequent output when repeated calls getContents() method.

/**
 * @param int $length
 * @return string
 */
public function read($length): string;

# getContents

Returns the remaining contents in a string, repeated calls will return the entire string, from beginning to end.

public function getContents(): string;