Chris@0: hwm = $hwm; Chris@0: } Chris@0: Chris@0: public function __toString() Chris@0: { Chris@0: return $this->getContents(); Chris@0: } Chris@0: Chris@0: public function getContents() Chris@0: { Chris@0: $buffer = $this->buffer; Chris@0: $this->buffer = ''; Chris@0: Chris@0: return $buffer; Chris@0: } Chris@0: Chris@0: public function close() Chris@0: { Chris@0: $this->buffer = ''; Chris@0: } Chris@0: Chris@0: public function detach() Chris@0: { Chris@0: $this->close(); Chris@0: } Chris@0: Chris@0: public function getSize() Chris@0: { Chris@0: return strlen($this->buffer); Chris@0: } Chris@0: Chris@0: public function isReadable() Chris@0: { Chris@0: return true; Chris@0: } Chris@0: Chris@0: public function isWritable() Chris@0: { Chris@0: return true; Chris@0: } Chris@0: Chris@0: public function isSeekable() Chris@0: { Chris@0: return false; Chris@0: } Chris@0: Chris@0: public function rewind() Chris@0: { Chris@0: $this->seek(0); Chris@0: } Chris@0: Chris@0: public function seek($offset, $whence = SEEK_SET) Chris@0: { Chris@0: throw new \RuntimeException('Cannot seek a BufferStream'); Chris@0: } Chris@0: Chris@0: public function eof() Chris@0: { Chris@0: return strlen($this->buffer) === 0; Chris@0: } Chris@0: Chris@0: public function tell() Chris@0: { Chris@0: throw new \RuntimeException('Cannot determine the position of a BufferStream'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reads data from the buffer. Chris@0: */ Chris@0: public function read($length) Chris@0: { Chris@0: $currentLength = strlen($this->buffer); Chris@0: Chris@0: if ($length >= $currentLength) { Chris@0: // No need to slice the buffer because we don't have enough data. Chris@0: $result = $this->buffer; Chris@0: $this->buffer = ''; Chris@0: } else { Chris@0: // Slice up the result to provide a subset of the buffer. Chris@0: $result = substr($this->buffer, 0, $length); Chris@0: $this->buffer = substr($this->buffer, $length); Chris@0: } Chris@0: Chris@0: return $result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes data to the buffer. Chris@0: */ Chris@0: public function write($string) Chris@0: { Chris@0: $this->buffer .= $string; Chris@0: Chris@0: // TODO: What should happen here? Chris@0: if (strlen($this->buffer) >= $this->hwm) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return strlen($string); Chris@0: } Chris@0: Chris@0: public function getMetadata($key = null) Chris@0: { Chris@0: if ($key == 'hwm') { Chris@0: return $this->hwm; Chris@0: } Chris@0: Chris@0: return $key ? null : []; Chris@0: } Chris@0: }