3 namespace Psr\Http\Message;
6 * Describes a data stream.
8 * Typically, an instance will wrap a PHP stream; this interface provides
9 * a wrapper around the most common operations, including serialization of
10 * the entire stream to a string.
12 interface StreamInterface
15 * Reads all data from the stream into a string, from the beginning to end.
17 * This method MUST attempt to seek to the beginning of the stream before
18 * reading data and read the stream until the end is reached.
20 * Warning: This could attempt to load a large amount of data into memory.
22 * This method MUST NOT raise an exception in order to conform with PHP's
23 * string casting operations.
25 * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
28 public function __toString();
31 * Closes the stream and any underlying resources.
35 public function close();
38 * Separates any underlying resources from the stream.
40 * After the stream has been detached, the stream is in an unusable state.
42 * @return resource|null Underlying PHP stream, if any
44 public function detach();
47 * Get the size of the stream if known.
49 * @return int|null Returns the size in bytes if known, or null if unknown.
51 public function getSize();
54 * Returns the current position of the file read/write pointer
56 * @return int Position of the file pointer
57 * @throws \RuntimeException on error.
59 public function tell();
62 * Returns true if the stream is at the end of the stream.
66 public function eof();
69 * Returns whether or not the stream is seekable.
73 public function isSeekable();
76 * Seek to a position in the stream.
78 * @link http://www.php.net/manual/en/function.fseek.php
79 * @param int $offset Stream offset
80 * @param int $whence Specifies how the cursor position will be calculated
81 * based on the seek offset. Valid values are identical to the built-in
82 * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
83 * offset bytes SEEK_CUR: Set position to current location plus offset
84 * SEEK_END: Set position to end-of-stream plus offset.
85 * @throws \RuntimeException on failure.
87 public function seek($offset, $whence = SEEK_SET);
90 * Seek to the beginning of the stream.
92 * If the stream is not seekable, this method will raise an exception;
93 * otherwise, it will perform a seek(0).
96 * @link http://www.php.net/manual/en/function.fseek.php
97 * @throws \RuntimeException on failure.
99 public function rewind();
102 * Returns whether or not the stream is writable.
106 public function isWritable();
109 * Write data to the stream.
111 * @param string $string The string that is to be written.
112 * @return int Returns the number of bytes written to the stream.
113 * @throws \RuntimeException on failure.
115 public function write($string);
118 * Returns whether or not the stream is readable.
122 public function isReadable();
125 * Read data from the stream.
127 * @param int $length Read up to $length bytes from the object and return
128 * them. Fewer than $length bytes may be returned if underlying stream
129 * call returns fewer bytes.
130 * @return string Returns the data read from the stream, or an empty string
131 * if no bytes are available.
132 * @throws \RuntimeException if an error occurs.
134 public function read($length);
137 * Returns the remaining contents in a string
140 * @throws \RuntimeException if unable to read or an error occurs while
143 public function getContents();
146 * Get stream metadata as an associative array or retrieve a specific key.
148 * The keys returned are identical to the keys returned from PHP's
149 * stream_get_meta_data() function.
151 * @link http://php.net/manual/en/function.stream-get-meta-data.php
152 * @param string $key Specific metadata to retrieve.
153 * @return array|mixed|null Returns an associative array if no key is
154 * provided. Returns a specific key value if a key is provided and the
155 * value is found, or null if the key is not found.
157 public function getMetadata($key = null);