Stream
in package
implements
StreamInterface
Interfaces, Classes and Traits
- StreamInterface
Table of Contents
- READABLE = ['w', 'w+', 'a', 'a+', 'r', 'r+']
- WRITABLE = ['w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+']
- List of modes that are either readable or writable, or both.
- $handle : resource|null
- The handle used to manipulate the stream. This class basically is a PSR7 conforming wrapper around this stream.
- $readable : bool
- $seekable : bool
- $writable : bool
- __construct() : mixed
- __toString() : string
- This is a convenience method to allow the printing of streams directly to the output, making it more convenient.
- close() : void
- Closes the stream. After this method has been invoked, the stream will no longer be usable.
- detach() : resource|null
- Detaches the stream from the underlying stream. Please note that this does not free the underlying resource.
- eof() : bool
- Returns true if we hit the end of the stream or whether the stream can continue to be read from.
- fromString() : StreamInterface
- getContents() : string
- Reads the remaining data from the stream into a string and returns it.
- getMetadata() : array<string|int, mixed>|mixed|null
- Get stream metadata as an associative array or retrieve a specific key.
- getSize() : int|null
- Returns the size (in bytes) of the stream. If the data is not available, the method returns a null value.
- isReadable() : bool
- If the stream can be read from, this method will return true.
- isSeekable() : bool
- Returns true if the stream allows being seeked through. Detached streams are implied to not be seekable (since they do not exist)
- isWritable() : bool
- If the stream can be written to, this method will return true.
- read() : string
- Read data from the stream. Please note that this will read from the position of the seek cursor onward.
- rewind() : void
- Seek to the beginning of the stream.
- seek() : void
- Seek to a position in the stream.
- tell() : int
- Tells the current position of the seeking cursor on the stream. If the stream is not seekable this will fail with a runtime exception.
- write() : int
- Write data to the stream.
Constants
READABLE
public
mixed
READABLE
= ['w', 'w+', 'a', 'a+', 'r', 'r+']
WRITABLE
List of modes that are either readable or writable, or both.
public
mixed
WRITABLE
= ['w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+']
Properties
$handle
The handle used to manipulate the stream. This class basically is a PSR7 conforming wrapper around this stream.
private
resource|null
$handle
$readable
private
bool
$readable
$seekable
private
bool
$seekable
$writable
private
bool
$writable
Methods
__construct()
public
__construct(resource $handle, bool $seekable, bool $readable, bool $writable) : mixed
Parameters
- $handle : resource
- $seekable : bool
- $readable : bool
- $writable : bool
Return values
mixed —__toString()
This is a convenience method to allow the printing of streams directly to the output, making it more convenient.
public
__toString() : string
Tags
Return values
string —close()
Closes the stream. After this method has been invoked, the stream will no longer be usable.
public
close() : void
Return values
void —detach()
Detaches the stream from the underlying stream. Please note that this does not free the underlying resource.
public
detach() : resource|null
Return values
resource|null —eof()
Returns true if we hit the end of the stream or whether the stream can continue to be read from.
public
eof() : bool
If the stream is detached, this method returns true. Implying that the stream can not be read from any further.
Return values
bool —fromString()
public
static fromString(string|StreamInterface $str) : StreamInterface
Parameters
- $str : string|StreamInterface
Return values
StreamInterface —getContents()
Reads the remaining data from the stream into a string and returns it.
public
getContents() : string
Return values
string —getMetadata()
Get stream metadata as an associative array or retrieve a specific key.
public
getMetadata([string $key = null ]) : array<string|int, mixed>|mixed|null
The keys returned are identical to the keys returned from PHP's stream_get_meta_data() function.
Parameters
- $key : string = null
-
Specific metadata to retrieve.
Tags
Return values
array<string|int, mixed>|mixed|null —Returns an associative array if no key is provided. Returns a specific key value if a key is provided and the value is found, or null if the key is not found.
getSize()
Returns the size (in bytes) of the stream. If the data is not available, the method returns a null value.
public
getSize() : int|null
Return values
int|null —isReadable()
If the stream can be read from, this method will return true.
public
isReadable() : bool
Return values
bool —isSeekable()
Returns true if the stream allows being seeked through. Detached streams are implied to not be seekable (since they do not exist)
public
isSeekable() : bool
Return values
bool —isWritable()
If the stream can be written to, this method will return true.
public
isWritable() : bool
Return values
bool —read()
Read data from the stream. Please note that this will read from the position of the seek cursor onward.
public
read(int $length) : string
Parameters
- $length : int
-
Read up to $length bytes from the object and return them. Fewer than $length bytes may be returned if underlying stream call returns fewer bytes.
Tags
Return values
string —Returns the data read from the stream, or an empty string if no bytes are available.
rewind()
Seek to the beginning of the stream.
public
rewind() : void
If the stream is not seekable, this method will raise an exception; otherwise, it will perform a seek(0).
Tags
Return values
void —seek()
Seek to a position in the stream.
public
seek(int $offset[, int $whence = SEEK_SET ]) : void
Parameters
- $offset : int
-
Stream offset
- $whence : int = SEEK_SET
-
Specifies how the cursor position will be calculated based on the seek offset. Valid values are identical to the built-in PHP $whence values for
fseek()
. SEEK_SET: Set position equal to offset bytes SEEK_CUR: Set position to current location plus offset SEEK_END: Set position to end-of-stream plus offset.
Tags
Return values
void —tell()
Tells the current position of the seeking cursor on the stream. If the stream is not seekable this will fail with a runtime exception.
public
tell() : int
Tags
Return values
int —Position of the file pointer
write()
Write data to the stream.
public
write(string $string) : int
Parameters
- $string : string
-
The string that is to be written.
Tags
Return values
int —Returns the number of bytes written to the stream.