contents = $contents; } public function copyToStream( $stream ) { fwrite( $stream, $this->getContents() ); } public function __toString() { return $this->contents; } public function close() { } public function detach() { return null; } public function getSize() { return strlen( $this->contents ); } public function tell() { return $this->offset; } public function eof() { return $this->offset >= strlen( $this->contents ); } public function isSeekable() { return true; } public function seek( $offset, $whence = SEEK_SET ) { switch ( $whence ) { case SEEK_SET: $this->offset = $offset; break; case SEEK_CUR: $this->offset += $offset; break; case SEEK_END: $this->offset = strlen( $this->contents ) + $offset; break; default: throw new \InvalidArgumentException( "Invalid value for \$whence" ); } if ( $this->offset > strlen( $this->contents ) ) { throw new \InvalidArgumentException( "Cannot seek beyond the end of a StringStream" ); } if ( $this->offset < 0 ) { throw new \InvalidArgumentException( "Cannot seek before the start of a StringStream" ); } } public function rewind() { $this->offset = 0; } public function isWritable() { return true; } public function write( $string ) { if ( $this->offset === strlen( $this->contents ) ) { $this->contents .= $string; } else { $this->contents = substr_replace( $this->contents, $string, $this->offset, strlen( $string ) ); } $this->offset += strlen( $string ); return strlen( $string ); } public function isReadable() { return true; } public function read( $length ) { if ( $this->offset === 0 && $length >= strlen( $this->contents ) ) { $ret = $this->contents; } elseif ( $this->offset >= strlen( $this->contents ) ) { $ret = ''; } else { $ret = substr( $this->contents, $this->offset, $length ); } $this->offset += strlen( $ret ); return $ret; } public function getContents() { if ( $this->offset === 0 ) { $ret = $this->contents; } elseif ( $this->offset >= strlen( $this->contents ) ) { $ret = ''; } else { $ret = substr( $this->contents, $this->offset ); } $this->offset = strlen( $this->contents ); return $ret; } public function getMetadata( $key = null ) { return null; } }