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