Open Journal Systems  3.3.0
BufferStream.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
14 class BufferStream implements StreamInterface
15 {
16  private $hwm;
17  private $buffer = '';
18 
26  public function __construct($hwm = 16384)
27  {
28  $this->hwm = $hwm;
29  }
30 
31  public function __toString()
32  {
33  return $this->getContents();
34  }
35 
36  public function getContents()
37  {
38  $buffer = $this->buffer;
39  $this->buffer = '';
40 
41  return $buffer;
42  }
43 
44  public function close()
45  {
46  $this->buffer = '';
47  }
48 
49  public function detach()
50  {
51  $this->close();
52  }
53 
54  public function getSize()
55  {
56  return strlen($this->buffer);
57  }
58 
59  public function isReadable()
60  {
61  return true;
62  }
63 
64  public function isWritable()
65  {
66  return true;
67  }
68 
69  public function isSeekable()
70  {
71  return false;
72  }
73 
74  public function rewind()
75  {
76  $this->seek(0);
77  }
78 
79  public function seek($offset, $whence = SEEK_SET)
80  {
81  throw new \RuntimeException('Cannot seek a BufferStream');
82  }
83 
84  public function eof()
85  {
86  return strlen($this->buffer) === 0;
87  }
88 
89  public function tell()
90  {
91  throw new \RuntimeException('Cannot determine the position of a BufferStream');
92  }
93 
97  public function read($length)
98  {
99  $currentLength = strlen($this->buffer);
100 
101  if ($length >= $currentLength) {
102  // No need to slice the buffer because we don't have enough data.
103  $result = $this->buffer;
104  $this->buffer = '';
105  } else {
106  // Slice up the result to provide a subset of the buffer.
107  $result = substr($this->buffer, 0, $length);
108  $this->buffer = substr($this->buffer, $length);
109  }
110 
111  return $result;
112  }
113 
117  public function write($string)
118  {
119  $this->buffer .= $string;
120 
121  // TODO: What should happen here?
122  if (strlen($this->buffer) >= $this->hwm) {
123  return false;
124  }
125 
126  return strlen($string);
127  }
128 
129  public function getMetadata($key = null)
130  {
131  if ($key == 'hwm') {
132  return $this->hwm;
133  }
134 
135  return $key ? null : [];
136  }
137 }
GuzzleHttp\Psr7\BufferStream\rewind
rewind()
Definition: BufferStream.php:74
GuzzleHttp\Psr7\BufferStream\read
read($length)
Definition: BufferStream.php:97
GuzzleHttp\Psr7\BufferStream
Definition: BufferStream.php:14
GuzzleHttp\Psr7\BufferStream\isWritable
isWritable()
Definition: BufferStream.php:64
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\BufferStream\isReadable
isReadable()
Definition: BufferStream.php:59
GuzzleHttp\Psr7\BufferStream\tell
tell()
Definition: BufferStream.php:89
GuzzleHttp\Psr7\BufferStream\write
write($string)
Definition: BufferStream.php:117
GuzzleHttp\Psr7\BufferStream\getContents
getContents()
Definition: BufferStream.php:36
GuzzleHttp\Psr7\BufferStream\getMetadata
getMetadata($key=null)
Definition: BufferStream.php:129
GuzzleHttp\Psr7\BufferStream\detach
detach()
Definition: BufferStream.php:49
GuzzleHttp\Psr7\BufferStream\isSeekable
isSeekable()
Definition: BufferStream.php:69
GuzzleHttp\Psr7\BufferStream\__toString
__toString()
Definition: BufferStream.php:31
GuzzleHttp\Psr7
Definition: AppendStream.php:2
GuzzleHttp\Psr7\BufferStream\eof
eof()
Definition: BufferStream.php:84
GuzzleHttp\Psr7\BufferStream\seek
seek($offset, $whence=SEEK_SET)
Definition: BufferStream.php:79
GuzzleHttp\Psr7\BufferStream\__construct
__construct($hwm=16384)
Definition: BufferStream.php:26
GuzzleHttp\Psr7\BufferStream\getSize
getSize()
Definition: BufferStream.php:54
GuzzleHttp\Psr7\BufferStream\close
close()
Definition: BufferStream.php:44