StreamInterface.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. namespace Psr\Http\Message;
  4. /**
  5. * Describes a data stream.
  6. *
  7. * Typically, an instance will wrap a PHP stream; this interface provides
  8. * a wrapper around the most common operations, including serialization of
  9. * the entire stream to a string.
  10. */
  11. interface StreamInterface
  12. {
  13. /**
  14. * Reads all data from the stream into a string, from the beginning to end.
  15. *
  16. * This method MUST attempt to seek to the beginning of the stream before
  17. * reading data and read the stream until the end is reached.
  18. *
  19. * Warning: This could attempt to load a large amount of data into memory.
  20. *
  21. * This method MUST NOT raise an exception in order to conform with PHP's
  22. * string casting operations.
  23. *
  24. * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
  25. * @return string
  26. */
  27. public function __toString();
  28. /**
  29. * Closes the stream and any underlying resources.
  30. *
  31. * @return void
  32. */
  33. public function close();
  34. /**
  35. * Separates any underlying resources from the stream.
  36. *
  37. * After the stream has been detached, the stream is in an unusable state.
  38. *
  39. * @return resource|null Underlying PHP stream, if any
  40. */
  41. public function detach();
  42. /**
  43. * Get the size of the stream if known.
  44. *
  45. * @return int|null Returns the size in bytes if known, or null if unknown.
  46. */
  47. public function getSize();
  48. /**
  49. * Returns the current position of the file read/write pointer
  50. *
  51. * @return int Position of the file pointer
  52. * @throws \RuntimeException on error.
  53. */
  54. public function tell();
  55. /**
  56. * Returns true if the stream is at the end of the stream.
  57. *
  58. * @return bool
  59. */
  60. public function eof();
  61. /**
  62. * Returns whether or not the stream is seekable.
  63. *
  64. * @return bool
  65. */
  66. public function isSeekable();
  67. /**
  68. * Seek to a position in the stream.
  69. *
  70. * @link http://www.php.net/manual/en/function.fseek.php
  71. * @param int $offset Stream offset
  72. * @param int $whence Specifies how the cursor position will be calculated
  73. * based on the seek offset. Valid values are identical to the built-in
  74. * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
  75. * offset bytes SEEK_CUR: Set position to current location plus offset
  76. * SEEK_END: Set position to end-of-stream plus offset.
  77. * @throws \RuntimeException on failure.
  78. */
  79. public function seek(int $offset, int $whence = SEEK_SET);
  80. /**
  81. * Seek to the beginning of the stream.
  82. *
  83. * If the stream is not seekable, this method will raise an exception;
  84. * otherwise, it will perform a seek(0).
  85. *
  86. * @see seek()
  87. * @link http://www.php.net/manual/en/function.fseek.php
  88. * @throws \RuntimeException on failure.
  89. */
  90. public function rewind();
  91. /**
  92. * Returns whether or not the stream is writable.
  93. *
  94. * @return bool
  95. */
  96. public function isWritable();
  97. /**
  98. * Write data to the stream.
  99. *
  100. * @param string $string The string that is to be written.
  101. * @return int Returns the number of bytes written to the stream.
  102. * @throws \RuntimeException on failure.
  103. */
  104. public function write(string $string);
  105. /**
  106. * Returns whether or not the stream is readable.
  107. *
  108. * @return bool
  109. */
  110. public function isReadable();
  111. /**
  112. * Read data from the stream.
  113. *
  114. * @param int $length Read up to $length bytes from the object and return
  115. * them. Fewer than $length bytes may be returned if underlying stream
  116. * call returns fewer bytes.
  117. * @return string Returns the data read from the stream, or an empty string
  118. * if no bytes are available.
  119. * @throws \RuntimeException if an error occurs.
  120. */
  121. public function read(int $length);
  122. /**
  123. * Returns the remaining contents in a string
  124. *
  125. * @return string
  126. * @throws \RuntimeException if unable to read or an error occurs while
  127. * reading.
  128. */
  129. public function getContents();
  130. /**
  131. * Get stream metadata as an associative array or retrieve a specific key.
  132. *
  133. * The keys returned are identical to the keys returned from PHP's
  134. * stream_get_meta_data() function.
  135. *
  136. * @link http://php.net/manual/en/function.stream-get-meta-data.php
  137. * @param string|null $key Specific metadata to retrieve.
  138. * @return array|mixed|null Returns an associative array if no key is
  139. * provided. Returns a specific key value if a key is provided and the
  140. * value is found, or null if the key is not found.
  141. */
  142. public function getMetadata(?string $key = null);
  143. }