1 | <?php |
||
37 | class IteratorStream implements Streamable |
||
38 | { |
||
39 | use IsReadable, IsWritable; |
||
40 | |||
41 | /** |
||
42 | * Buffer stream |
||
43 | * @var \CSVelte\IO\BufferStream A BufferStream object |
||
44 | */ |
||
45 | protected $buffer; |
||
46 | |||
47 | protected $overflow; |
||
48 | |||
49 | /** |
||
50 | * Is stream readable? |
||
51 | * |
||
52 | * @var boolean Whether stream is readable |
||
53 | */ |
||
54 | protected $readable = true; |
||
55 | |||
56 | /** |
||
57 | * Is stream writable? |
||
58 | * |
||
59 | * @var boolean Whether stream is writable |
||
60 | */ |
||
61 | protected $writable = false; |
||
62 | |||
63 | /** |
||
64 | * Is stream seekable? |
||
65 | * |
||
66 | * @var boolean Whether stream is seekable |
||
67 | */ |
||
68 | protected $seekable = false; |
||
69 | |||
70 | /** |
||
71 | * @var array Any additional options / meta data |
||
72 | */ |
||
73 | protected $meta = [ |
||
74 | |||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * Instantiate an iterator stream |
||
79 | * |
||
80 | * Instantiate a new iterator stream. The iterator is used to continually |
||
81 | * refill a buffer as it is drained by read operations. |
||
82 | * |
||
83 | * @param \Iterator The iterator to stream data from |
||
84 | * @param \CSVelte\IO\BufferIterator|null Either a buffer or null (to use |
||
85 | * default buffer) |
||
86 | * @todo this should expect a BufferInterface or a Bufferable rather than |
||
87 | * a BufferStream |
||
88 | */ |
||
89 | 21 | public function __construct(Traversable $iter, $buffer = null) |
|
105 | |||
106 | /** |
||
107 | * Readability accessor. |
||
108 | * |
||
109 | * Despite the fact that any class that implements this interface must also |
||
110 | * define methods such as read and readLine, that is no guarantee that an |
||
111 | * object will necessarily be readable. This method should tell the user |
||
112 | * whether a stream is, in fact, readable. |
||
113 | * |
||
114 | * @return boolean True if readable, false otherwise |
||
115 | */ |
||
116 | 2 | public function isReadable() |
|
120 | |||
121 | 13 | public function read($bytes) |
|
139 | |||
140 | 11 | protected function inflateBuffer() |
|
148 | |||
149 | /** |
||
150 | * Read the entire stream, beginning to end. |
||
151 | * |
||
152 | * In most stream implementations, __toString() differs from getContents() |
||
153 | * in that it returns the entire stream rather than just the remainder, but |
||
154 | * due to the way this stream works (sort of like a conveyor belt), this |
||
155 | * method is an alias to getContents() |
||
156 | * |
||
157 | * @return string The entire stream, beginning to end |
||
158 | */ |
||
159 | 3 | public function __toString() |
|
164 | |||
165 | /** |
||
166 | * Read the remainder of the stream |
||
167 | * |
||
168 | * @return string The remainder of the stream |
||
169 | */ |
||
170 | 4 | public function getContents() |
|
182 | |||
183 | /** |
||
184 | * Return the size (in bytes) of this stream (if known). |
||
185 | * |
||
186 | * @return int|null Size (in bytes) of this stream |
||
187 | */ |
||
188 | 1 | public function getSize() |
|
192 | |||
193 | /** |
||
194 | * Return the current position within the stream/readable |
||
195 | * |
||
196 | * I can't decide whether there is any meaningful way to "tell" the |
||
197 | * current position within this type of stream. For now I'm just |
||
198 | * going to return false because the nature of this type of stream |
||
199 | * means that it technically has no definitive beginning or end and |
||
200 | * therefor no absolute position. If a "tell" is truly needed, I |
||
201 | * suppose I could keep track of how many bytes have been read over |
||
202 | * the lifetime of the object, but I don't think that is meaningful |
||
203 | * and/or useful. |
||
204 | * |
||
205 | * @return int The current position within readable |
||
206 | */ |
||
207 | 1 | public function tell() |
|
211 | |||
212 | /** |
||
213 | * Determine whether the end of the stream has been reached |
||
214 | * |
||
215 | * @return boolean Whether we're at the end of the stream |
||
216 | */ |
||
217 | 4 | public function eof() |
|
224 | |||
225 | /** |
||
226 | * Rewind to beginning of stream |
||
227 | */ |
||
228 | 3 | public function rewind() |
|
233 | |||
234 | /** |
||
235 | * Get stream metadata as an associative array or retrieve a specific key. |
||
236 | * |
||
237 | * The keys returned are identical to the keys returned from PHP's |
||
238 | * stream_get_meta_data() function. |
||
239 | * |
||
240 | * @param string $key Specific metadata to retrieve. |
||
241 | * @return array|mixed|null Returns an associative array if no key is |
||
242 | * provided. Returns a specific key value if a key is provided and the |
||
243 | * value is found, or null if the key is not found. |
||
244 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
245 | */ |
||
246 | 2 | public function getMetadata($key = null) |
|
253 | |||
254 | /** |
||
255 | * Closes the stream and any underlying resources. |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | 1 | public function close() |
|
269 | |||
270 | /** |
||
271 | * Separates any underlying resources from the stream. |
||
272 | * |
||
273 | * After the stream has been detached, the stream is in an unusable state. |
||
274 | * |
||
275 | * @return string|null Underlying PHP stream, if any |
||
276 | * @todo I'm not sure what detach is for so I don't know whether what I'm |
||
277 | * doing here is right. The reason I have the method at all is because |
||
278 | * psr7 StreamInterface has one.f |
||
279 | */ |
||
280 | 1 | public function detach() |
|
289 | |||
290 | /** |
||
291 | * Writability accessor. |
||
292 | * |
||
293 | * Despite the fact that any class that implements this interface must also |
||
294 | * define methods such as write and writeLine, that is no guarantee that an |
||
295 | * object will necessarily be writable. This method should tell the user |
||
296 | * whether a stream is, in fact, writable. |
||
297 | * |
||
298 | * @return boolean True if writable, false otherwise |
||
299 | */ |
||
300 | 1 | public function isWritable() |
|
304 | |||
305 | /** |
||
306 | * Write data to the output. |
||
307 | * |
||
308 | * @param string The data to write |
||
309 | * @return int The number of bytes written |
||
310 | */ |
||
311 | 2 | public function write($data) |
|
315 | |||
316 | /** |
||
317 | * Seekability accessor. |
||
318 | * |
||
319 | * Despite the fact that any class that implements this interface must also |
||
320 | * define methods such as seek, that is no guarantee that an |
||
321 | * object will necessarily be seekable. This method should tell the user |
||
322 | * whether a stream is, in fact, seekable. |
||
323 | * |
||
324 | * @return boolean True if seekable, false otherwise |
||
325 | */ |
||
326 | 2 | public function isSeekable() |
|
330 | |||
331 | /** |
||
332 | * Seek to specified offset. |
||
333 | * |
||
334 | * @param integer Offset to seek to |
||
335 | * @param integer Position from whence the offset should be applied |
||
336 | * @return boolean True if seek was successful |
||
337 | */ |
||
338 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
342 | |||
343 | } |
||
344 |
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: