composer package updates
[openemr.git] / vendor / zendframework / zend-diactoros / src / RelativeStream.php
blob5f2eb915e12a9838548f454b4e4878d2d55ac394
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Diactoros;
10 use Psr\Http\Message\StreamInterface;
11 use RuntimeException;
13 use const SEEK_SET;
15 /**
16 * Class RelativeStream
18 * Wrapper for default Stream class, representing subpart (starting from given offset) of initial stream.
19 * It can be used to avoid copying full stream, conserving memory.
20 * @example see Zend\Diactoros\AbstractSerializer::splitStream()
22 final class RelativeStream implements StreamInterface
24 /**
25 * @var StreamInterface
27 private $decoratedStream;
29 /**
30 * @var int
32 private $offset;
34 /**
35 * Class constructor
37 * @param StreamInterface $decoratedStream
38 * @param int $offset
40 public function __construct(StreamInterface $decoratedStream, $offset)
42 $this->decoratedStream = $decoratedStream;
43 $this->offset = (int)$offset;
46 /**
47 * {@inheritdoc}
49 public function __toString()
51 if ($this->isSeekable()) {
52 $this->seek(0);
54 return $this->getContents();
57 /**
58 * {@inheritdoc}
60 public function close()
62 $this->decoratedStream->close();
65 /**
66 * {@inheritdoc}
68 public function detach()
70 return $this->decoratedStream->detach();
73 /**
74 * {@inheritdoc}
76 public function getSize()
78 return $this->decoratedStream->getSize() - $this->offset;
81 /**
82 * {@inheritdoc}
84 public function tell()
86 return $this->decoratedStream->tell() - $this->offset;
89 /**
90 * {@inheritdoc}
92 public function eof()
94 return $this->decoratedStream->eof();
97 /**
98 * {@inheritdoc}
100 public function isSeekable()
102 return $this->decoratedStream->isSeekable();
106 * {@inheritdoc}
108 public function seek($offset, $whence = SEEK_SET)
110 if ($whence == SEEK_SET) {
111 return $this->decoratedStream->seek($offset + $this->offset, $whence);
113 return $this->decoratedStream->seek($offset, $whence);
117 * {@inheritdoc}
119 public function rewind()
121 return $this->seek(0);
125 * {@inheritdoc}
127 public function isWritable()
129 return $this->decoratedStream->isWritable();
133 * {@inheritdoc}
135 public function write($string)
137 if ($this->tell() < 0) {
138 throw new RuntimeException('Invalid pointer position');
140 return $this->decoratedStream->write($string);
144 * {@inheritdoc}
146 public function isReadable()
148 return $this->decoratedStream->isReadable();
152 * {@inheritdoc}
154 public function read($length)
156 if ($this->tell() < 0) {
157 throw new RuntimeException('Invalid pointer position');
159 return $this->decoratedStream->read($length);
163 * {@inheritdoc}
165 public function getContents()
167 if ($this->tell() < 0) {
168 throw new RuntimeException('Invalid pointer position');
170 return $this->decoratedStream->getContents();
174 * {@inheritdoc}
176 public function getMetadata($key = null)
178 return $this->decoratedStream->getMetadata($key);