5f2eb915e12a9838548f454b4e4878d2d55ac394
[yaffs-website] / src / RelativeStream.php
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  */
7
8 namespace Zend\Diactoros;
9
10 use Psr\Http\Message\StreamInterface;
11 use RuntimeException;
12
13 use const SEEK_SET;
14
15 /**
16  * Class RelativeStream
17  *
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()
21  */
22 final class RelativeStream implements StreamInterface
23 {
24     /**
25      * @var StreamInterface
26      */
27     private $decoratedStream;
28
29     /**
30      * @var int
31      */
32     private $offset;
33
34     /**
35      * Class constructor
36      *
37      * @param StreamInterface $decoratedStream
38      * @param int $offset
39      */
40     public function __construct(StreamInterface $decoratedStream, $offset)
41     {
42         $this->decoratedStream = $decoratedStream;
43         $this->offset = (int)$offset;
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     public function __toString()
50     {
51         if ($this->isSeekable()) {
52             $this->seek(0);
53         }
54         return $this->getContents();
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     public function close()
61     {
62         $this->decoratedStream->close();
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     public function detach()
69     {
70         return $this->decoratedStream->detach();
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     public function getSize()
77     {
78         return $this->decoratedStream->getSize() - $this->offset;
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     public function tell()
85     {
86         return $this->decoratedStream->tell() - $this->offset;
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     public function eof()
93     {
94         return $this->decoratedStream->eof();
95     }
96
97     /**
98      * {@inheritdoc}
99      */
100     public function isSeekable()
101     {
102         return $this->decoratedStream->isSeekable();
103     }
104
105     /**
106      * {@inheritdoc}
107      */
108     public function seek($offset, $whence = SEEK_SET)
109     {
110         if ($whence == SEEK_SET) {
111             return $this->decoratedStream->seek($offset + $this->offset, $whence);
112         }
113         return $this->decoratedStream->seek($offset, $whence);
114     }
115
116     /**
117      * {@inheritdoc}
118      */
119     public function rewind()
120     {
121         return $this->seek(0);
122     }
123
124     /**
125      * {@inheritdoc}
126      */
127     public function isWritable()
128     {
129         return $this->decoratedStream->isWritable();
130     }
131
132     /**
133      * {@inheritdoc}
134      */
135     public function write($string)
136     {
137         if ($this->tell() < 0) {
138             throw new RuntimeException('Invalid pointer position');
139         }
140         return $this->decoratedStream->write($string);
141     }
142
143     /**
144      * {@inheritdoc}
145      */
146     public function isReadable()
147     {
148         return $this->decoratedStream->isReadable();
149     }
150
151     /**
152      * {@inheritdoc}
153      */
154     public function read($length)
155     {
156         if ($this->tell() < 0) {
157             throw new RuntimeException('Invalid pointer position');
158         }
159         return $this->decoratedStream->read($length);
160     }
161
162     /**
163      * {@inheritdoc}
164      */
165     public function getContents()
166     {
167         if ($this->tell() < 0) {
168             throw new RuntimeException('Invalid pointer position');
169         }
170         return $this->decoratedStream->getContents();
171     }
172
173     /**
174      * {@inheritdoc}
175      */
176     public function getMetadata($key = null)
177     {
178         return $this->decoratedStream->getMetadata($key);
179     }
180 }