631c41e0fcb366c555697986aaf768284afb1a0d
[yaffs-website] / zendframework / zend-diactoros / src / RelativeStream.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
5  * @see       http://github.com/zendframework/zend-diactoros for the canonical source repository
6  * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7  * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8  */
9
10 namespace Zend\Diactoros;
11
12 use Psr\Http\Message\StreamInterface;
13 use RuntimeException;
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         $this->seek(0);
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 }