1 | <?php |
||
34 | class IteratorStream implements Streamable |
||
35 | { |
||
36 | use IsReadable, IsWritable; |
||
37 | |||
38 | /** |
||
39 | * Buffer stream |
||
40 | * @var \CSVelte\IO\BufferStream A BufferStream object |
||
41 | */ |
||
42 | protected $buffer; |
||
43 | |||
44 | protected $overflow; |
||
45 | |||
46 | /** |
||
47 | * Is stream readable? |
||
48 | * |
||
49 | * @var boolean Whether stream is readable |
||
50 | */ |
||
51 | protected $readable = true; |
||
52 | |||
53 | /** |
||
54 | * Is stream writable? |
||
55 | * |
||
56 | * @var boolean Whether stream is writable |
||
57 | */ |
||
58 | protected $writable = false; |
||
59 | |||
60 | /** |
||
61 | * Is stream seekable? |
||
62 | * |
||
63 | * @var boolean Whether stream is seekable |
||
64 | */ |
||
65 | protected $seekable = false; |
||
66 | |||
67 | /** |
||
68 | * @var array Any additional options / meta data |
||
69 | */ |
||
70 | protected $meta = [ |
||
71 | |||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * Instantiate an iterator stream |
||
76 | * |
||
77 | * Instantiate a new iterator stream. The iterator is used to continually |
||
78 | * refill a buffer as it is drained by read operations. |
||
79 | * |
||
80 | * @param \Iterator The iterator to stream data from |
||
81 | * @param \CSVelte\IO\BufferIterator|null Either a buffer or null (to use |
||
82 | * default buffer) |
||
83 | */ |
||
84 | 14 | public function __construct(Iterator $iter, $buffer = null) |
|
92 | |||
93 | /** |
||
94 | * Readability accessor. |
||
95 | * |
||
96 | * Despite the fact that any class that implements this interface must also |
||
97 | * define methods such as read and readLine, that is no guarantee that an |
||
98 | * object will necessarily be readable. This method should tell the user |
||
99 | * whether a stream is, in fact, readable. |
||
100 | * |
||
101 | * @return boolean True if readable, false otherwise |
||
102 | */ |
||
103 | 1 | public function isReadable() |
|
104 | { |
||
105 | 1 | return $this->readable; |
|
106 | } |
||
107 | |||
108 | 10 | public function read($bytes) |
|
122 | |||
123 | 10 | protected function inflateBuffer() |
|
131 | |||
132 | /** |
||
133 | * Read the entire stream, beginning to end. |
||
134 | * |
||
135 | * In most stream implementations, __toString() differs from getContents() |
||
136 | * in that it returns the entire stream rather than just the remainder, but |
||
137 | * due to the way this stream works (sort of like a conveyor belt), this |
||
138 | * method is an alias to getContents() |
||
139 | * |
||
140 | * @return string The entire stream, beginning to end |
||
141 | */ |
||
142 | 3 | public function __toString() |
|
147 | |||
148 | /** |
||
149 | * Read the remainder of the stream |
||
150 | * |
||
151 | * @return string The remainder of the stream |
||
152 | */ |
||
153 | 4 | public function getContents() |
|
165 | |||
166 | /** |
||
167 | * Return the size (in bytes) of this stream (if known). |
||
168 | * |
||
169 | * @return int|null Size (in bytes) of this stream |
||
170 | */ |
||
171 | 1 | public function getSize() |
|
172 | { |
||
173 | // no way to know so return null |
||
174 | 1 | } |
|
175 | |||
176 | /** |
||
177 | * Return the current position within the stream/readable |
||
178 | * |
||
179 | * @return int The current position within readable |
||
180 | */ |
||
181 | public function tell() |
||
185 | |||
186 | /** |
||
187 | * Determine whether the end of the stream has been reached |
||
188 | * |
||
189 | * @return boolean Whether we're at the end of the stream |
||
190 | */ |
||
191 | 4 | public function eof() |
|
198 | |||
199 | /** |
||
200 | * Rewind to beginning of stream |
||
201 | */ |
||
202 | 3 | public function rewind() |
|
207 | |||
208 | /** |
||
209 | * Get stream metadata as an associative array or retrieve a specific key. |
||
210 | * |
||
211 | * The keys returned are identical to the keys returned from PHP's |
||
212 | * stream_get_meta_data() function. |
||
213 | * |
||
214 | * @param string $key Specific metadata to retrieve. |
||
215 | * @return array|mixed|null Returns an associative array if no key is |
||
216 | * provided. Returns a specific key value if a key is provided and the |
||
217 | * value is found, or null if the key is not found. |
||
218 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
219 | */ |
||
220 | 2 | public function getMetadata($key = null) |
|
221 | { |
||
222 | 2 | if (!is_null($key)) { |
|
223 | 1 | return isset($this->meta[$key]) ? $this->meta[$key] : null; |
|
224 | } |
||
225 | 1 | return $this->meta; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Closes the stream and any underlying resources. |
||
230 | * |
||
231 | * @return void |
||
232 | */ |
||
233 | public function close() |
||
242 | |||
243 | /** |
||
244 | * Separates any underlying resources from the stream. |
||
245 | * |
||
246 | * After the stream has been detached, the stream is in an unusable state. |
||
247 | * |
||
248 | * @return string|null Underlying PHP stream, if any |
||
249 | * @todo I'm not sure what detach is for so I don't know whether what I'm |
||
250 | * doing here is right. The reason I have the method at all is because |
||
251 | * psr7 StreamInterface has one.f |
||
252 | */ |
||
253 | public function detach() |
||
261 | |||
262 | /** |
||
263 | * Writability accessor. |
||
264 | * |
||
265 | * Despite the fact that any class that implements this interface must also |
||
266 | * define methods such as write and writeLine, that is no guarantee that an |
||
267 | * object will necessarily be writable. This method should tell the user |
||
268 | * whether a stream is, in fact, writable. |
||
269 | * |
||
270 | * @return boolean True if writable, false otherwise |
||
271 | */ |
||
272 | 1 | public function isWritable() |
|
273 | { |
||
274 | 1 | return $this->writable; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Write data to the output. |
||
279 | * |
||
280 | * @param string The data to write |
||
281 | * @return int The number of bytes written |
||
282 | */ |
||
283 | public function write($data) |
||
287 | |||
288 | /** |
||
289 | * Seekability accessor. |
||
290 | * |
||
291 | * Despite the fact that any class that implements this interface must also |
||
292 | * define methods such as seek, that is no guarantee that an |
||
293 | * object will necessarily be seekable. This method should tell the user |
||
294 | * whether a stream is, in fact, seekable. |
||
295 | * |
||
296 | * @return boolean True if seekable, false otherwise |
||
297 | */ |
||
298 | 1 | public function isSeekable() |
|
302 | |||
303 | /** |
||
304 | * Seek to specified offset. |
||
305 | * |
||
306 | * @param integer Offset to seek to |
||
307 | * @param integer Position from whence the offset should be applied |
||
308 | * @return boolean True if seek was successful |
||
309 | */ |
||
310 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
314 | |||
315 | } |
||
316 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: